Finding a point on a line - math

I know the start and end points on a line segment. For this example say that the line segment has a distance of 5. Now I want to know the point that has a distance of three away from the end point. Any idea how to do this with math?
Start Point (0,0)
End Point (0,5)
Point I want to find (0,2)

If your points are (x1, y1) and (x2, y2), and you want to find the point (x3, y3) that is n units away from point 2:
d = sqrt((x2-x1)^2 + (y2 - y1)^2) #distance
r = n / d #segment ratio
x3 = r * x2 + (1 - r) * x1 #find point that divides the segment
y3 = r * y2 + (1 - r) * y1 #into the ratio (1-r):r

Related

Given 2 vector and 2 angle how to find the 3rd vector

It seems to be a very easy question but I just can't figure it out ...
as shown on the below graph:
Supposing we know :
Vector (X,Y)
Vector (X1,Y1)
Angle a
How can I get the vector (?,?) in Unity ?
Many Thanks in advance.
Subtract X1,Y1 from all coordinates.
XX = X - X1
YY = Y - Y1
Let (DX, DY) is vector between (XX, YY) and unknown point.
This vector is perpendicular to (XX, YY), so scalar product is zero.
And length of this vector is equal to length of (XX, YY) multiplied by tangent of angle.
So equation system is
DX * XX + DY * YY = 0
DX^2 + DY^2 = (XX^2 + YY^2) * Tan^2(Alpha)
Solve this system for unknowns (DX, DY) (there are two solutions in general case), then calculate unknown coordinates as (X + DX, Y + DY)
Not totally sure if there is a more efficient method to do this, but it will work.
First you need to find the magnitude of the distance vector between X,Y and X1,Y1. We will call this Dist1.
Dist1 = Vector2.Distance(new Vector2(X,Y), new Vector2(X1,Y1));
Using this distance, we can find the magnitude of the vector for the line going to X?,Y? which we will call DistQ.
DistQ = Dist1 / Mathf.Cos(a * Mathf.Deg2Rad);
You now need to find the angle of this line relative to the overall coordinate plane which will create a new triangle with X?Y? and the x-axis.
angle = Mathf.Atan2((Y - Y1), (X - X1)) * Mathf.Rad2Deg - a;
Now we can use more trig with the DistQ hypotenuse and this new angle to find the X?(XF) and Y?(YF) components relative to X1 and Y1, which we will add on to get the final vector components.
XF = DistQ * Mathf.Cos(angle * Mathf.Deg2Rad) + X1;
YF = DistQ * Mathf.Sin(angle * Mathf.Deg2Rad) + Y1;

Calculate coords of a point giving angle and a line

I work in canvas and I want to draw some lines.
I have a first line defined by 2 points P1 (x1,y1) and P2 (x2,y2).
I know how to calculate distance r between these 2 points with formula : sqrt((x2-x1)^2 + (y2-y1)^2) .
My problem is the following. With a defined angle Alpha, I would like to calculate coords of a point P3 (x3,y3) like distances P1P3 = P1P2 = r.
I guess it's a problem with trigonometry or polar coords perhaps but I don't remember these notions and I have some problems to find the solution.
The following image resumes the post and show a representation :
Someone has some ideas about the solution ?
Thanks by advance for your help.
Sylvain
The basic idea is to rotate the difference vector:
dx := x2 - x1
dy := y2 - y1
dx' := cos alpha * dx - sin alpha * dy
dy' := sin alpha * dx + cos alpha * dy
x3 := x1 + dx'
y3 := x1 + dy'

Get new position x distance away from another position using a slope

Excuse me if I phrased the title wrong, I am not great with math and don't know the right term, but I figure someone will edit it correctly.
I am creating a script in Lua and I have a target location and my location. I found the slope for it so at the moment I have X1, X2, Y1, Y2, and M(slope).
I do not know the math to add a new point with x distance away.
Sort of like this badly done MS paint example where the 2 black dots are target on the far right, and my location on the left, and then the green dot is what I want.
I'm going to assume thatThe black point on the left is (X1, Y1)The black point on the right is (X2, Y2)The slope M = (Y2 – Y1)/(X2 - X1)This means that the straight line between (X1, Y1) and (X2, Y2) are all the points (X,Y) where
Y = Y1 + M (X-X1)
Define the distance D between (X1, Y1) and (X2, Y2), which is given by
D = Math.sqrt((X2 - X1)² + (Y2 – Y1)²)
where Math.sqrt(...) is the square root function. If you want a point on that straight line to be a distance d from (X1, Y1) then there are two such points, one to the left of (X1, Y1) and one to the right of (X1, Y1). The coordinates of those two points are
(X1 + (X2 - X1) d/D, Y1 + (Y2 - Y1) d/D)
and
(X1 - (X2 - X1) d/D, Y1 - (Y2 - Y1) d/D)
The first point, with the + sign, is the point that's a distance d from (X1, Y1) in the direction of (X2, Y2). The second point with the - sign, is the point that's a distance d from (X1, Y1) but in the direction away from (X2, Y2). If the case you want is always as shown in the diagram, then the answer is always to take the second point with the - sign.
You have to combine two formula.
1. (y3-y2)^2 + (x3-x2)^2 = d
2. (y2-y1)/(x2-x1) = (y3-y2)/(x3-x2)
This is haskell code for your question. it maybe usefull.
foo x1 y1 x2 y2 d =
[(x3,y3) |
x3 <- [-100..100], y3 <- [-100..100],
(y3-y2)^2 + (x3-x2)^2 == d,
(y2-y1)/(x2-x1) == (y3-y2)/(x3-x2)]
ps: 100 is a range. you can change it. this function return two couple. like (-1,1), (11,9). because I dont specify the way.
Another solution:(I combined formulas)
let k = d / sqrt((y2-y1)^2 + (x2-x1)^2)
x3 = x2 + k * (x2-x1) other x3 = x2 - k * (x2-x1)
y3 = y2 + k * (y2-y1) other y3 = y2 - k * (y2-y1)

Get the point that corresponds to 1/3rd the distance from p1 to p2

So for example, if we have p1 = (x1,y1) and p2 = (x2,y2) and I want to find the point that corresponds to 1/3rd the distance from p1 and p2 that lies in the line formed by p1 and p2, then what formula would I use? Having a brainfart right now.
Use Section Formula . Read here.
You have to find a point(x,y) that divides line in ratio 1:3 .
x = (x2+3*x1)/4
y = (y2+3*y1)/4
If the line segment is of distance d units then point (x,y) lies at distance d/3 from (x1,y1) and
distance 2d/3 from point (x2,y2)
If A is the vector to the first point, and B is the vector to the second point, then the point you want is
(2A + B) / 3
This works because the point one third of the way between A and B, vectorially, is the vector A + one third of the vector between A and B:
That is A + 1/3(B-A)
Algebra does the rest.
Same one as for any other position:
p(t) = a*(1-t) + b*t
where 0 <= t <= 1 gives all points on a line between vectors "a" and "b"/
In your case
p = (x1, y1)* (1-1/3) + (x2,y2) * 1/3
which is how some other answers look like.
The point p3 = (x3, y3) 1/3rd of the distance is:
x3 = (2 * x1 + x2) / 3
y3 = (2 * y1 + y2) / 3

Inverse 3D (triangle) projection

I have a 3D math problem which I just can't seem to solve.
I have data of 3 points. The data is a (2D) coordinate on a plane, floating somewhere in 3D space. I also know the (2D) coordinate of the projection. That results in the following array of data:
[[[x1,y1], [px1,py1],
[[x2,y2], [px2,py2],
[[x3,y3], [px3,py3]]
Where the normal (x1 etc.) coordinates stand for the coordinates on the plane and the other (px1 etc.) for the projected coordinates.
What I would like to do is project a new 2D coordinate ([x4,y4]).
.
What I tried so far:
Ofcourse you need an eye for projection, so I set that to [xe,ye,-1]. The xe and ye are known. (It is photo referencing, so I just placed the eye in the center of the photograph.)
Beneath the eye I placed the projection surface (z=0). That gives the following projection coordinates:
[[[x1,y1], [px1,py1,0],
[[x2,y2], [px2,py2,0],
[[x3,y3], [px3,py3,0]]
I can't do the same for the coordinates on the plane, since I don't know anything about that plane.
I also figured that I could make a parameterized formula of the lines running from the eye through the projection coordinates. For line1 that would be:
line1x = xe+(px1-xe)*t1
line1y = ye+(py1-ye)*t1
line1z = -1+t1 // = -1+(0--1)*t1
I also know the distance between the points in 3D. That's the same as in 2D. That means the distance between point1 and point2 would be sqrt((x1-x2)^2+(y1-y2)^2).
I also know the distance between the lines (line1 and line2) at any time. That is sqrt((line1x-line2x)^2+(line1y-line2y)^2+(line1z-line2z)^2).
However, I don't really know how to go from here... Or even whether this is the right route to take.
.
I hope you understand what I want to be able to do, and that you can help me.
Thanks in advance!
There is a function Projection, which can transform points so that Projection([x1, y1]) = [px1, py1] , Projection([x2, y2]) = [px2, py2], Projection([x3, y3]) = [px3, py3]. If I understand correctly, author wants to know how to find this Projection function, so that he can trasnform [x4, y4] into [px4, py4].
Since we are dealing with planes here, the Projection function looks like this:
Proj([ix, iy]) :
return [ax*ix + bx*iy + cx,
ay*iy + by*iy + cy];
Using that we can make 2 equation systems to solve.
The first one
x1 * ax + y1 * bx + cx = px1
x2 * ax + y2 * bx + cx = px2
x3 * ax + y3 * bx + cx = px3
Solving for ax, bx and cx gives us
ax = (px1 * (y3 - y2) - px2*y3 + px3*y2 + (px2 - px3) * y1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
bx = - (px1 * (x3 - x2) - px2*x3 + px3*x2 + (px2 - px3) * x1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
cx = (px1 * (x3*y2 - x2*y3) + x1 * (px2*y3 - px3*y2) + (px3*x2 - px2*x3) * y1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
The second one
x1 * ay + y1 * by + cy = py1
x2 * ay + y2 * by + cy = py2
x3 * ay + y3 * by + cy = py3
Solving for ay, by and cy gives us
ay = (py1 * (y3 - y2) - py2*y3 + py3*y2 + (py2 - py3) * y1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
by = - (py1 * (x3 - x2) - py2*x3 + py3*x2 + (py2 - py3) * x1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
cy = (py1 * (x3*y2 - x2*y3) + x1 * (py2*y3 - py3*y2) + (py3*x2 - py2*x3) * y1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
Note: I used this tool to solve equation systems.
You should use homographic functions and homogeneous coordinates, which are generaly used for 3D perspective operations.
Write
(x4,y4,1) = A1*(x1,y1,1) + A2*(x2,y2,1) + A3*(x3,y3,1),
solving for A1,A2,A3. Then
(xp4,yp4) = A1*(px1,py1) + A2*(px2,py2) + A3*(px3,py3).
1st edit.
(A1,A2,A3) is the solution of the linear system Mat*(A1,A2,A3)=(x4,y4,1).
( x1 x2 x3 )
Mat = ( y1 y2 y3 )
( 1 1 1 )
This can be solved in various ways. For example using Cramer's rules.
2nd edit.
The 1's I inserted are not Z coordinates, but homogeneous extensions of the input coordinates (which must be Euclidean coordinates). (A1,A2,A3) are homogeneous coordinates in the basis formed by the triangle vertices.
3rd edit.
The correspondence between the 3D plane and the projection plane is a projective transformation. It can be defined as a 3x3 matrix T operating on homogeneous coordinates in the input plane (x,y,1) (in your coordinate system) and producing coordinates (u,v,t) in the projection plane. Then px=u/t and py=v/t.
If a point has homogeneous coordinates (A1,A2,A3) in the basis formed by three points of the input plane (not on the same line) then its projection has the same homogeneous coordinates in the projected basis.
It seemed quite clear to me 1 hour ago, but now I'm beginning to doubt: maybe knowing one additional pair of points is needed to have a single solution to the problem... If you can find it, have a look at the book "Algebraic Projective Geometry" by J.G. Semple and G.T. Kneebone.
I don't really understand the problem? Are you trying to locate an object in 3d-space that you know is located on a plane (a wall or floor for example) and the only input you have is 3 points(of which you know the distances between in 3d-space) from a camera-image?
In that case you will have 3 equations like this where localCoordinates is the points coordinates in objectspace(gives the known distance between the points) and world is the objects position in 3d-space.
cameraCoordinates = world*view*projection*localCoordinates
This will yield an equation system with 6 unknown(rotation and position in 3d) and 6 equations (2 for every point). It will however be non linear so you have to solve it using numerical methods. Try the Newton Rapson method.
A "bit" late here, but the highest rated answer doesn't take into account the 3D-space of the problem. We have a perspective projection problem, with three points on a plane (actually any 3 3D points) being projected (as in projective geometry) on the surface of a camera.
It is not possible to give an unambiguous solution to this problem (multiple solutions exist). The general problem of finding a camera position and pose given 3 3D points and their respective 2D perspective projections can be solved using the P3P (Perspective-3-Point) algorithm from the original RANSAC paper, which give up to four possible feasible solutions (with the points in front of the camera).
Given a camera pose, it is trivial to calculate the projection of additional plane points.

Resources