Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Plotting points around circle  RSS feed

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a clock face and i want to be able to specify the hour and minute and the hands will move to the correct location.
I found the equation for finding the points around a circle r*cos(a) where "r" will be the length of the clock hand and a will be the angle.
I came up with this formula for the minute hand. I originally set the minute to 15, and this formula worked fine for that time. When i try a different time though the hand stays in the same position and the output for both calculations remained almost identical.
I did originally have PI/60 instead of PI%60 but that provided completely the wrong time.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were closer the first time - you definitely need to divide by 60 (% doesn't do what you want). But I think the problem is you're using the angle to the horizontal. If minute is zero then sin(...) = 0, and your hand is pointing to 3. Try swapping sin and cos.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, works perfectly now with sin and cos swapped around! Thank you
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry another question. For the hour hand i would have thought that formula could be modified to the same as below.
The hour hand is working in the same way, just moving 30degrees around the face for every hour. Apparently not, i can get it to appear right for a few times if i put 200+ instead of - for the y co ordinate as well
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone help with working out the hour hand please?
I am still a bit stuck on this
Thank you
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't you still got sin/cos the wrong way round in that example?

I'd take the formula that worked before, then divide the angle that you used before by 12. So you replace the 60 in the initial formula with 720.

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I swapped them round to see if it made any difference to the result.
Could you explain that last bit again please. What do you mean by "divide the angle that you used before by 12"
I thought dividing by 12 like in the last code block i did would be correct.
Also the last sentence, are you saying the only change to make is replace the 60 with 720?
Sorry for all the questions, the coding is fine but the maths is causing me problems at the moment.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your minute hand, you were using 2*Math.PI*minute/60 as the angle (in radians).

If you use 2*Math.PI*minute/12 for the hour hand, that's 5 times the angle of the minute hand, which means the hour hand is moving faster than the minute hand. You want it to move twelve times slower. So you need 2*Math.PI*minute/(60*12).

Another way of looking at it would be to use 2*Math.PI*hour/12. That's fine, but you need to translate from minutes to hours.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that makes sense now.
I have this, but it needs very high numbers to move the hand anywhere. For example, putting 900 for the minutes moves it to the 3 position, any normal number between 0-60 means it barely moves from the 12 o clock.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? If I stick minute = 60 into those formulas in a calculator I get roughly what I'd expect.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Really? If I stick minute = 60 into those formulas in a calculator I get roughly what I'd expect.


Yep, If i use the formulas above, enter 60 for the minutes and use the line below to draw the line, the hour hand points roughly towards where 1-2 o clock should be.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!