Math trigonometry
endif ?>A sine calculates the vertical offset between two points based on the angle. A cosine calculates the horizontal distance.
Let's say we have two points, a and b, connected by a line. | |
Now let's assume a is at the origin (or center) of a circle. The circle has a radius equal to the length of the line connecting a and b. So b is located somewhere on the circle's circumference. | |
There is a horizontal and vertical distance between a and b. We can use sine and cosine to determine those distances. | |
Each line from a to a point on the circle's circumference (for example, b) has an angle. Measured counterclockwise, starting from 0°, a circle has a total circumference of 360°. So a line from a going straight up would have an angle of 90°. In the case of the line between a and b, the angle is 60°. | |
The cosine function calculates the horizontal distance between a and b based on the angle. The sine function calculates the vertical distance between a and b. In the case of an angle of 60°, the sine yields 0.5. So the horizontal distance between a and b is half the length of the line between a and b. | |
With sine and cosine we can calculate the distance between a and b. This is useful if we have position coordinates for a and we need to calculate the position of a point b that is orbiting around it. For 60°, the sine yields 0.5, meaning half the length of the distance between a and b. So the horizontal distance between a and b is 70 * 0.5 = 35. So b's x equals a's x plus 35. | |
The command in NodeBox would look like this: def coordinates(x0, y0, distance, angle): from math import radians, sin, cos angle = radians(angle) x1 = x0 + cos(angle) * distance y1 = y0 + sin(angle) * distance return x1, y1 |