I want to know how to make a JavaScript function that returns the coordinates at a given minute . The central coordinates are the center of the clock, so they can be negative.
In Microsiervos I found a link , which parallels what I'm asking, it's a clock that gives you the time in coordinates .
I'm trying division or subtraction, but it doesn't work.
function(minutos)
{
var x=0,y=0
x=minutos*60
y=x-12
return [x,y]
}
edit:
Looking at some answers I have seen something strange: Why use cosine , if it is possible to extract in value using sine ?
According to this Game Development page and this Mathematics page , the coordinates of a point can be calculated by knowing
Assuming that the center of the clock has coordinates
[0,0]
, then the formula for the coordinates of the new point would be something like this:For the sine and cosine we can use the JavaScript
sin()
and functionscos()
, both of which take an angle in radians as a parameter. And that brings us to the second point: the angle in radians. We know that a circumference has 2π radians, and that there are 60 minutes in an hour, so for each minute, the angle will be something like this:For the value of π we can use the property
PI
. But it has a bit of a trick, as the direction is to the right, that means that we are in the first 15 minutes, so the angle will have to be adjusted a bit:And putting all that together, we would be left with something like this:
Here is an example (using seconds instead of minutes, but the idea is similar):
To be able to answer that question, imagine that the clock is a circle, to know the coordinates the sine function is necessary , and the sine function needs the number pi , and one way to calculate pi is through the arcsine function .
This is the article of the Number pi in Wikipedia , it puts the formula of how to calculate pi by means of the arcsine .
The formula of the sine function would be this.
Thinking a little, taking into account that a complete revolution of the circle has 360 degrees , and that an hour has 60 minutes , I was able to solve the problem, and also I did it this way because I mistrusted the values of the standard javascript functions , by not knowing its source code , it must be somewhere in the V8 engine (Wikipedia) , I wanted to be sure to give correct values.
The final code would be this.
Tested on Node.js:
EDIT
Detailed explanation of the heart of the matter:
var rad = 2 * Math.PI * ( min / 60 ) - ( ( 2 * Math.PI ) / 4 );
We convert the minutes into radians.
( min / 60 )
It results in the degrees equivalent to the indicated minutes. To convert it to radians (the mathematical functions involved,Math.sin
andMath.cos
, work with radians, not degrees), multiply by 2Pi.The functions
seno
andcoseno
are periodic functions , with results between -1 and 1, inclusive ( [-1, 1] ). That expression prepares the previous result ( the angle in radians that we are processing ) so that the outputs ofMath.sin()
andMath.cos()
are useful to us. Let's say it scales the radians so that the result ofMath.sin()
yMath.cos()
fits into our full circle space.EDIT 1 to fit the new version of the question.
Explanation of why this substitution is possible
As I said before,
seno( )
andcoseno( )
are periodic functions , that is, their values repeat over time. What we did not say is its period, how long it takes to repeat its values. In both cases, its period is PI / 2. Let's think about it carefully... same range of result values... same period... this suggests that both functions must be very similar, only varying the displacement on the axis X of the result.In this cool graph, taken from Wikipedia, everything can be seen very well. The X axis represents the angle in radians , while the Y axis represents the output of the function (the value it returns).
Source: Wikipedia, Trigonometry
If we look at the graph, we see everything clearly:
Next, since the graph has hardly any values, I show some, to check it:
EDIT 2
Why don't the values match? According to the paragraph above, they should match, right?
I recommend taking a look at Why can't my programs do arithmetic calculations correctly?
In this example table, in addition to the above, and as we will see in the code, part of the fault lies with the use of the function
Number.toFixed( )
, which discards decimals. In this specific case, nothing happens, but... if we have to scale the result (multiply it by some number), the inaccuracy can be large enough to produce unwanted artifacts , especially in graphical matters.END EDIT 2
The code in Node.js, very simple, in case someone wants to check it out:
It is appreciated that
cos( 90º ) == 0
, which is exactly the same assen( 0 ) == 0
; As said before, the cosine of an angle is the sine ofangulo - PI/2
; or, put another way, cosine var 45º behind .We already know why this substitution is possible.
Answer to the new question.
I speak for myself, others will have their own reasons. I used the cosine because it is what comes in all the examples that I have seen on the Internet. It didn't occur to me to do it like that, without the cosine ;-)
EDIT 2
Taking into account the issue of numerical precision discussed above, it turns out that there is a reason to use cosine .
Let's think:
So, it
coseno( X )
will have more precision thanseno( X - ( PI / 2 ) )
, for the simple reason that the latter uses 2 imprecise numbers to perform the calculation, which will increase the imprecision of the output.What things !
Simple: Trigonometric Circle
Assuming your clock will have radius 1 on the cartesian coordinate plane then you can use the following function:
If the radius of your clock is larger, you simply multiply the result by the radius.
The idea is based on the trigonometric circle, we know that a circumference will always have two pi radians.
Obviously we must establish a relationship between the minutes and the trigonometric circle, by default it is in pi radians so we must convert the minutes to radians.
If you like to use in another coordinate system, just do a transformation.