Results 1 to 8 of 8
Thread: Doing something like compas
 04262010, 05:21 AM #1Member
 Join Date
 Apr 2010
 Posts
 4
 Rep Power
 0
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. :(

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!
 04262010, 05:32 AM #3Member
 Join Date
 Apr 2010
 Posts
 4
 Rep Power
 0
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.

You're quite welcome!
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.
 04262010, 06:00 AM #5Member
 Join Date
 Apr 2010
 Posts
 4
 Rep Power
 0
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.

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!
 04262010, 06:26 AM #7Member
 Join Date
 Apr 2010
 Posts
 4
 Rep Power
 0
Is the formula i use is the correct formula to draw 67 degrees West of North? Or I need to figure out the formula?

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 counterclockwise and start at the 12 o'clock position, tou're going to have to translate your degrees a little. Something like so:
Java Code:double angleInRadians = (270  angleInDegrees) * Math.PI / 180.0;
Bookmarks