views:

94

answers:

3

I have to draw lines around a circle (like in clock). How can i achieve this using a for loop? alt text

A: 

you should read up on basic trigonometry and focus on Quadrants to achieve that.

Bablo
+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
he was asking about javascript, `java.lang.Math` is a java class, and isn't available in javascript
Sander Rijken
It's just `Math` in JavaScript.
poke
Sorry, I read Java. Skim reading! Thanks poke :)
El Ronnoco
Here is an example with Canvas: http://jsbin.com/usomi4
poke
@poke I think that's the OP's answer right there!
El Ronnoco
+1  A: 

Have a look at the source code of CoolClock.

Aaron Digulla
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
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
@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
@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