draw circle problem

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

draw circle problem

autoria
what's wrong with my drawCircle function, it looks like i'm drawing vertical line.


my code:
function void drawCircle(int cx, int cy, int r) {
        var int dx, dy;
        var int r_squared;
        if ((cx>511)|(cy>255)|(r>181)) {  
            do Sys.error(12);  
        }  
        let dy = -r;
        let r_squared = r*r;
        while( ~(dy > r) ) {
            let dx = Math.sqrt(r_squared-(dy*dy));
            do Screen.drawHorizontal( cx-dx, cx+dx, cy+dy );
            let dy = dy + 1;
        }
        return;
    }
Reply | Threaded
Open this post in threaded view
|

Re: draw circle problem

ivant
Here's what the book says about sqrt: "function int sqrt (int x): returns the integer part of the square root of x."

You may want to use the midpoint circle algorithm instead (specifically, the variant with integer-based arithmetic).
Reply | Threaded
Open this post in threaded view
|

Re: draw circle problem

ivant
Please, disregard my previous message. I forgot that this what you're implementing is the algorithm from the book. I'll try to find the problem and will write when I do.
Reply | Threaded
Open this post in threaded view
|

Re: draw circle problem

ivant
I checked and your code seems to work just fine. Perhaps you haven't implemented correctly drawHorizontal?
Reply | Threaded
Open this post in threaded view
|

Re: draw circle problem

cadet1620
Administrator
Math.sqrt() always returning 0 would also cause this vertical line behavior.

--Mark