How can I find equation of a line or draw a line, given a starting point, length of line and angle of line (relative to x-axis)?
Let's call the start point (x1, y1)
the other end of the line (x2, y2)
.
Then if you are given a length [L] and an angle from the x-axis [a]:
x2 = x1 + (L * cos(a))
y2 = y1 + (L * sin(a))
If the angle is from the y-axis - swap the cos and the sin.
Draw your line from (x1,y1)
to (x2, y2)
.
You may find an ambiguity as to which direction you want the line to go, you need to be careful how you define your angle.
You'll want to draw it from (0, 0)
to (x_length, tan(angle)*x_length)
. The gradient will be tan(angle)
. You can adjust this for a different starting point by subtracting everything from that starting point.
An equation of a line is like:
m*x + n = y
m can be calculated by angle; m = tan(angle)
And if you know a start point then you can find n.
tan(angle) * startPoint_X + n = startPoint_Y
So n = startPoint_Y - (tan ( angle) * startPoint_X )
If you want to draw a line-segment and you know the length, the start point and the angle, there will be two equations.
The first is m*x + n = y
(we solved it).
And this means m*(endPoint_X) + n = endPoint_Y
The second is to find the endPoint.
length^2 = (endPoint_X - startPoint_X)^2 + (endPoint_Y - startPoint_Y)^2
There are only two things that still we don't know: endPoint_x & endPoint_Y If we rewrite the equation:
length^2 = (endPoint_X - startPoint_X)^2 + ( m*(endPoint_X) + n - startPoint_Y)^2
now we know everything except endPoint_X. This equation will give us two solutions for endPoint_X. Then you can find two different ednPoint_Y.
Starting point you know (x1, x2)
, end point is (x1 + l * cos(ang), y1 + l * sin(ang))
where l
is the length and ang
is the angle.
There is actually two different questions: one in the title, another in the body.
Let's start by answering the question from the title:
Line Equation
The equation of a line is
y = a*x + b
where a
is a tangent of an angle between a line and X-axis, and b
is an elevation of the line drawn through (0, 0).
Line equation given angle and a point
You can easily calculate a
(since you know angle), but you don't know b
. But you also know x0
and y0
, so you can easily calculate b
:
b = y0 - a*x0
Now, equation looks like this:
y = tan(fi)*x + y0 - tan(fi)*x0 = tan(fi)*(x - x0) + y0
Draw a segment given point, angle, length
We want to draw a segment from starting point so that it's length is L and angle to the x-axis is fi.
This is a totally different problem.
You should imagine a right-angled triangle whose acute angle positioned at (x0, y0).
You know Hypotenusa (L) and an angle (fi).
By definition,
a = L*cos(fi) (adjacent, x)
b = L*sin(fi) (opposite, y)
All you need is to add x0 and y0:
x1 = x0 + L*cos(fi)
y1 = y0 + L*sin(fi)