I'm sure this is basic trigonometry, and I bet I covered at school many years ago, but I struggle knowing what function to apply to a real world situation. Anyway, rather than try and explain what I need help with, I've drawn a little diagram:
I know p1, p2, r1 and r2 but I can't remember (or know how to search for) how to work out what p3 and p4 are.
The basic application of this setup is I have 2 circles (red and blue) and I need them constantly connected as I drag them around the canvas. The pink link will connect them via their centre points but I don't want the line to penetrate the circle's circumferences.
Hope that makes sense? Thanks in advance.
this is simple vector math (no trigonometry needed)
create unit vector v with P1 to P2 direction
That is easy in vector form:
v=P2-P1; v/=|v|
And when put into 2D:
v.x=P2.x-P1.x;
v.y=P2.y-P1.y;
l=sqrt((v.x*v.x)+(v.y*v.y))
v.x/=l;
v.y/=l;
Now just translate from P1,P2by r1,r2
Vector form:
P3=P1+r1*v
P4=P2-r2*v
In 2D:
P3.x=P1.x+r1*v.x;
P3.y=P1.y+r1*v.y;
P4.x=P2.x-r2*v.x;
P4.y=P2.y-r2*v.y;
You have to solve the following equation system:
For p3 -->
(X-p1x)/(p1x-p2x)=(Y-p1y)/(p1y-p2y)
(X-p1x)^2 + (Y-p1y)^2 = r1^2
The same for p4 just change r1 for r2 and p1 for p4 in the second equation.
The first equation is the equation of a line given 2 points.
And the second equation is the equation of a circle given a center point and a radius.
The resulting X, Y values will be the values of p3, and then p4.
Let d be the distance between p1(x1, y1) and p2(x2,y2)
Thus
d = sqrt((x1-x2)^2 + (y1-y2)^2)
Now point p3(x3, y3) divides the line between p1 and p2 in the ratio of r1:(d-r1)
Thus
x3 = (r1*x2 + (d-r1)*x1)/d and
y3 = (r1*y2 + (d-r1)*y1)/d
Similarly for p4(x4, y4)
x4 = (r2*x1 + (d-r2)*x2)/d and
y4 = (r2*y1 + (d-r2)*y2)/d
What I am going to say is a little long. I will let you write your own code, however, of course will not help with that.
You know points P1, P2, and radius R1 and R2. Say suppose points P1 and P2 have coordinates (x1,y1) and (x2,y2) respectively.
The line connecting P1 and P2 is a straight line and hence you can calculate the slope of the line using the formula m=(y2-y1)/(x2-x1). Since you know the slope and know two coordinates, you can calculate the intercept c and construct a formula of the form y=mx+c.
Once the line formula is there, you can apply values for x and calculate y for point P3, lets say x3 and y3 since you have the radius R1. Similarly, calculate the coordinates for P4.
Related
My question is simple but i can't remember the "transformation" formula:
I have a surface S in a 3d world. I know the coordinate of all four corners (P1, P2, P3, P4) where P1 is (x1, y1, z1) ect.
Then a random rotation and translation yielding the same surface is applied.
I found the new coordinate of all four corners (Q1, Q2, Q3, Q4).
I already found the transfomation matrix for all this 4 point so (R1, T1), (R2, T2) ...
But, How can i found the universal transformation matrix for all points of S?
So, for example, i have coordinate of point P5 on our surface. How Can i found Q5?
Thanks
EDIT:
I found the answer, was really stupid sorry. The transformation matrix are different for each point because it is composed by Rmat x tvec . Rmat is equal for all point but tvec no, it change depending by where the rotation was applied. BUT yeah, all point are in relationship, I can use only the trasformation matrix of one of this point, P1 and apply this formula; Q2 = (R1,t1) x P1 + P2* where P2* is P2 coordinate respect P1 = P2 - P1
I have a polygon where I'm looking for 2 segments that are approximately 90 degree (+-20°). I always found the first one which is between p1 and p2 and I want to find p3, but in the image below, we can se that between p3 and p2 it's not a direct line.
I had thought maybe looking not directly the first point after p2 but a certain number of points and to see if it's still 90° between p1 p2 and that point but then I have the problem that it will not stop to the good p3. So I thought maybe a cost function calculated with the distance between p2 and the looking point and maybe the dot product, but it didn't turned out to be good.
Does someone maybe have an idea how could I just ignore the points between p3 and p2?
You could try to adapt the Hough transform to this application.
I need a help in writing the algorithm remove nodes Bezier curve. Using cubic Bezier curves, there are two curves (P0, P1, P2, P3 and Q0, Q1, Q2, Q3), which have a common point (P3=Q0). Need to get a single curve (P0, R1, R2, Q3), repeating the shape of two. How to find the coordinates of control points R1, R2?
Thank you!
In the general case, it's not possible to do what you're asking. You're asking to go from 7 degrees of freedom down to 4 but keep the same result. The representative power of the lower DOF system can't match that of the higher. The only time it would be possible is if the more complex curve still happened to lie in the simpler space. For example, if your two Bezier curves came from sub-dividing a single parent curve with points R0, R1, R2, R3. Using the de Casteljau algorithm, we can generate two new curves, P and Q, that lie on the same original curve and share a point that is t distance along the original curve (where t is in [0,1]).
P0 = R0
P1 = R0*(1-t) + R1*t
X = R1*(1-t) + R2*t
P2 = P1*(1-t) + X*t
Q3 = R3
Q2 = R2*(1-t) + R3*t
Q1 = X*(1-t) + Q2*t
Q0 = P3 = P2*(1-t) + Q1*t
If that relationship doesn't hold for your original points, then you'll have to craft an approximation. But you might get away with pretending that the relationship holds and just invert the equations:
R1 = (P1 - P0*(1-t))/t
R2 = (Q2 - Q3*t)/(1-t)
Where
t = (Q0 - P2)/(Q1 - P2)
This last equation is the problem because, unless P2, Q0, Q1 are co-linear it won't work exactly. t is a scalar, but Q1-P2 is normally an n-dimensional point. So you can solve it separately for each dimension and find the average, or be a bit more sophisticated and minimize the squared error.
I'm trying to find a solution to this symbolic non-linear vector equation:
P = a*(V0*t+P0) + b*(V1*t+P1) + (1-a-b)*(V2*t+P2) for a, b and t
where P, V0, V1, V2, P0, P1, P2 are known 3d vectors.
I attempted to do that in Matlab like this:
P = sym('P', [3,1])
P0 = sym('P0', [3,1])
P1 = sym('P1', [3,1])
P2 = sym('P2', [3,1])
V0 = sym('V0', [3,1])
V1 = sym('V1', [3,1])
V2 = sym('V2', [3,1])
syms a b t
F = a*(V0*t+P0) + b*(V1*t+P1) + (1-a-b)*(V2*t+P2) - P
solve(F,a,b,t)
I get
Warning: Explicit solution could not be found.
I'm starting to run out of ideas how to solve it, this isn't the first math package I tried.
The interesting bit is that this equation has a simple geometrical interpretation. If you imagine that points P0-P2 are vertices of a triangle, V0-V2 are roughly vertex normals and point P lies above the triangle, then the equation is satisfied for a triangle containing point P with it's three vertices on the three rays (V*t+P), sharing the same parameter t value. a, b and (1-a-b) become the barycentric coordinates of the point P.
So if the case is not degenerate, there should be only one well defined solution for t.
As symbolic equation, this one has 3 variables, so there is no way to have a single solution.
Imagine you pick any values for say b and t. Then in almost all cases you can solve for a, so you get many different solutions.
If you want to think geometrically, imagine that V0 and V1 point in the upper half-space regarding the (P0,P1,P2) triangle, but V2 point in the lower. Also V0,V1 are perpendicular to the plane of the triangle and V0 and V1 are unit vectors.
Now if you have a plane pinned at the point P, which intersects the rays P0+t*V0 and P1+t*V1 at the same distance above the triangle, you can move the plane in such a way that it stays pinned at P and intersecting the two rays at the same distance. It's only a matter of having had picked V2 in such a way that the point of intersection with this plane moves at the same velocity, so it will correspond to the same t, thus giving you infinitely many solutions.
Another example would be if all V0-V2 were colinear with the triangle P0,P1,P2. Then you trivially get a solution for any t.
So you need more equations to solve this symbolically.
I have a triangle in 3D space defined by its 3 vertices, p0, p1, and p2.
I wish to calculate a plane in this 3D space which lies along both p0 and p1 and faces a third point, p2.
This plane is to be defined by a position and normalised direction/
In addition to lying along p0 and p1, and facing p2, the plane should also be perpendicular to the plane created by p0, p1, and p2
I've struggled with this for quite a while and any help anyone can offer is greatly appreciated.
Your question is ill-posed. For any plane that lies on p0 and p1 there will be some point on that plane that "faces" point p2. So all that's left to compute is some plane along p0 and p1.
normal = normalize(cross(p1-p0, pX-p0)) //pX is anything except p1
planePoint = p0
EDIT: see comments
here is an example of my comment explanation
octave:14> p0
p0 =
0 0 0
octave:15> p1
p1 =
0 0 5
octave:16> p2
p2 =
5 0 0
octave:17> cross(p1-p0, cross(p1-p0,p2-p0))
ans =
-125 0 0
You'll notice that the sign is wrong, play with the order of parameters in the cross product to get it facing the right way. Also don't forget to normalize...but it wont affect the direction. Also check to make sure the norm after each cross product isn't near 0, otherwise there is no unique answer.. (triangle forms a line)
Unless I'm misunderstanding what you're asking, a vector from the line to p2 will be the normal to the plane you're trying to define. Basically, you construct a line at right angles to the line p0-p1, running through p2.