Yaroslav
Yaroslav

Reputation: 4820

get coordinates of points that form circumference

I have an ellipse:

 Ellipse2D e2D  = new Ellipse2D.Float(startPoint.x, startPoint.y, x - startPoint.x, y - startPoint.y);

And what I need is to get coordinates of all points that form circumference.

ArrayList<Point> oneDraw = new ArrayList<>();
for (int i = startX; i < borderX; i++)
        for (int j = startY; j < borderY; j++)
            if (e2D.contains(new Point(i, j))) 
                oneDraw.add(new Point(i, j));

By doing so, I put all coordinates that are inside my circle to the list, but I don't need this.

Thank you for the answer and spent time.

Upvotes: 1

Views: 487

Answers (2)

Thomas
Thomas

Reputation: 88707

Your approach would add all pixels in the box which are inside the ellipse to the list, i.e. you'd get the area of the ellipse rather than its circumference. I'd say it would be better to look for the correct formula and solve it for discrete x/y pairs.

Or yet better apply one of the algorithms for drawing ellipses that can be found on the net, e.g. this one: http://www.mathopenref.com/coordcirclealgorithm.html
Then get the pixels that have been drawn (if still needed).

Edit: if you have a look at the source code of Ellipse2D you can either get an an idea of how to implement your own algorithm or you could just use getPathIterator() with a uniform transform and then "rasterize" the path elements into your list.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168815

  • Create an ellipse that is a little larger than the target ellipse.
  • Create an ellipse that is a little smaller than the target ellipse.
  • Subtract the 2nd ellipse from the first. This will form an elliptical ring.
  • Do the current 'contains' code with the elliptical ring.

Upvotes: 1

Related Questions