I have to draw lines around a circle (like in clock). How can i achieve this using a for loop?
+2
A:
I'm not sure how to do the actual drawing of a line in Java but to calculate co-ordinates from a central point (cx,cy)
use
px = cx+sin(a)*r
py = cy+cos(a)*r
Where a
is the angle (in radians - I think ie 180 degress=Pi radians) and r
is the radius.
To draw the little lines around the outside you would need to use this formula with say a radius of 100 and the also with a radius of 105 and draw between the two sets of co-ordinates.
eg
for (var a=0,aMax=(2*Math.PI),aStep=(Math.PI/30); a<aMax; a+=aStep){
px1 = cx+Math.sin(a)*r;
py1 = cy+Math.cos(a)*r;
px2 = cx+Math.sin(a)*(r+5);
py2 = cy+Math.cos(a)*(r+5);
//draw line between (px1,py1) and (px2,py2)
};
El Ronnoco
2010-09-28 08:11:18
he was asking about javascript, `java.lang.Math` is a java class, and isn't available in javascript
Sander Rijken
2010-09-28 08:12:39
It's just `Math` in JavaScript.
poke
2010-09-28 08:14:20
Sorry, I read Java. Skim reading! Thanks poke :)
El Ronnoco
2010-09-28 08:15:45
Here is an example with Canvas: http://jsbin.com/usomi4
poke
2010-09-28 08:36:08
@poke I think that's the OP's answer right there!
El Ronnoco
2010-09-28 08:57:37
I don't think browsing through a complex code (it supports skins - wtf) is worth it when all OP is looking for is a simple calculation..
poke
2010-09-28 08:42:42
The skins should contain the code to draw the lines. This code should be pretty simple. Moreover, looking at code from other people is always a good learning experience.
Aaron Digulla
2010-09-28 09:07:25
@poke Agreeed, the OP wants to "draw lines around a circle" - not implement a fullblown clock. The goal may be totally un-clock related but just clock *like*
El Ronnoco
2010-09-28 09:08:04
@Aaron Digulla: But if you have no idea how those lines are drawn you will have a very hard time understanding the code and finding the actual bit you are interested in..
poke
2010-09-28 09:12:13