I want to calculate the time when I give you an angle in the analog clock implementation.

Asked 5 months ago, Updated 5 months ago, 17 views

I would appreciate it if you could give me some advice.

The implementation below is Swift, but any language is fine.

It is implemented to determine the correct time from 12:00 to 11:00 if the time hand of the analog clock is given an angle (for example, if the angle is 0 degrees, the number 12 will be returned). I can't think of a pretty way to write it.What I think is the best thing right now is that I can give you time by using the dictionary like the one below to make the angle and time into a dictionary.

func AngleToTime(){
    var test = [0:12, 30:1, 60:2, 90:3, 12:4, 150:5, 180:6, 210:7, 240:8, 270:9, 300:10, 330:11]
    let test = 0
    print("Now\(test[testtest])!) It's time!")

Ideally, I would like to make it a function, and if I receive int, which is an angle with an argument, I will have time as a result of the calculation.It's an hour hand now, so I can do something with the above implementation, but I don't want to be in trouble when it comes to minute or second hands.

javascript php java swift3

2022-09-30 14:03

2 Answers

In the case of the hour hand, it is 30 degrees for an hour, so
/* However, 12 o'clock shows midnight.

before printing if you want to display 12:00 instead of midnight if hour == 0 {
You may want to add .

For long and short hands, 1 minute and 1 second is equivalent to 6 degrees, so
is fine.

2022-09-30 14:03

In the case of the hour hand, 1 degree is 2 minutes, so wouldn't it be okay to use Math.floor (angle x 2/60) (angle x 2% 60) minutes (special treatment only when 0)

For example, javascript looks like this

function func(v){
  varm = v*2;
  result.value=m?fill_zero(Math.floor(m/60), 2)+":"+fill_zero(m%60,2):"12:00";
function fill_zero(v,len){
  while (v. length <len)
    v = "0" + v;
  return v;
<input type="number" value="0" step="1" min="0" max="359" id="degre" name="degre" onchange="func(this.value)">
<input type="time" value="12:00" id="result" name="result" readonly>

2022-09-30 14:03

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.