Results 1 to 15 of 15
  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Finding points around a circle

    The problem is to create a clock face which displays the correct time when a user enters a time, say 2:45.
    Im having problems, firstly remembering my trig and secondly putting it into practice.
    For finding the co ordinates around a circle i use r * cos(a) for x and sin*cos(a) for y. With r being the radius and a being the angle.
    When putting this into practice, r is obviously the length of my clock hand but what to use for the angle is where i am having issues.
    For finding the angle to use for the x and y minute hand i have found these.
    x = PI/2 - PI/30*m
    y = PI/2 - PI/2*m

    With m being the minute. I have tried these out (not using java) to see if they work but the answers i am getting don't seem right.
    Could someone explain the maths behind it for me?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Finding points around a circle

    Java works with radians, i.e. 360 degrees == 2*PI radians. So one minute is 2*PI/60 radians. (PI is defined in the Math class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Finding points around a circle

    Quote Originally Posted by JosAH View Post
    Java works with radians, i.e. 360 degrees == 2*PI radians. So one minute is 2*PI/60 radians. (PI is defined in the Math class).

    kind regards,

    Jos
    I think i have it now, so for my minute hand my formula will be below. Assuming the time is 15minutes past. I did the calculations and they fit approximately where i think they should. (200,200) is my centre point hence the 200 + and -.

    x =200 + 180* cos(2*PI/60*15)
    y =200 - 180* sin(2*PI/60*15)

  4. #4
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Finding points around a circle

    I have tried to implement this but for some reason the hand no longer displays on my frame.
    I create the hand like this, this is the only section that has changed.

    Java Code:
    double minuteXCor =200+(80*Math.cos(2*Math.PI/60*minute));
    double minuteYCor =200-(80*Math.sin(2*Math.PI/60*minute));
    
    Line2D.Double clockMinuteHand = new Line2D.Double(200,200,minuteXCor,minuteYCor);
            g2.draw(clockMinuteHand);
    The clock is created here

    Java Code:
    import java.awt.Insets;
    import javax.swing.*;
    public class ClockViewer 
    {
        public static void main(String[] args)
        {
            JFrame frame = new JFrame();
            frame.pack();
            Insets insets = frame.getInsets();
            int addedWidth = insets.left + insets.right;
            int addedHeight = insets.top + insets.bottom;
            
            final int HEIGHT = 400 + addedHeight;
            final int WIDTH = 400 + addedWidth;
        
            frame.setSize(WIDTH, HEIGHT);
            frame.setTitle("Clock");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            Clock newClock = new Clock(hour,min);
            frame.add(newClock);
            frame.setVisible(true);
        }
        
    }

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Finding points around a circle

    The snippet you've shown us doesn't show the problem. If it worked before and now it doesn't, why not go back to your previous implementation and step by step change the code to what you want it to be?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Finding points around a circle

    I have no idea why it wasn't working, i replaced it with the original numbers and then put minuteXCor and minuteYCor back in and it displayed correctly then. The problem now is the hand isnt displayed correctly. With 15 set for the minutes that hand faces towards about 50 or ten to the hour. I printed the x and y co ordinates that i had calculated and they came out as 200, 100. Not really sure what i am doing wrong

  7. #7
    Lowest0ne is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: Finding points around a circle

    Are you putting the hand at pixel (200,100) or the pixel that is offset from the center of the clock by (200,100)?

  8. #8
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Finding points around a circle

    Quote Originally Posted by Lowest0ne View Post
    Are you putting the hand at pixel (200,100) or the pixel that is offset from the center of the clock by (200,100)?
    The hand is centred at 200,200. The x and y co ordinate that i calculated came out with co ordinates 200, 100. Those are the numbers i get when i print out the double values onto my clock. The offset pixel is not at the point 200,100 though as i have my clock centred to 200,200 and the hand is pointing towards the top left. I can add a screenshot if that helps you understand what i mean.

  9. #9
    Lowest0ne is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: Finding points around a circle

    If the center of your circle is at (200,200) and the radius is, say, 100, then the 15 minute should have the hand at 300,200. I guess there are a lot of possible math problems going on.

  10. #10
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Finding points around a circle

    I used this formula and it came back with the correct time for 15 minutes past.

    Java Code:
    double minuteXCor =200+(100*Math.cos(2*Math.PI%60*minute));
    double minuteYCor =200-(100*Math.sin(2*Math.PI%60*minute));
    When i change the minute to something different the result is almost identical and the hand doesn't move. Is there something wrong with the original formula?

  11. #11
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Finding points around a circle

    I found out how to fix the minute hand by swapping the sin and cos around using the original formula.
    My problem is now the hour hand, i thought it would simply be the same but PI/12 instead of PI/60 . . . .apparently it isnt that simple.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Finding points around a circle

    Think of time t measured in minutes; the minute hand is at position 2*PI*t/60 after t minutes and the hour hand is at position 2*PI*t/60/12.

    kins regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Finding points around a circle

    Thanks for the reply again Jos.
    I have tried many variations of the formula you gave but have had no luck.
    Java Code:
            double hourXCor = 200+(80*Math.cos(2*Math.PI/60/12*hour));
            double hourYCor = 200+(80*Math.sin(2*Math.PI/60/12*hour));
    I have tried replacing hour (or t in your example) with the time in hours or in minute equivalent but the result still doesn't match with what i expect. I have also tried switching the cos and sin around but again that doesnt seem to make much difference.

  14. #14
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Finding points around a circle

    I now have this but it is not giving me the expected result

    Java Code:
    double hourXCor = 200+(80*Math.sin(2*Math.PI*minute/(60*12)));  
    double hourYCor = 200-(80*Math.cos(2*Math.PI*minute/(60*12)));

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Finding points around a circle

    Those formulas are fine; the time is measured in minutes, so e.g. three o'clock == 180 minutes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 08-11-2011, 11:46 AM
  2. Calculating points in a circle
    By Masochist in forum New To Java
    Replies: 5
    Last Post: 02-14-2011, 07:36 PM
  3. Finding the closest point in a list of points
    By sAntA199 in forum New To Java
    Replies: 8
    Last Post: 12-13-2009, 09:41 AM
  4. Replies: 0
    Last Post: 11-25-2008, 10:24 AM
  5. Replies: 2
    Last Post: 08-24-2008, 01:30 AM

Posting Permissions

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