# draw circle problem

5 messages
Open this post in threaded view
|
Report Content as Inappropriate

## draw circle problem

 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;     }
Open this post in threaded view
|
Report Content as Inappropriate

## Re: draw circle problem

 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).
Open this post in threaded view
|
Report Content as Inappropriate

## Re: draw circle problem

 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.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: draw circle problem

 I checked and your code seems to work just fine. Perhaps you haven't implemented correctly drawHorizontal?