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)
Related
I read this tutorial for aabb-plane test, and I'm having trouble understanding the "optimized" aabb-plane test described.
Under "Geometric Approach - Testing Boxes II", I understand everything until this line:
Assume a AAB that has its components x,y,and z varying between xmin
and xmax; ymin and ymax; and zmin and zmax. The components of the
positive vertex p are selected as follows: ...
I also don't get the code that comes afterwards.
My AABB objects are defined by their minVertex, maxVertex parameters.
Can you explain me what did the author mean?
Consider the following picture where the diagonal PS is (supposedly) parallel to the normal vector (BTW, this is what the tutorial calls the AAB case). This picture represents the 2D version of the problem where the idea is easier to explain and understand.
The p-vertex is, by definition, the one that is further aligned with the direction of the normal. This means that you have to decide which one of the following four vectors is further aligned with the normal:
R - Q, Q - R, P - S or S - P (1)
In this case the answer is S - P, which means that S is the p-vertex and P the n-vertex. But the problem is how to detect this programmatically.
Well, the dot product between two vectors measures the projection of one of them onto the other, so the problem can be restated as: which of the vectors in (1) has the maximum dot product with the normal (xn, yn)?
Here are the four dot products:
(xn, yn).(R - Q) = xn(x1 - x2) + yn(y1 - y2)
(xn, yn).(Q - R) = xn(x2 - x1) + yn(y2 - y1)
(xn, yn).(P - S) = xn(x1 - x2) + yn(y2 - y1)
(xn, yn).(S - P) = xn(x2 - x1) + yn(y1 - y2)
So, the one we are looking for is the one that makes both terms of the sum positive, this is
if xn > 0 select x2 - x1 (>0) otherwise x1 - x2 (<0)
if yn > 0 select y2 - y1 (>0) otherwise y1 - y2 (<0)
The reason being the rule of signs (+ times + = + and - times - = +.)
All of this translates into
start with p-vertex = (x1, y1)
if xn > 0, change x1 with x2 (effectively leaving p-vertex = (x2, y1))
if yn > 0, change y1 with y2 (effectively leaving p-vertex = (x*, y2))
(In step 3, x* is x1 or x2 depending on the result of setp 2)
These arguments remain valid if you are in 3 dimensions. The only difference is that you have to add z coordinates everywhere.
I have a question regarding formula curving through a control point.
As you know, HTML Canvas has quadraticCurveTo(x1, y1, x2, y2) with x1 and x2 being the control point.
However when you try to draw a stroke using it, the stroke will never touch the control point.
So we have this formula:
x1 = xt * 2 - (x0 + x2) / 2;
y1 = yt * 2 - (y0 + y2) / 2;
(xt, yt) = the point you want to curve through. t for tangent as it is 90 degrees perpendicular at that point.
This recalculates the control point position.
I got this formula from a book, however the book doesn't explain how it is been derived. I tried google around but in vain.
Anyone knows how this formula is derived?
Thanks,
Venn.
Quadratic Bezier curve is described by equations:
x(t) = x0 * (1-t)^2 + 2 * x1 * t * (1 - t) + x2 * t^2 (and similar for y(t)).
If we apply parameter value t = 1/2 (in some way - middle of the curve), we will get your formula:
x(t=1/2) = xt = x0 * 1/4 + 2 * x1 * 1/4 + x2 * 1/4
then
x1/2 = xt - (x0 + x2)/4
x1 = 2 * xt - (x0 + x2)/2
This is called a Spline. More to the point, it appears that they are using a Bezier Curve.
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
I have x,y data coming in from a [coordinates1] database (GIS - but this could be any database). I have my application with it's own coordinate system, referencing THE SAME MAP.
I have established that a linear relationship exists between coordinates1 (x,y) and coordinates2(x,y) as I have subtracted two different coordinates1 and coordinates2 (dividing x1 with x2 and y1 with y2) and in all cases I get them both showing 0.724 or 0.141 or 0.825 respectively i.e. coordinates1 + coordinates2.
What I now need to figure out - or you help - is that if coordinates1(100000,200000) and coordinates2(0.125,0.255) how do I calculate coordinates2(x,y) from the data in coordinates1(x,y)?
For the sake of clarity, I'm going to call coordinates in your base (xn, yn), and coordinates in your target (un, vn).
Now, if we assume:
The origins of the two coordinate systems are the same.
The orientation of the two coordinate systems are the same (i.e. one is not rotated with respect to the other).
In this case you only need one set of points {(x1, y1), (u1, v1)} to determine the location of (un, vn):
un = u1/x1 * xn
vn = v1/y1 * yn
Note: we must have x1 ≠ 0, y1 ≠ 0
On the other hand, if the two coordinate systems have different origins (but they are still not rotated with respect to one another), we will need two sets of points {(x1, y1), (u1, v1)} and {(x2, y2), (u2, v2)}:
un = (u2 - u1)/(x2 - x1) * (xn - x1) + u1
vn = (v2 - v1)/(y2 - y1) * (yn - y1) + v1
Note: we must have x1 ≠ x2, y1 ≠ y2
Now, if the two coordinate systems are rotated with respect to one another, you need (I believe) one more set of matching coordinates. But it doesn't sound like you need that (unless one of your maps has north pointing in a direction other than straight up), so I'm not going to work out the math now. :)
To do the conversion, you need to know the coordinates of one point O in your two coordinates systems .
Let's suppose O has coordinates x1O,y1O in coordinate system 1, and x2O,y2O in coordinate system 2.
Then a point with coordinates x1,y1 in system 1, and x2,y2 in system 2 will satisfy:
(x1O - x1) = Kx * (x2O - x2)
(y1O - y1) = Ky * (y2O - y2)
where Kx and Ky are the scale factor. If you know the coordinates of an other point M in both systems, than you will have Kx and Ky with
Kx = (x1O - x1M) / (x2O - x2M)
Ky = (y1O - y1M) / (y2O - y2M)
Then, you just need to apply the first relationship to go from one system to another system, with
x1 = x1O - Kx * (x2O - x2)
y1 = y10 - Ky * (y2O - y2)
or
x2 = x2O - (x1O - x1) / Kx
y2 = y2O - (y1O - y1) / Ky
Do you also need the code ?
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.