Plot() a circle?
-
I'm switching from circle() to plot() to have more control over my drawing app, the pixels are just a big array of strings (runs surprisingly better). How do I plot a circle using like
function makeCircle(x,y,colorID) setPixel(x,y,colorID) setPixel(x - 1, y - 0, colorID) setPixel(x + 1, y - 0, colorID) return void
(this is just a example of how i'm trying to do it)
I'm also thinking of a system for brushes, so i'm kinda trying to set myself up to be able to do that later. Would be cool of the size was adjustable (maybe a for loop) but that's unnecessary
-
@heyomayo If you want to use plot() to draw circles you could do something like:
function plotCircle(x, y, r, n, colour) for t = 0 to n loop pos = {x, y} + r * sinCos(360 * t / n) plot(pos.x, pos.y, colour) repeat return void
...where n is the number of pixels drawn. It's a bit cumbersome since it would depend on
the resolution andthe size of the circle. A small circle of course needs less pixels and thus a smaller number for n. And r is the radius of the circle.In order to get a continuous circle no matter the size, one could use line() instead to connect the points, and n then would mean the number of vertices...
EDIT: I missed the part where you said you stored the pixels in an array, but it should be the same thing either way using either setPixel() or plot().
-
In reality, a better approach is probably Bresenham's Circle Algorithm:
https://en.wikipedia.org/wiki/Midpoint_circle_algorithmI also found this post, which seems to indicate that you can set n to 5.657 * r. I haven't tried that myself, but the maths seems to check out.
https://math.stackexchange.com/questions/167289/determining-the-minimum-number-of-pixels-on-the-boundary-of-a-circle-drawn-in-diThen the function would become:
function plotCircle(x, y, r, colour) n = round(5.657 * r) for t = 0 to n loop pos = {x, y} + r * sinCos(360 * t / n) plot(pos.x, pos.y, colour) repeat return void
-
@vinicity Thank you so much for the function, i'll try it out