Reputation: 43
I need to write a program using java for an assignment. The program needs to output a circle of a given radius at a given coordinate. So far I've created a for loop nested inside of another for loop, to scan all coordinates and print a "#" whenever the coordinate satisfies the equation of a circle, which is: (x−a)^2 +(y−b)^2 =r^2
However, my circle comes out missing certain coordinates.
I believe it is because I didn't take into account one of the hints included in the question.
The hint is: The discrete world we have to work with makes it hard to express the strict equality in equation 1 (equation 1 being the equation of a circle). Can you relax that equality in order to draw better circles?
I would greatly appreciate if someone would give me any ideas as to how to "relax" the equation of a circle to output a fuller circle.
Thanks!
Upvotes: 4
Views: 796
Reputation: 43
This is what happens when I cast the coordinates to int, and also add 0.5, it looks a lot better, but I'm not sure why the left side is left out.
It doesn't need to be so nice, the "goal" is this:
Upvotes: 0
Reputation: 425228
The problem is that integer arithmetic's rounding is impacting your results.
Integer rounding isn't really rounding, it's truncating (all decimal parts are lost ie (int).9 = 0), so you could first try adding .5 to each result (or -.5 if the number is negative) before letting the truncating occur.
If that doesn't help, try drawing multiple circles on the same grid by repeating the process but with r +/- .5, to "fill in" the jagged gaps.
If that's not enough, I would "draw" a circle on a virtual grid at a larger scale (maybe 4x) and with a thicker line (draw 3 circles of r +/- 1), then shrink it to fit the real grid.
Upvotes: 1