Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing lines at increasing angles

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?

like image 695
Priam Avatar asked Jan 21 '23 01:01

Priam


1 Answers

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:

  • The diagonal side in the picture opposite to the 90° angle is called the hypotenuse and has a length len. The hypotenuse is what you're trying to draw.
  • The vertical side is the side opposite to the angle θ and has a length y.
  • The horizontal side is the side adjacent to the angle θ 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:

  • The cosine of an angle is equal to the length of the adjacent side divided by the length of the hypotenuse.
  • The sine of an angle is equal to the length of the opposite side divided by the length of the hypotenuse.

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 .

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.

like image 97
In silico Avatar answered Feb 28 '23 01:02

In silico