Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw ellipse using center point (not upper left corner)

I try to find a solution for drawing ellipses based on the center point, not the upper left corner as it is specified in the constructor of Ellipse2D.Double. As seen in the picture the ellipses should have the same center point and scale, is that somehow possible?

enter image description here

Thanks in advance for your help.

like image 897
nyyrikki Avatar asked Dec 01 '25 09:12

nyyrikki


1 Answers

If (x,y) is the center you want to use and you can only specify the upper left corner, then use the following:

private Ellipse2D getEllipseFromCenter(double x, double y, double width, double height)
{
    double newX = x - width / 2.0;
    double newY = y - height / 2.0;

    Ellipse2D ellipse = new Ellipse2D.Double(newX, newY, width, height);

    return ellipse;
}

If called with the center point and the width and height, this will "transform" your center point to the upper left corner and create an Ellipse2D which is located just as you want it to be.

like image 136
Baz Avatar answered Dec 04 '25 01:12

Baz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!