I'm not very good at math or geometry, but I want to draw some line segments at increasing angles. What I want to draw is something like when you hold your hand up and spread your fingers apart: lines that start at a common point and expand out at angles that have an equal difference between them.
I have tried this:
len = 300;
angle = 10;
for (i = 0; i <= 5; ++i) {
endPointX = 50 + len * Math.cos(angle);
endPointY = 50 + len * Math.tan(angle);
draw.Line(50, 50, endPointX, endPointY);
angle += 10;
}
However, that's totally wrong and produces something like this
http://i.stack.imgur.com/taX40.png
But I want something like this (bad mspaint, sorry):
http://i.stack.imgur.com/8xfpp.png
What's the right math for this?
There are two separate issues in your question, I will cover each.
Here's an ASCII picture of your situation:
B + /| / | / | / | len / | y / | / | / | / __| / θ | | +----------+ A x C
This is a right triangle. It has three sides:
len
. The hypotenuse is what you're trying to draw.θ
and has a length y
.θ
and has a length x
.Given the above illustration the following equations are true:
cos(θ) = x/len
sin(θ) = y/len
These equations are another way of saying:
When solving the equation for x
and y
, you get:
x = len * cos(θ)
y = len * sin(θ)
So you want sin()
and cos()
, not cos()
and tan()
. If the point A
is not at the origin, you would need to offset x
and y
by addition, like so:
x = len * cos(θ) + 50
y = len * sin(θ) + 50
With the values for x
and y
, you can find the coordinates for point B
on the triangle, and thus be able to draw your lines.
Also, assuming you're programming in Java, the trigonometric functions in the Math
class expect the angle in radians, not degrees. Lots of programming languages that provides trigonometric functions are like this.
Radians and degrees measure the same thing, but a complete rotation in degrees goes from 0
to 360°
while a complete rotation in radians go from 0
to 2π
.
To convert angles in degrees to radians, multiply the angle by π/180
. In Java, the constant π
is provided by Math.PI
.
For example, an angle of 10° degrees is equivalent to 10 * π/180
, or π/18
radians.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With