Results 1 to 8 of 8
  1. #1
    DingDang is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Cool 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. :(

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    DingDang is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by DingDang View Post
    Thanks for the warm welcome, sir! :)
    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.
    Let's see what you can do first. Writing code is mainly your responsibility, but we'll gladly help you with your code if you should post it.

    Again, best of luck.

  5. #5
    DingDang is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  7. #7
    DingDang is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Is the formula i use is the correct formula to draw 67 degrees West of North? Or I need to figure out the formula?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    Java Code:
    double angleInRadians = (270 - angleInDegrees) * Math.PI / 180.0;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •