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)?
相关问题
- d3.js moving average with previous and next data v
- How to get a fixed number of evenly spaced points
- Check if a number is a perfect power of another nu
- How to determine +/- sign when calculating diagona
- Union of many (more than two) polygons without hol
相关文章
- ceil conterpart for Math.floorDiv in Java?
- why 48 bit seed in util Random class?
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- Need help generating discrete random numbers from
- How do you create a formula that has diminishing r
- Math.Max vs Enumerable.Max
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]:
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.
Starting point you know
(x1, x2)
, end point is(x1 + l * cos(ang), y1 + l * sin(ang))
wherel
is the length andang
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
where
a
is a tangent of an angle between a line and X-axis, andb
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 knowb
. But you also knowx0
andy0
, so you can easily calculateb
:Now, equation looks like this:
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,
All you need is to add x0 and y0:
You'll want to draw it from
(0, 0)
to(x_length, tan(angle)*x_length)
. The gradient will betan(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 can be calculated by angle;
m = tan(angle)
And if you know a start point then you can find n.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.
There are only two things that still we don't know: endPoint_x & endPoint_Y If we rewrite the equation:
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.