Silverlight Spirograph

I've recently started reading the book Professional ASP.NET 2.0 Design: CSS, Themes, and Master Pages by Jacob J. Sanford and skipped ahead to Appendix B: An Introduction to Microsoft Silverlight. This chapter provides an excellent introduction to Silverlight projects using Visual Studio 2008 and the Silverlight 1.1 Alpha SDK. Unfortunately, the Wrox web site did not have a download for this chapter and I had to type in all the code and come up with one image. I learned that XAML tags are case sensitive! I finally got the analog clock example working. Clocks seem to be a popular demonstration project for Silverlight!

I learned enough to tackle my own project. A long time ago I found some sample code for generating spirograph designs and it is been my favorite algorithm ever since. I've used this code wherever a language permits one to draw lines. I've used Perl and the Tk GUI library to draw spirograph designs in Linux and I've even used it on my Palm handheld device. Since the analog clock example demonstrated drawing lines on the canvas dynamically that was all I needed to know to use my favorite graphic trick.

Silverlight Spirograph

The canvas does not require any objects because all of the lines will be drawn through code:

<Canvas x:Name="parentCanvas"
        xmlns="http://schemas.microsoft.com/client/2007"
        
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        
Loaded="Page_Loaded"
        
x:Class="Spirograph.Page;assembly=ClientBin/Spirograph.dll"
        Width="590"
        Height="590"
        Background="White"
        >
</
Canvas>

You can create a variety of interesting patterns just by changing the radius of the fixed circle or the offset of the moving circle. Usually I would add textboxes so you can change these values and then redraw the screen but I have not learned how to do this yet in XAML.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents; using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Spirograph
{
    
public partial class Page : Canvas
    {
        
public void Page_Loaded(object o, EventArgs e)
        {
            
// Required to initialize variables
            InitializeComponent();

            
int x = 0;
            
int y = 0;
            
int prevx = 0;
            
int prevy = 0;
            
int t;
            
int R = 35; // Radius fixed circle
            int I = 400; // Iterations
            int r = 1; // Radius moving circle
            int O = 150; // Offset in moving circle
            string hex = "#0000FF";
            
string xaml = "";
            
Line line = new Line();
            
Canvas parentCanvas = (Canvas)this.FindName("parentCanvas");

            
for (t = 0; t <= I; t++)
            {
                prevx = x;
                prevy = y;
                x = (
int)((R + r) * Math.Cos((double)t) - (r + O) * Math.Cos((double)(((R + r) / r) * t)));
                y = (
int)((R + r) * Math.Sin((double)t) - (r + O) * Math.Sin((double)(((R + r) / r) * t)));
                
if (t > 0)
                {
                    
if (hex == "#00FF00")
                    {
                        hex =
"#FF0000";
                     }
                    
else if (hex == "#0000FF")
                    {
                        hex =
"#00FF00";
                    }
                    
else if (hex == "#FF0000")
                    {
                        hex =
"#0000FF";
                    }
                    xaml =
"<Line X1=\"" + x + "\" Y1=\"" + y + "\" X2=\"" + x + "\" Y2=\"" + y + "\" Stroke=\"" + hex + "\" StrokeThickness=\"1\" Canvas.Left=\"289\" Canvas.Top=\"289\"></Line>";
                    
Debug.WriteLine(xaml, "xaml");
                    line = (
Line)XamlReader.Load(xaml);
                    parentCanvas.Children.Add(line);
                    xaml =
"<Line X1=\"" + prevx + "\" Y1=\"" + prevy + "\" X2=\"" + x + "\" Y2=\"" + y + "\" Stroke=\"" + hex + "\" StrokeThickness=\"1\" Canvas.Left=\"289\" Canvas.Top=\"289\"></Line>";
                    
Debug.WriteLine(xaml, "xaml");
                    line = (
Line)XamlReader.Load(xaml);
                    parentCanvas.Children.Add(line);
                }
            }
        }
    }

9 Comments

  • I ran across your blog and saw that you mention the download for Appendix B was missing. I am not sure what happened but I verified that the downloads weren't there for Appendix A or B on the wrox website. I contacted Wrox yesterday and resent the downloads for those appendices and I am hoping they are up later this week (although, it being a holiday, I'm not sure if that will happen).

    Hopefully, this didn't hurt your experience. If you need the downloads and they still aren't up, you can email me directly at jacob@captare.com and I will get them to you. :o)

    -Jacob

  • Thanks for making the downloads available, Jacob. I've been reading your book at work during lunch. I'm combining a lot of tips and tricks from various sources to develop a pure CSS web site design that I can use for all my projects.

  • That is what I did as well. In my last job, we were working on creating a template to be used on all .NET projects and I was tasked with doing all of the design stuff. I incorporated the CSS Control Adapters and a completely CSS-based design for the layout. The site was 100% CSS and compliant (and looked materially the same in opera, firefox, ie6, ie7, and safari). It was a challenge.

    Let me know if you get hung up on anything. I've done it before so I might be able to help. :o)

  • Silverlight spirograph.. Huh, really? :)

  • Silverlight spirograph.. Smashing :)

  • The increasing the rate of of your metabolism through supervision of Phen375 is entirely secure, currently folks having slow,
    or underactive metabolism that happen to be at risk of excess
    weight from the get go. A high level one that eats little but is in
    danger of involuntary extra weight, the probability is
    that your chosen metabolism is functioning to slow.
    phen375 side effects will rapidly sort out your
    issue, accelerating the metabolism ,eliminating your fats, and offering you a
    fully new lease of one's energy.

  • Keep this going please, great job!

  • Hello my family member! I wish to say that this post is awesome, great
    written and include almost all significant infos. I'd like to see more posts like this .

  • It's fantastic that you are getting thoughts from this piece of writing as well as from our argument made at this place.

Comments have been disabled for this content.