# How to draw a circle • ### Question

• Well...I am always having a hard time understanding coordinate system such as x and y.

anyway, my question is pretty simple: How to draw a circle on a panel, picturebox or form...any of them will do. I would like the circle to start at or in the middle of the panel and expand gradually...

However, what I ended up with was a circle expanding on the top left corner

```Circle |
---------O--------------
|

What I want is

the circle start in

the middle and expands in

all four direction with the same size.

But as

you can see, my circle (the word Circle on

the top left) ended up there and expands larger than the panel it resides.

So I do

not really know how to do

it.
```

• Moved by Sunday, November 7, 2010 2:04 PM : Move to more appropriate forum (From:Visual C# Language)
Sunday, November 7, 2010 5:08 AM

• Ok, here's a quick takeout of a testprogram, so the code might be a bit weird (I think, it was better to create a class "Spiral" that holds the data) The Form's Paint-Event should be hooked to Form1_Paint

``` public partial class Form1 : Form
{
private System.Drawing.Drawing2D.GraphicsPath _gPath = new System.Drawing.Drawing2D.GraphicsPath();

public Form1()
{
InitializeComponent();
}

public List<PointF> ArchimedischeSpirale(float cx, float cy, double growth, double rotations, int StartAngle)
{
List<PointF> fList = new List<PointF>();

int z = 1;

for (float i = 1F; i < (float)(rotations * 360); i += 0.01F)
//use this when calculating counter by arclength
//for (float i = 1F; i < (float)(rotations * 360); )
{
double rf = 0.0;

//Überlauffehler abfangen
try
{
rf = growth * i * Math.PI / 180;
}
catch
{
i += 0.1F;
continue;
}

PointF pt = new PointF((float)(cx + (Math.Cos((i + StartAngle) * Math.PI / 180) * rf)),
(float)(cy + (Math.Sin((i + StartAngle) * Math.PI / 180) * rf)));

//Control.MaxSize
if (pt.X + cx > 32767F || pt.X - cx < -32767F)
{
i += 0.1F;
continue;
}

if (pt.Y + cy > 32767F || pt.Y - cy < -32767F)
{
i += 0.1F;
continue;
}

//Commented out other way of calculating loopCounter by ArcLength
//double u = 0.0;
//float ii = i;

//while (u < z)
//{
// ii += 0.001F;
// //Umfang
// u = growth / 2 * (Math.Log(Math.Sqrt(Math.Pow(ii * Math.PI / 180, 2) + 1) + ii * Math.PI / 180) + ii * Math.PI / 180 * (Math.Sqrt(Math.Pow(ii * Math.PI / 180, 2) + 1)));
//}

//z++;

//i += (ii - i);

}

fList.TrimExcess();

return fList;
}

private void Form1_Load(object sender, EventArgs e)
{
CalcSpiral();
}

private void CalcSpiral()
{
if (_gPath != null)
_gPath.Dispose();
_gPath = new System.Drawing.Drawing2D.GraphicsPath();

List<PointF> Spiral = ArchimedischeSpirale(this.ClientSize.Width / 2F, this.ClientSize.Height / 2F,
12, 4.55, 0);

this.Invalidate();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawPath(Pens.Blue, _gPath);
}

private void Form1_ResizeEnd(object sender, EventArgs e)
{
CalcSpiral();
}
}
```

Regards,

Thorsten

• Proposed as answer by Thursday, November 11, 2010 8:46 AM
• Marked as answer by Monday, November 15, 2010 2:52 AM
Sunday, November 7, 2010 7:07 AM
• • Proposed as answer by Sunday, November 7, 2010 2:03 PM
• Marked as answer by Monday, November 15, 2010 2:52 AM
Sunday, November 7, 2010 7:07 AM
• Hm, I read your post again, and I'm not sure anymore, if you wanted the spiral...

So, here's an ellipse and a circle: The Form's Paint-Event should be hooked to Form1_Paint

``` public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//ellipse
e.Graphics.DrawEllipse(Pens.Blue, new Rectangle(new Point(0, 0), this.ClientSize));
//circle
int wh = Math.Min(this.ClientSize.Width, this.ClientSize.Height);
e.Graphics.DrawEllipse(Pens.Red, new RectangleF((this.ClientSize.Width - wh) / 2F,
(this.ClientSize.Height - wh) / 2F, wh, wh));
}
}
```

Regards,

Thorsten

• Proposed as answer by Thursday, November 11, 2010 8:46 AM
• Marked as answer by Monday, November 15, 2010 2:52 AM
Sunday, November 7, 2010 8:11 AM

### All replies

• Do you mean drawing a (archimedic/archimedian) spiral?

http://en.wikipedia.org/wiki/Spiral

Regards,

Thorsten

Sunday, November 7, 2010 6:27 AM
• Ok, here's a quick takeout of a testprogram, so the code might be a bit weird (I think, it was better to create a class "Spiral" that holds the data) The Form's Paint-Event should be hooked to Form1_Paint

``` public partial class Form1 : Form
{
private System.Drawing.Drawing2D.GraphicsPath _gPath = new System.Drawing.Drawing2D.GraphicsPath();

public Form1()
{
InitializeComponent();
}

public List<PointF> ArchimedischeSpirale(float cx, float cy, double growth, double rotations, int StartAngle)
{
List<PointF> fList = new List<PointF>();

int z = 1;

for (float i = 1F; i < (float)(rotations * 360); i += 0.01F)
//use this when calculating counter by arclength
//for (float i = 1F; i < (float)(rotations * 360); )
{
double rf = 0.0;

//Überlauffehler abfangen
try
{
rf = growth * i * Math.PI / 180;
}
catch
{
i += 0.1F;
continue;
}

PointF pt = new PointF((float)(cx + (Math.Cos((i + StartAngle) * Math.PI / 180) * rf)),
(float)(cy + (Math.Sin((i + StartAngle) * Math.PI / 180) * rf)));

//Control.MaxSize
if (pt.X + cx > 32767F || pt.X - cx < -32767F)
{
i += 0.1F;
continue;
}

if (pt.Y + cy > 32767F || pt.Y - cy < -32767F)
{
i += 0.1F;
continue;
}

//Commented out other way of calculating loopCounter by ArcLength
//double u = 0.0;
//float ii = i;

//while (u < z)
//{
// ii += 0.001F;
// //Umfang
// u = growth / 2 * (Math.Log(Math.Sqrt(Math.Pow(ii * Math.PI / 180, 2) + 1) + ii * Math.PI / 180) + ii * Math.PI / 180 * (Math.Sqrt(Math.Pow(ii * Math.PI / 180, 2) + 1)));
//}

//z++;

//i += (ii - i);

}

fList.TrimExcess();

return fList;
}

private void Form1_Load(object sender, EventArgs e)
{
CalcSpiral();
}

private void CalcSpiral()
{
if (_gPath != null)
_gPath.Dispose();
_gPath = new System.Drawing.Drawing2D.GraphicsPath();

List<PointF> Spiral = ArchimedischeSpirale(this.ClientSize.Width / 2F, this.ClientSize.Height / 2F,
12, 4.55, 0);

this.Invalidate();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawPath(Pens.Blue, _gPath);
}

private void Form1_ResizeEnd(object sender, EventArgs e)
{
CalcSpiral();
}
}
```

Regards,

Thorsten

• Proposed as answer by Thursday, November 11, 2010 8:46 AM
• Marked as answer by Monday, November 15, 2010 2:52 AM
Sunday, November 7, 2010 7:07 AM
• • Proposed as answer by Sunday, November 7, 2010 2:03 PM
• Marked as answer by Monday, November 15, 2010 2:52 AM
Sunday, November 7, 2010 7:07 AM
• Hm, I read your post again, and I'm not sure anymore, if you wanted the spiral...

So, here's an ellipse and a circle: The Form's Paint-Event should be hooked to Form1_Paint

``` public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//ellipse
e.Graphics.DrawEllipse(Pens.Blue, new Rectangle(new Point(0, 0), this.ClientSize));
//circle
int wh = Math.Min(this.ClientSize.Width, this.ClientSize.Height);
e.Graphics.DrawEllipse(Pens.Red, new RectangleF((this.ClientSize.Width - wh) / 2F,
(this.ClientSize.Height - wh) / 2F, wh, wh));
}
}
```

Regards,

Thorsten

• Proposed as answer by Thursday, November 11, 2010 8:46 AM
• Marked as answer by Monday, November 15, 2010 2:52 AM
Sunday, November 7, 2010 8:11 AM
• I will check out your code and see if that work. I think it is pretty much like you say a spiral. That means

a circle starts from the outside of the picturebox and than gradually shrinks itself until it is about to disappears. Then it will gradually expands until it reaches or touches the edge of the picturebox, and then starts to shrink again.

The process is infinite. But we gotta use thread to do that or better, use threads from the threadpool. The user is allowed to click a button to create as many circles as he wants until the threads from the threadpool have been used up.

Wednesday, November 10, 2010 2:45 PM