# Finding points around a circle

• 10-11-2012, 03:55 PM
wdh321
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?
• 10-11-2012, 05:10 PM
JosAH
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
• 10-11-2012, 05:36 PM
wdh321
Re: Finding points around a circle
Quote:

Originally Posted by JosAH
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)
• 10-12-2012, 03:01 PM
wdh321
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.

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

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);     }     }```
• 10-12-2012, 05:27 PM
JosAH
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
• 10-13-2012, 05:08 PM
wdh321
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
• 10-13-2012, 05:16 PM
Lowest0ne
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)?
• 10-13-2012, 05:23 PM
wdh321
Re: Finding points around a circle
Quote:

Originally Posted by Lowest0ne
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.
• 10-13-2012, 06:09 PM
Lowest0ne
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-16-2012, 04:16 PM
wdh321
Re: Finding points around a circle
I used this formula and it came back with the correct time for 15 minutes past.

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?
• 10-16-2012, 09:36 PM
wdh321
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.
• 10-17-2012, 08:58 AM
JosAH
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
• 10-17-2012, 03:52 PM
wdh321
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.
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.
• 10-18-2012, 09:22 PM
wdh321
Re: Finding points around a circle
I now have this but it is not giving me the expected result

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)));```
• 10-19-2012, 07:34 AM
JosAH
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