coure2011
coure2011

Reputation: 42404

mathematics for drawing little lines around circle

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

Upvotes: 6

Views: 2381

Answers (3)

El Ronnoco
El Ronnoco

Reputation: 11922

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=π 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)
};

Upvotes: 10

Aaron Digulla
Aaron Digulla

Reputation: 328556

Have a look at the source code of CoolClock.

Upvotes: 2

Bablo
Bablo

Reputation: 916

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

Upvotes: 1

Related Questions