Log In  

I'm trying some math things to learn pico8 and programming in general.

After a few days and realizing that the square root in lua (and in every language?) only gives positive numbers, I’ve come to this monster to draw a simple circle haha

function _init()
end

function _update60()
cls(1)
end

function _draw()
--coordinates
line(64,0,64,128,2)
line(0,64,128,64,2)
manual_circle()
circ(64,64,63)
end

function manual_circle()
for x=0,50 do
radius=50
local y=-sqrt((radius^2)-(x^2))
pset(x+64,y+64,11)
end
for x=-50,0 do
radius=50
local y=-sqrt((radius^2)-(x^2))
pset(x+64,y+64,11)
end
for x=0,50 do
radius=50
local y=-sqrt((radius^2)-(x^2))
pset(x+64,-y+64,11)
end
for x=-50,0 do
radius=50
local y=-sqrt((radius^2)-(x^2))
pset(x+64,-y+64,11)
end
end

And it looks like this:

as you can see the function doesn't print the pixels continuosly as the circ function, probably because of the square root giving decimals.

What is the correct way of drawing a circle “manually”???

Thank you!

P#61759 2019-02-11 13:57 ( Edited 2019-02-11 13:58)

:: wieczu

Search Google for: Bresenham circle drawing algorithm

https://en.wikipedia.org/wiki/Midpoint_circle_algorithm may be good as well.

P#61765 2019-02-11 14:20
:: qouka

Why don't you use just x=cos(a)r and
y=sin(a)
r, where a is the angle and
r the radius and you iterate over the
angle.

P#61779 2019-02-11 18:30
1

wieczu: wow didn't think this was such a complicated thing! Thank you very much, that's exactly what I was looking for!

qouka: you are right! I just wanted to see the most “manual” way of doing it.

P#61800 2019-02-12 10:14

wieczu answer (and the many variants) is the only appropriate option to draw a circle on a computer screen.
Sin/cos are ok but getting the right number of slices to not have holes would be super complex!
(beside sin/cos are/were notoriously slow)

P#61820 2019-02-12 20:27

Check out this thread on the Minsky circle: https://www.lexaloffle.com/bbs/?tid=29976

(the title is no longer entirely correct, but it's still an interesting and surprisingly performant alternative to Bresenham)

P#61829 2019-02-13 01:25

Fascinating! The native pico8 circle function should have been something really hard to do! Thanks! :D

P#61837 2019-02-13 09:32

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-08-17 18:26 | 0.013s | 2097k | Q:24