Trying to find 2D coordinates of point along straight line, given one coordinate - unity3d

I have game object that moves along a path on a 2D plane, between 2 points (x1,y1) and (x2,y2). Occasionally it gets moved off the path and needs to be put back on it. When this happens I'll know the x-coordinate, but need to calculate the y-coordinate along the path given the x-coordinate.
Here's an illustration of what I mean:

You have a line segment, i.e., the set of all convex combinations of the given endpoints. You would like to find the coefficients that yield the convex combination (x3,y3), where y3 is unknown.
t (x1,y1) + (1-t) (x2,y2) = (x3,y3)
Since x3 is known, we obtain
t = (x3 - x2) / (x1 - x2)
and, thus,
y3 = ((x3-x2) y1 + (x1-x3) y2) / (x1 - x2)

The general equation of a line in 2D is a.x + b.y + c = 0 where the vector U = (-b, a) is a direction vector of the line.
Because (x1, y1) and (x2, y2) are on the line, you know that:
a.x1 + b.y1 + c = 0
a.x2 + b.y2 + c = 0
(x2-x1, y2-y1) is a direction vector of the line so:
-b = x2-x1 and a = y2-y1
So one equation of your line ax + by + c = 0 with :
a = y2-y1
b = x1-x2
c = -a.x1 - b.y1 = x1(y1-y2) + y1(x2-x1)
Knowing a, b, c and x3, you can easily find y3:
y3 = -(c + a.x3) / b
Pay attention nevertheless to the case where b = 0 (case of a vertical line)

Related

Discretising a PDE using MATLAB

I want to solve the following PDE which describes the time evolution of a membrane
The PDE is discretised as follows
where
and x(s_i) = x_i, j=0,1, and x^j=x, x^j=y. We are ignoring the pressure term for now as well as constants like k_t etc.
We wish to find x(t) using a forward Euler method, by setting x = x + h*dx/dt, with h=1e-6.
My solution (for x(t)) is as follows (ignoring the y terms for ease of answering)
l = [359,1:358];
r = [2:359,1]; %left and right indexing vectors
l1 = [358,359,1:357]; %twice left neighbour
r1 = [3:359,1,2]; %twice right neighbour
%create initial closed contour with coordinates x and y
phi = linspace(0,2*pi,360); phi(end) = []; %since closed curve
x = (5 + 0.5*sin(20*phi)).*cos(phi);
y = (5+0.5*sin(20*phi).*cos(phi);
ds2 = (1/360)^2;
for i = 1:2e5
lengths = sqrt( (x-x(r)).^2 + (y-y(r)).^2 );
Tx=(1/10)/ds2*(x(r) -2*x +x(l) - x0*(((x(r)-x)/lengths)-((x-x(l))/lengths(l)) ) );
%tension term
Bx = 1/ds2^2*(x(r1) - 2*x(r) + x -2(x(r) -2*x + x(l)) + x -2*x(l) + x(l1) ); %bending term
x = x + 1/(1e6)*(Tx); % - Bx);
% Euler forward step
end
Currently, the code runs, but in the last line, if I decomment out the Bx term, the program fails to run. It seems as though my Bx matches what's in the discretisation, but obviously not.

How to find the third coordinate of an equilateral triangle?

If I have two points say A(x,y) & B(p,q), how can I find the coordinates of the third point on both sides of AB?
I have the formula for one side, but that cannot give the other one..
Need the formula for coordinates of third point on each sides.
Since you have the result on one side then it's easy to find the symmetrical point.
Let's say your result for the third point is C(r, s). You need to find D(t, w), the symmetrical point of C, with respect to the segment AB.
For this we consider the middle of AB: M(u, v) = (A(x, y) + B(p, q)) / 2;
We have the following equation: M(u, v) = (D(t, w) + C(r, s))/2.
We get D(t, w) = 2 * M(u, v) - C(r, s).
We further get: D(t, w) = A(x, y) + B(p, q) - C(r, s).
A possible solution, based on a rotation matrix R is:
A = [0 0];
B = [0 1];
AB = B-A;
theta = deg2rad(60);
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
C = A + AB*R';
X = [A;B;C; A];
plot(X(:, 1), X(:, 2));
axis equal

Formula to find intersection point of two lines

How to find an point where line1 and lin2 intersect, if both lines are defined by x,y,alpha where x,y are coordinates of a point on a line and alpha is the angle between the line and x=const?
I tried applying sine theorem but it yields two answers (triangles can be built on both sides of a line). I can check which point forms correct slope with one of the points, but that is ugly.
I can switch to y=ax+b representation, but then I have special cases that I have to worry about. Vertical and horizontal lines should be differently to avoid division by zero in 1/sin(alpha) and 1/cos(alpha) cases.
I am not looking for an implementation in a certain language, just a formula.
These questions are not relevant because they deal with finite line segments, NOT lines.
Given two points and two vectors, find point of intersection
How do you detect where two line segments intersect?
Suppose line 1 is defined by [x1, y1] and alpha1 and line 2 by [x2, y2] and alpha2.
Suppose k1 = tan(alpha1) and k2 = tan(alpha2).
Then the formula for the x-coordinate of the intersection is
x = (y2 - y1 + k1 * x1 - k2 * x2) / (k1 - k2)
Note: Function tan is undefined for angles pi / 2 + k * pi (where k is an arbitrary integer), so:
if k1 is undefined, then x = x1 and y = y2 + k2 * (x1 - x2)
if k2 is undefined, then x = x2 and y = y1 + k1 * (x2 - x1)
(both are practically the same with exchange of indices 1 <--> 2).
For a line equation Y = aX + b, you can calculate a = tan(alpha).
So if line1 is defined as x, y and alpha, the equation is Y = tan(alpha) * X + b.
Now to find b, you need a point on your line. This point has coordinates (x, y).
y = ax + b
b = y - ax
So you line equation is:
Y = tan(alpha) * X + (y - tan(alpha) * x)
Now you only have to solve the lines equation:
Y = a1 * X + b1
Y = a2 * X + b2
Which is:
a1 * X + b1 = a2 * X + b2
(a1 - a2) * X = b2 - b1
X = (b2 - b1) / (a1 - a2)
Right now you can calculate Y too.
So if we replace, we obtain:
X = ((y2 - tan(alpha2) * x2) - (y1 - tan(alpha1) * x1)) / (tan(alpha1) - tan(alpha2)
Simplified:
X = (y2 - y1 - tan(alpha2) * x2 + tan(alpha1) * x1)) / (tan(alpha1) - tan(alpha2)
And then:
Y = tan(alpha1) * X + (y - tan(alpha1) * x

Finding the intersection points of ray-5th order polynomial

I am doing ray tracing and I have A screen described in the world coordinates as Matrices(I had before the X,Y,Z in the screen coordinates and by using transformation and rotation I got it in the world coordinates)
Xw (NXM Matrix)
Yw (NXM Matrix)
Zw (I have got this polynomial (5th order polynomial)by fitting the 3D data Xw and Yw. I have it as f(Xw,Yw))
I have the rays equations too described as usual:
X = Ox + t*Dx
Y = Oy + t*Dy
Z = Oz + t*Dz %(O is the origin point and D is the direction)
So what I did is that I replaced the X and Y in the Polynomial equation f(Xw,Yw) and solved it for t so I can then get the intersection point.
But apparently the method that I used is wrong(The intersection points that I got were somewhere else).
Could any one please help me and tell me what is the mistake. Please support me.
Thanks
This is part of the code:
X_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(1,1) + ScreenYCoordinates.*Rotation_matrix_screen(1,2) + ScreenZCoordinates.*Rotation_matrix_screen(1,3) + Zerobase_scr(1);
Y_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(2,1) + ScreenYCoordinates.*Rotation_matrix_screen(2,2) + ScreenZCoordinates.*Rotation_matrix_screen(2,3) + Zerobase_scr(2);
Z_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(3,1) + ScreenYCoordinates.*Rotation_matrix_screen(3,2) + ScreenZCoordinates.*Rotation_matrix_screen(3,3) + Zerobase_scr(3); % converting the screen coordinates to the world coordinates using the rotation matrix and the translation vector
polymodel = polyfitn([X_World_coordinate_scr(:),Y_World_coordinate_scr(:)],Z_World_coordinate_scr(:),5); % using a function from the MAtlab file exchange and I trust this function. I tried it different data and it gives me the f(Xw,Yw).
ScreenPoly = polyn2sym(polymodel); % Function from Matlab file exchange to give the symbolic shape of the polynomial.
syms X Y Z t Dx Ox Dy Oy oz Dz z;
tsun = matlabFunction(LayerPoly, 'vars',[X,Y,Z]); % just to substitue the symboles from X , Y and Z to (Ox+t*Dx) , (Oy+t*Dy) and (Oz+t*Dz) respectively
Equation = tsun((Ox+t*Dx),(Oy+t*Dy),(Oz+t*Dz));
Answer = solve(Equation,t); % solving it for t but the equation that it is from the 5th order and the answer is RootOf(.... for z)
a = char(Answer); % preparing it to find the roots (Solutions of t)
R = strrep(a,'RootOf(','');
R1 = strrep(R,', z)','');
b = sym(R1);
PolyCoeffs = coeffs(b,z); % get the coefficient of the polynomail
tfun = matlabFunction(PolyCoeffs, 'vars',[Ox,Oy,oz,Dx,Dy,Dz]);
tCounter = zeros(length(Directions),1);
NaNIndices = find(isnan(Surface(:,1))==1); %I have NaN values and I am taking them out
tCounter(NaNIndices) = NaN;
NotNaNIndices = find(isnan(Surface(:,1))==0);
for i = NotNaNIndices' % for loop to calc
OxNew = Surface(i,1);
OyNew = Surface(i,2);
OzNew = Surface(i,3);
DxNew = Directions(i,1);
DyNew = Directions(i,2);
DzNew = Directions(i,3);
P = tfun(OxNew,OyNew,OzNew ,DxNew,DyNew,DzNew);
t = roots(P);
t(imag(t) ~= 0) = []; % getting rid of the complex solutions
tCounter(i) = t;
end
Please support
Thanks in advance

Point in plane closest to point

I'm trying to find a point in the plane closest to a given point.
I have the equation of the plane, the point and the distance between them.
How do I find the point in the plane which is closest to the given point?
I have a tetrahedron with sides:
bcx=0, acy=0, abz=0, x/a+y/b+z/c=1 (a,b,c are not to mix with Ax+By+Cz=D in the planes equation, they are to be entered when running the script).
function [d n]=tetradist(x,y,z,a,b,c)
if z>0 && y>0 && x>0 && z<c && y<b && x<a && x/a+y/b+z/c<1
d1=abs(a*b*z)/sqrt((a*b)^2);
d2=abs(b*c*x)/sqrt((b*c)^2);
d3=abs(a*c*y)/sqrt((a*c)^2);
d4=abs(b*c*x+a*c*y+a*b*z-a*b*c)/sqrt((b*c)^2 + (a*c)^2 + (a*b)^2);
A = [d1 d2 d3 d4];
B = sort(A,'ascend');
d = B(1);
point=[x y z];
if d==d1
normalv=[0 0 a*b]';
elseif d==d2
normalv=[b*c 0 0]';
elseif d==d3
normalv=[0 a*c 0]';
else
normalv=[b*c a*c a*b]';
end
end
So now I have the shortest distance, my point as a vector and the normal vector of the closest plane. Now how do I find the point in said plane which is closest to my point 'point'?
Thanks in advance!
If the equation of your plane is Ax + By + Cz = D and the location of the point is (P, Q, R) then the location in the plane that is closest to the point is
(P,Q,R) + λ * (A,B,C)
where
λ = (D - P*A - B*Q - C*R) / (A^2 + B^2 + C^2)
The following Matlab code calculates this point
function x = closestpoint(n, d, p)
# n is the vector [A,B,C] that defines the plane
# d is the distance of the plane from the origin
# p is the point [P,Q,R]
v = (d - sum(p.*n)) / sum(n.*n);
x = p + v * n;