Hi,

I'm trying to draw circle and oval by myself without using ".addEllipse"
Can I draw it by pointer? which mean i point 4 point and then make it to become the shape of circle and oval.
I tried to find solution in the net, but i dont seems to get any solution.

Any Idea?

Dani AI

Generated

Useful follow-up to what and already discussed. The transform trick that suggested is a good fix, but note: GDI+ default origin is the top-left of the surface (not the midpoint). Two simple, complementary approaches let you draw circles/ovals yourself and keep every shape centered at (100,100) so switching shapes doesn't move them.

Store shapes by a canonical center-plus-radii model (cx, cy, rx, ry). To draw using the built-in DrawEllipse without AddEllipse, compute a bounding rectangle from that center:

float cx = 100f, cy = 100f;
float w = 80f, h = 50f;
var rect = new RectangleF(cx - w/2f, cy - h/2f, w, h);
g.DrawEllipse(pen, rect);

If you want to draw the curve yourself (no built-ins), use the parametric equation x = cx + acos(t), y = cy + bsin(t). Generate N points around 0..2π and call DrawLines or FillPolygon for a smooth result:

int steps = 64;
PointF[] pts = new PointF[steps+1];
for (int i = 0; i <= steps; i++) {
  double t = i * 2*Math.PI / steps;
  pts[i] = new PointF(cx + (float)Math.Cos(t)*a, cy + (float)Math.Sin(t)*b);
}
g.DrawLines(pen, pts);

Other tips: approximate a precise ellipse with four cubic Béziers (use kappa ≈ 0.55228475) when you need resolution-independent curves. When using transforms, remember they persist on the Graphics object—use Save/Restore or ResetTransform to avoid shifting subsequent draws. Enable SmoothingMode.HighQuality for anti-aliased edges, use floating-point coordinates for subpixel accuracy, and keep a single canonical representation (center + params) so switching between polygon, ellipse, or bezier versions keeps the visual position identical.

Recommended Answers

All 4 Replies

Do you want to implement the Circle, or Line Drawing Algorithm instead of using Graphics.DrawLine(), DrawEllipse () Method?

Or

You want to make application like paint ? rubberband elastic Drawing?

Actually, I created other shape such as trapezium, parallelogram, by using the addlines.

I created them by having a middle point of the shape start from (x,y) - (100,100)

but when i straight use the build in function to draw ellipse, i can't find any ways to set the mid point of the ellipse start from (100,100)

I do that is because when the user need to change from one shape to another, the position of it wont be changing because all of the shape started from 100,100

Any idea of drawing my own ellipse so that i can start from position 100,100 ??
Any help would be very appreciated

Hi, You can perform the Transformation on your Points(By just adding or subtracting (100, 100) to each points). GDI+ uses (0,0) is the Mid point. But it provides Matrix class to Transform the Coordinate System.

Example to Transform the Coordinate System

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Rectangle rect = new Rectangle (0,0,100,100);
    Matrix matrix = new Matrix();
    
    g.DrawEllipse(Pens.Blue, rect);
    matrix.Translate(100, 100);
    g.Transform = matrix;
    g.DrawEllipse(Pens.Green, rect);
}

Here first Circle will be drawn Blue color and (0,0) as the starting point
Second circle will use the Transformation and (100,100) as the starting Point

I think this may helpful to you

commented: Great !! +2

Thanks alot !!
it works out fine !!
Appreciate it !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.