0

I need to draw a circle with a specific range. In my case I need to start from the top and then go down to the bottom of the circle, going over the left outer border of the circle. I need to know the length of each line and the X and Y coordinates. I basically see a circle as a bunch of lines stacked on top of each other where as it goes further down the horizontal line length grows and grows until it reaches the middle point, then it goes all the way back and shrinks and shrinks. Then you have a circle. I need to iterate over each of those lines, knowing their X and Y coordinates from the left side so that I can then do line_to_the_left(x, y, length) to draw the circle.

enter image description here

How would an algorithm taking a range look like that does this? I know that one thing I would need is Pi.

치큰0
  • 43
  • 2
  • 3
  • If your iteration variable is the y (height) coordinate, you can get the corresponding x coordinate via the relation (x − Cx)² + (y − Cy)² = R², where (Cx, Cy) is the circle's centre and R its radius. – M Oehm Apr 05 '21 at 17:49
  • You might want to take a look at the Midpoint Circle Algorithm (https://en.wikipedia.org/wiki/Midpoint_circle_algorithm) to see if that will get you what you need. It's not exactly what you describe, but it may be something you can easily adapt to meet your need. – andand Apr 05 '21 at 20:51
  • there are also better algorithms to render filled circle for example see [Is there a more efficient way of texturing a circle?](https://stackoverflow.com/a/61097673/2521214) which does use only basic operations in the main iterations... – Spektre Apr 06 '21 at 07:51

1 Answers1

0

You can make loop over integer Y values. For center coordinates cx, cy and radius R:

for y = - R ... R:
    hw = (int) sqrt(R^2 - y^2)  //halfwidth
    line(cy + y, cx - hw,  cy + y, cx + hw)  //left and right ends of line
MBo
  • 77,366
  • 5
  • 53
  • 86
  • For my use case I need it to be in a `line_to_the_left(x, y, length)` fashion rather than `line(x1, y1, x2, y2)`. – 치큰0 Apr 06 '21 at 16:14
  • 1
    I do believe that you can calculate line length here having `hw` – MBo Apr 06 '21 at 16:20