# Doing something like compas

• 04-26-2010, 05:21 AM
DingDang
Doing something like compas
Hi!

I'm trying to draw a direction using the following code:

gc.drawOval(10,10,50,50);
gc.drawLine(10 + 25, 10+25, 10+25, 10);

It creates and oval with a straight line pointing to the top, assuming that is North.

Now, from my calculation, I have a value of 67, translated into 67 degrees west of North. What is the formula should I use for gc.drawline so that it will show the direction correctly on the oval?

Seriously, I'm not a mathematician and I don't have any clue on how to do it. :(
• 04-26-2010, 05:29 AM
Fubarable
You'll want to use a little trigonometry and the Math library's static trig methods, Math.sin(...) and Math.cos(...). If I remember correctly (the API will tell us for sure), these act on radians not degrees (edit: looked it up and yep, it's radians), but that's OK because Math also has a toRadians(...) method.

Much luck and welcome to the forum!
• 04-26-2010, 05:32 AM
DingDang
Thanks for the warm welcome, sir! :)

Yes, no doubt it will involve sin, cos, tan and such. Just a simple example will take me to the right direction.
• 04-26-2010, 05:36 AM
Fubarable
Quote:

Originally Posted by DingDang
Thanks for the warm welcome, sir! :)

You're quite welcome!

Quote:

Yes, no doubt it will involve sin, cos, tan and such. Just a simple example will take me to the right direction.

Again, best of luck.
• 04-26-2010, 06:00 AM
DingDang
My code for drawing the direction looks something like this

g.drawLine(10 + 25, 10+25, (int)(35+ (25*Math.cos(2*3.1415/180*67))),(int)( 35 + (25*Math.sin(2*3.1415/180*67))) );

I just grab the code blindly. It doesn't show the right angle. :(

If I can understand, at least, the formula to draw the line based on radius, that would be great.
• 04-26-2010, 06:16 AM
Fubarable
Don't try to write it out all on one line as it gets difficult to maintain and debug. You're much better off breaking this down into steps. I'd do:

Assign values of the center of your circle, I'd call the variables centerX and centerY and would use doubles here.

Assign the value of the of the radius: double radius = 25.0;

Draw on paper your points, angles, etc...

One of the line endpoints will be the int values of centerX and centerY.

The other endpoints will use
x = int value of (radius * cos(angle in radians) + centerX)
y = int value of (radius * sin(angle in radians) + centerY).

You'll need to try out some code, change it if it doesn't work, experiment and see what happens.

Much luck!
• 04-26-2010, 06:26 AM
DingDang
Is the formula i use is the correct formula to draw 67 degrees West of North? Or I need to figure out the formula?
• 04-26-2010, 07:01 AM
Fubarable
No. Because Java's graphics sets zero degrees at the 3 o'clock position and has positive number degrees going clockwise, if you want to go counter-clockwise and start at the 12 o'clock position, tou're going to have to translate your degrees a little. Something like so:

Code:

`double angleInRadians = (270 - angleInDegrees) * Math.PI / 180.0;`