I want to calculate the intersection points of two curves arcs described by mathematical ellipses.
Ellipses are based on fitted points 1 and described with:
vertex (main-axis), theta (angle of rotation) , a (y-scale factor)
How can I calculate the intersection of the eclipse parts? Implementation will be done in Matlab and C++.
Ellipse Fitting
Based on Source 1, the ellipse is calculated as following in matlab:
//Calculates ellipse based on provided xy data
[vertex,theta,a] = myfit(xdata,ydata);
plot(xdata,ydata,'k.','linewidth', 3);
hold on
drawParabola([vertex(1) vertex(2) a -theta],'Color', colorstring(i), 'linewidth', 1)
The Source Code can be found here:
Dropbox Matlab Files
Write general equations
A1 * x^2 + 2 * B1 * x * y + C1 * y^2 + D1 * x + E1 * y + F1 = 0
A2 * x^2 + 2 * B2 * x * y + C2 * y^2 + D2 * x + E2 * y + F2 = 0
for both ellipses and solve this equation system for x and y (I'm sure that Matlab knows the way). You'll get up to four solutions (there are possible 4 intersection points)
Related
I have a triangle in 3D cartesian space, it forms a surface. I have a normal vector of that surface. What I want to find out, is a vector tangent to that surface, which points the most "upwards". (The orange one on image, forgive my paint skills)
Let one triangle edge vector is A. Get perpendicular vector in the plane
P = N x A
and normalize P and A
p = P / len(P)
a = A / len(A)
Any unit vector in the plane is combination of these base vectors
v = p * cos(t) + a * sin(t) (1)
We want that Z-component of v to be maximal (as far as I understand most "upwards")
vz = pz * cos(t) + az * sin(t) (2)
has extremum when it's derivative by t is zero
0 = (pz * cos(t) + az * sin(t))' = -pz * sin(t) + az * cos(t)
tan(t) = az / pz
t = atan2(az , pz)
put t values into (1) and get needed vector v
I'm doing AR application where I have camera pose orientation and position.
Given 4 points in world coordinates, how would I wrap the camera image into 2D quad ?
So given the Right image, I would like to get a 2D quad as shown in the left.
Parameters of perspective transformation matrix could be calculated using system of 8 equations for initial and warped coordinates of points:
x1' = (A * x1 + B * y1 + C) / (G * x1 + H * y1 + 1.0)
y1' = (D * x1 + E * y1 + F) / (G * x1 + H * y1 + 1.0)
You can find description of perspective transformation math in in Paul Heckbert article.
Example of implementation (C++): Antigrain library (file agg_trans_perspective.h)
Hello again first part is working like a charm, thank you everyone.
But I've another question...
As I've no interface, is there a way to do the same thing with out not knowing the radius of the circle?
Should have refresh the page CodeMonkey solution is exactly what I was looking for...
Thank you again.
============================
First I'm not a developer, I'm a simple woodworker that left school far too early...
I'm trying to make one of my tool to work with an autonomous robot.
I made them communicate by reading a lot of tutorials.
But I have one problem I cant figure out.
Robot expect position of the tool as (X,Y) but tool's output is (A,B,C)
A is the distance from tool to north
B distance to east
C distance at 120 degree clockwise from east axe
the border is a circle, radius may change, and may or may not be something I know.
I've been on that for 1 month, and I can't find a way to transform those value into the position.
I made a test with 3 nails on a circle I draw on wood, and if I have the distance there is only one position possible, so I guess its possible.
But how?
Also, if someone as an answer I'd love pseudo code not code so I can practice.
If there is a tool to make a drawing I can use to make it clearer can you point it out to me?
Thank you.
hope it helps :
X, Y are coordinate from center, Da,Db, Dc are known.
Trying to make it more clear (sorry its so clear in my head).
X,Y are the coordinate of the point where is the tool (P).
Center is at 0,0
A is the point where vertical line cut the circle from P, with Da distance P to A;
B is the point where horizontal line cuts the circle fom P, with Db distance P to B.
C is the point where the line at 120 clockwise from horizontal cuts the circle from P, with Dc distance P to C.
Output from tool is an array of int (unit mm): A=123, B=114, C=89
Those are the only informations I have
thanks for all the ideas I'll try them at home later,
Hope it works :)
Basic geometry. I decided to give up having the circle at the origin. We don't know the center of the circle yet. What you do have, is three points on that circle. Let's try having the tool's position, given as P, as the new (0,0). This thus resolves to finding a circle given three points: (0, Da); (Db,0), and back off at 120° at Dc distance.
Pseudocode:
Calculate a line from A to B: we'll call it AB. Find AB's halfway point. Calculate a line perpendicular to AB, through that midpoint (e.g. the cross product of AB and a unit Z axis finds the perpendicular vector).
Calculate a line from B to C (or C to A works just as well): we'll call it BC. Find BC's halfway point. Calculate a line perpendicular to BC, through that midpoint.
Calculate where these two lines cross. This will be the origin of your circle.
Since P is at (0,0), the negative of your circle's origin will be your tool's coordinates relative to the circle's origin. You should be able to calculate anything you need relative to that, now.
Midpoint between two points: X=(X1+X2)/2. Y=(Y1+Y2)/2.
The circle's radius can be calculated using, e.g. point A and the circle's origin: R=sqrt(sqr((Ax-CirX)+sqr(Ay-CirY))
Distance from the edge: circle's radius - tool's distance from the circle's center via Pythagorean Theorem again.
Assume you know X and Y. R is the radius of the circle.
|(X, Y + Da)| = R
|(X + Db, Y)| = R
|(X - cos(pi/3) * Dc, Y - cos(pi/6) * Dc)| = R
Assuming we don't know the radius R. We can still say
|(X, Y + Da)|^2 = |(X + Db, Y)|^2
=> X^2 + (Y+Da)^2 = (X+Db)^2 + Y^2
=> 2YDa + Da^2 = 2XDb + Db^2 (I)
and denoting cos(pi/3)*Dc as c1 and cos(pi/6)*Dc as c2:
|(X, Y + Da)|^2 = |(X - c1, Y - c2)|^2
=> X^2 + Y^2 + 2YDa + Da^2 = X^2 - 2Xc1 + c1^2 + Y^2 - 2Yc2 + c2^2
=> 2YDa + Da^2 = - 2Xc1 + c1^2 - 2Yc2 + c2^2
=> Y = (-2Xc1 + c1^2 + c2^2 - Da^2) / 2(c2+Da) (II)
Putting (II) back in the equation (I) we get:
=> (-2Xc1 + c1^2 + c2^2 - Da^2) Da / (c2+Da) + Da^2 = 2XDb + Db^2
=> (-2Xc1 + c1^2 + c2^2 - Da^2) Da + Da^2 * (c2+Da) = 2XDb(c2+Da) + Db^2 * (c2+Da)
=> (-2Xc1 + c1^2 + c2^2) Da + Da^2 * c2 = 2XDb(c2+Da) + Db^2 * (c2+Da)
=> X = ((c1^2 + c2^2) Da + Da^2 * c2 - Db^2 * (c2+Da)) / (2Dbc2 + 2Db*Da + 2Dac1) (III)
Knowing X you can get Y by calculating (II).
You can also make some simplifications, e.g. c1^2 + c2^2 = Dc^2
Putting this into Python (almost Pseudocode):
import math
def GetXYR(Da, Db, Dc):
c1 = math.cos(math.pi/3) * Dc
c2 = math.cos(math.pi/6) * Dc
X = ((c1**2 + c2**2) * Da + Da**2 * c2 - Db * Db * (c2 + Da)) / (2 * Db * c2 + 2 * Db * Da + 2 * Da * c1)
Y = (-2*X*c1 + c1**2 + c2**2 - Da**2) / (2*(c2+Da))
R = math.sqrt(X**2 + (Y+Da)**2)
R2 = math.sqrt(Y**2 + (X+Db)**2)
R3 = math.sqrt((X - math.cos(math.pi/3) * Dc)**2 + (Y - math.cos(math.pi/6) * Dc)**2)
return (X, Y, R, R2, R3)
(X, Y, R, R2, R3) = GetXYR(123.0, 114.0, 89.0)
print((X, Y, R, R2, R3))
I get the result (X, Y, R, R2, R3) = (-8.129166703588021, -16.205081335032794, 107.1038654949096, 107.10386549490958, 107.1038654949096)
Which seems reasonable if both Da and Db are longer than Dc, then both coordinates are probably negative.
I calculated the Radius from three equations to cross check whether my calculation makes sense. It seems to fulfill all three equations we set up in the beginning.
Your problem is know a "circumscribed circle". You have a triangle define by 3 distances at given angles from your robot position, then you can construct the circumscribed circle from these three points (see Circumscribed circle from Wikipedia - section "Other properties"). So you know the diameter (if needed).
It is also known that the meeting point of perpendicular bisector of triangle sides is the center of the circumscribed circle.
Let's a=Da, b=Db. The we can write a system for points A and B at the circumference:
(x+b)^2 + y^2 = r^2
(y+a)^2 + x^2 = r^2
After transformations we have quadratic equation
y^2 * (4*b^2+4*a^2) + y * (4*a^3+4*a*b^2) + b^4-4*b^2*r^2+a^4+2*a^2*b^2 = 0
or
AA * y^2 + BB * y + CC = 0
where coefficients are
AA = (4*b^2+4*a^2)
BB = (4*a^3+4*a*b^2)
CC = b^4-4*b^2*r^2+a^4+2*a^2*b^2
So calculate AA, BB, CC coefficients, find solutions y1,y2 of quadratic eqiation, then get corresponding x1, x2 values using
x = (a^2 - b^2 + 2 * a * y) / (2 * b)
and choose real solution pair (where coordinate is inside the circle)
Quick checking:
a=1,b=1,r=1 gives coordinates 0,0, as expected (and false 1,-1 outside the circle)
a=3,b=4,r=5 gives coordinates (rough) 0.65, 1.96 at the picture, distances are about 3 and 4.
Delphi code (does not check all possible errors) outputs x: 0.5981 y: 1.9641
var
a, b, r, a2, b2: Double;
aa, bb, cc, dis, y1, y2, x1, x2: Double;
begin
a := 3;
b := 4;
r := 5;
a2 := a * a;
b2:= b * b;
aa := 4 * (b2 + a2);
bb := 4 * a * (a2 + b2);
cc := b2 * b2 - 4 * b2 * r * r + a2 * a2 + 2 * a2 * b2;
dis := bb * bb - 4 * aa * cc;
if Dis < 0 then begin
ShowMessage('no solutions');
Exit;
end;
y1 := (- bb - Sqrt(Dis)) / (2 * aa);
y2 := (- bb + Sqrt(Dis)) / (2 * aa);
x1 := (a2 - b2 + 2 * a * y1) / (2 * b);
x2 := (a2 - b2 + 2 * a * y2) / (2 * b);
if x1 * x1 + y1 * y1 <= r * r then
Memo1.Lines.Add(Format('x: %6.4f y: %6.4f', [x1, y1]))
else
if x2 * x2 + y2 * y2 <= r * r then
Memo1.Lines.Add(Format('x: %6.4f y: %6.4f', [x2, y2]));
From your diagram you have point P that you need it's X & Y coordinate. So we need to find Px and Py or (Px,Py). We know that Ax = Px and By = Py. We can use these for substitution if needed. We know that C & P create a line and all lines have slope in the form of y = mx + b. Where the slope is m and the y intercept is b. We don't know m or b at this point but they can be found. We know that the angle of between two vectors where the vectors are CP and PB gives an angle of 120°, but this does not put the angle in standard position since this is a CW rotation. When working with circles and trig functions along with linear equations of slope within them it is best to work in standard form. So if this line of y = mx + b where the points C & P belong to it the angle above the horizontal line that is parallel to the horizontal axis that is made by the points P & B will be 180° - 120° = 60° We also know that the cos angle between two vectors is also equal to the dot product of those vectors divided by the product of their magnitudes.
We don't have exact numbers yet but we can construct a formula: Since theta = 60° above the horizontal in the standard position we know that the slope m is also the tangent of that angle; so the slope of this line is tan(60°). So let's go back to our linear equation y = tan(60°)x + b. Since b is the y intercept we need to find what x is when y is equal to 0. Since we still have three undefined variables y, x, and b we can use the points on this line to help us here. We know that the points C & P are on this line. So this vector of y = tan(60°)x + b is constructed from (Px, Py) - (Cx, Cy). The vector is then (Px-Cx, Py-Cy) that has an angle of 60° above the horizontal that is parallel to the horizontal axis. We need to use another form of the linear equation that involves the points and the slope this time which happens to be y - y1 = m(x - x1) so this then becomes y - Py = tan(60°)(x - Px) well I did say earlier that we could substitute so let's go ahead and do that: y - By = tan(60°)(x - Ax) then y - By = tan(60°)x - tan(60°)Ax. And this becomes known if you know the actual coordinate points of A & B. The only thing here is that you have to convert your angle of 120° to standard form. It all depends on what your known and unknowns are. So if you need P and you have both A & B are known from your diagram the work is easy because the points you need for P will be P(Ax,By). And since you already said that you know Da, Db & Dc with their lengths then its just a matter of apply the correct trig functions with the proper angle and or using the Pythagorean Theorem to find the length of another leg of the triangle. It shouldn't be all that hard to find what P(x,y) is from the other points. You can use the trig functions, linear equations, the Pythagorean theorem, vector calculations etc. If you can find the equation of the line that points C & P knowing that P has A's x value and has B's y value and having the slope of that line that is defined by the tangent above the horizontal which is 180° - phi where phi is the angle you are giving that is CW rotation and theta would be the angle in standard position or above the horizontal you have a general form of y - By = tan(180° - phi)(x - Ax) and from this equation you can find any point on that line.
There are other methods such as using the existing points and the vectors that they create between each other and then generate an equilateral triangle using those points and then from that equilateral if you can generate one, you can use the perpendicular bisectors of that triangle to find the centroid of that triangle. That is another method that can be done. The only thing you may have to consider is the linear translation of the line from the origin. Thus you will have a shift in the line of (Ax - origin, By - origin) and to find one set the other to 0 and vise versa. There are many different methods to find it.
I just showed you several mathematical techniques that can help you to find a general equation based on your known(s) and unknown(s). It just a matter of recognizing which equations work in which scenario. Once you recognize the correct equations for the givens; the rest is fairly easy. I hope this helps you.
EDIT
I did forget to mention one thing; and that is the line of CP has a point on the edge of the circle defined by (cos(60°), sin(60°)) in the 1st quadrant. In the third quadrant you will have a point on this line and the circle defined by (-cos(60°), -sin(60°)) provided that this line goes through the origin (0,0) where there is no y nor x intercepts and if this is the case then the point on the circle at either end and the origin will be the radius of that circle.
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 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.