On Wed, 20 Jun 2007 12:58:10 -0700, Christopher Ireland
<(E-Mail Removed)> wrote:
> Using the definition for en ellipse
> (http://en.wikipedia.org/wiki/Ellipse) I
> can draw an arc of points. However, the end points of this arc do not
> coincide with the end points of an arc drawn with the DrawArc method,
I'm not really clear on what your question is. You aren't drawing lines
that would form an arc. You are drawing vertical bars 2 pixels high at
various points around the ellipse, and that's exactly the output you get..
Here's a version of your code (minus the constant Form-derived class
implementation stuff) that corrects that along with some other problems
(you were failing to dispose of newly created pens correctly, for
example). It draws practically the same exact pixels as the DrawArc()
method, with only very minor variations that one would naturally expect
from two completely different implementations of drawing an ellipse
(especially when one implementation is actually just drawing straight line
segments between one degree intervals on the ellipse).
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Draw(ClientRectangle, e.Graphics);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Invalidate();
}
private void RectCenter(Rectangle r, out int x, out int y)
{
x = (r.Left + r.Right) / 2;
y = (r.Top + r.Bottom) / 2;
}
private PointF PointFromEllipse(Rectangle bounds, float degrees)
{
float a = bounds.Width / 2.0f;
float b = bounds.Height / 2.0f;
float rad = ((float)Math.PI / 180.0f) * degrees;
int xCenter, yCenter;
RectCenter(bounds, out xCenter, out yCenter);
float x = xCenter + (a * (float)Math.Cos(rad));
float y = yCenter + (b * (float)Math.Sin(rad));
return new PointF(x, y);
}
private void Draw(Rectangle rect, Graphics g)
{
rect.Inflate(-5, -5);
g.DrawArc(Pens.Green, rect, 0, 315);
PointF p, pPrev = PointFromEllipse(rect, 0);
for (int i = 1; i < 315; i++)
{
p = PointFromEllipse(rect, i);
g.DrawLine(Pens.Red, p, pPrev);
pPrev = p;
}
}