There are two separate problems in your question, I will talk about each of them.
Here's an ASCII image of your situation:
B
+
/ |
/ |
/ |
/ |
len / | y
/ |
/ |
/ |
/ __ |
/ θ | |
+ ---------- +
A x C
This is a regular triangle. It has three sides:
- The diagonal side in the image opposite 90 ° is called the hypotenuse and has the length
len . Hypotenuse is what you are trying to do. - The vertical side is the side opposite the angle
θ and having a length y . - The horizontal side is the side adjacent to the angle
θ and having 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 the angle is equal to the length of the adjacent side divided by the length of the hypotenuse.
- The sine of the 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 point A not at the origin, you will need to offset x and y by adding, for example:
x = len * cos(θ) + 50 y = len * sin(θ) + 50
With the x and y values, you can find the coordinates for point B in the triangle, and thus you can draw your own lines.
Also, if you program in Java, the trigonometric functions of the Math class expect an angle in radians , not degrees. Many programming languages that provide trigonometric functions are as follows.
Radians and degrees measure the same thing, but the full rotation in degrees goes from 0 to 360° , and the full rotation in radians goes 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.