
|
Perhaps not so much a bug as an unexpected feature, the aptly named Spirograph bug can be seen when the Graphics.Widen method is used several times on GraphicsPath objects that contain curves. It produces a regular and often highly interesting shape which can be pleasing to the eye but is nevertheless undesirable. The problem arises from the difficult process of deciding the mitre limits of joined line segments.To see the Spirograph bug in action create a GraphicsPath object and add an ellipse to it of, say, 200*200. Then widen the ellipse using a pen width of 50. This produces a torus. Then widen the path a second time with a pen width of 50 or 25. The resulting graphics path is not what you'd expect.
Figure 1. The Spirograph bug in action. The code that produced this shape is shown in listing 1. It's all in the Paint event handler. using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace SpirographBug
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw,true);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
GraphicsPath pth=new GraphicsPath();
pth.AddEllipse(this.ClientSize.Width/4,this.ClientSize.Height/4,this.ClientSize.Width/2,this.ClientSize.Height/2);
Pen p=new Pen(Color.Black,50);
pth.Widen(p);
p.Width=25;
pth.Widen(p);
e.Graphics.FillPath(Brushes.LightBlue,pth);
e.Graphics.DrawPath(Pens.DarkBlue,pth);
pth.Dispose();
p.Dispose();
}
}
}
The Spirograph toy is one of the best selling toys ever. During the 1960's and early 1970's almost every child in the western hemisphere had one or had played with one. The current Spirograph is less sophisticated than the huge box of gears that were in the original but is nevertheless still a classic toy. Spirograph is a trademark of the Hasbro corporation. Click here to see the Spirograph toy. Copyright Robert W. Powell 2003. All rights reserved. |