• Draw arc from point?
    6 replies, posted
This is more a maths question, but I'm sure many people here can answer it. I wish to draw an arc. That is, mark a series of points equidistant from a set point going from 0 degrees to 180 degress (so really, half a circle). How would I do this using screen points?
(x0, y0) is the center of the circle, r is its radius (x - x0)^2 + (y - y0)^2 = r^2 Solving for y, since you probably want the half-circle to be horizontal (0-180 is generally from right to left) y^2 - 2*y*y0 + y0^2- r^2 + (x - x0)^2 = 0 y = ( -(-2*y0) ± sqrt( (-2*y0)^2 - 4*(y0^2 - r^2 + (x - x0)^2) ) )/2 Give x any values between x0-r and x0+r to get corresponding values on the y-axis. Choose either + or - for the ± part. Draw lines between those points or something, I don't know about that part.
Another method would involve trigonometry. y = r * cos( t * Pi / 180); x = r * sin ( t * Pi/ 180); Where t is your degree point. In this setup (t=0) is the very top of the circle, (t++) will move clockwise. To draw your half-circle you could do: [code] for(int t = 0; t < 180; t++) { y = r * cos( t * Pi / 180); x = r * sin ( t * Pi/ 180); } [/code] If you wanted to draw an arc going clockwise from an arbitary point: [code] // a is your arbitary point for(int t = a; t < (a+180); t++) { y = r * cos( t * Pi / 180); x = r * sin ( t * Pi/ 180); } [/code] In these examples the amount you add to t every step is dependant. If you are using a large radius then I would make t a float and decrease the step t makes (maybe there is a relation to it and the radius).
[QUOTE=Mattz333;20154890]Another method would involve trigonometry. y = r * cos( t * Pi / 180); x = r * sin ( t * Pi/ 180); Where t is your degree point. In this setup (t=0) is the very top of the circle, (t++) will move clockwise. To draw your half-circle you could do: [code] for(int t = 0; t < 180; t++) { y = r * cos( t * Pi / 180); x = r * sin ( t * Pi/ 180); } [/code] If you wanted to draw an arc going clockwise from an arbitary point: [code] // a is your arbitary point for(int t = a; t < (a+180); t++) { y = r * cos( t * Pi / 180); x = r * sin ( t * Pi/ 180); } [/code] In these examples the amount you add to t every step is dependant. If you are using a large radius then I would make t a float and decrease the step t makes (maybe there is a relation to it and the radius).[/QUOTE] Also notice that these will indeed be clockwise even though they might appear counter-clockwise on your screen. That's because the y-axis in graphics is usually mirrored.
[QUOTE=ThePuska;20156112]Also notice that these will indeed be clockwise even though they might appear counter-clockwise on your screen. That's because the y-axis in graphics is usually mirrored.[/QUOTE] Ah yes, I was thinking of the standard Cartesian system when I wrote this out. To correct the code you just need to negate the y-coordinate calculation.
Using Mattz code (yes i'm lazy, and chose it because I understand it better), I've been able to get the arc. Changing the code to [code]for(int t = 90; t < 270; t++)[/code] Should make it an arc going from/to two horizontal points?
[QUOTE=FerrisWheel;20160294]Using Mattz code (yes i'm lazy, and chose it because I understand it better), I've been able to get the arc. Changing the code to [code]for(int t = 90; t < 270; t++)[/code] Should make it an arc going from/to two horizontal points?[/QUOTE] Yes, you can do that, or simply swap the sin and cos.
Sorry, you need to Log In to post a reply to this thread.