I have grid based map, and I'm trying to develop a predicate that gives me the diagonal distance between 2 points, this is what I have,
dist_diagonal((X,Y),(X,W),Passos,D):-
dist_ortogonal((X,Y),(X,W),D1),
D is D1 + Passos.
dist_diagonal((X,Y),(Z,Y),Passos,D):-
dist_ortogonal((X,Y),(Z,Y),D1),
D is D1 + Passos.
dist_diagonal((X,Y),(Z,W),Passos,_):-
X>Z,
Y<W,
Passos1 is Passos+1,
X1 is X-1,
Y1 is Y+1,
dist_diagonal((X1,Y1),(Z,W),Passos1,_).
dist_diagonal((X,Y),(Z,W),Passos,_):-
X<Z,
Y<W,
Passos1 is Passos+1,
X1 is X+1,
Y1 is Y+1,
dist_diagonal((X1,Y1),(Z,W),Passos1,_).
but when I try to test it :
dist_diagonal((5,4),(3,8),0,D).
I get true instead of a value for D
What's wrong with my predicate?
PS: I should add that I'm going to use this only with points that verrify Y<Z.
PS2: Notice that I'm calculating the distance using 8 kinds of movements, not the euclidean distance.Diagonal distance
You need to write : dist_diagonal((X,Y),(Z,W),Passos,D):- instead of dist_diagonal((X,Y),(Z,W),Passos,_):-
Your predicate is returning true since D could be anything (since you use anonymous variables '_').
So you need to write:
dist_diagonal((X,Y),(Z,W),Passos,D):-
X>Z,
Y<W,
Passos1 is Passos+1,
X1 is X-1,
Y1 is Y+1,
dist_diagonal((X1,Y1),(Z,W),Passos1,D).
dist_diagonal((X,Y),(Z,W),Passos,D):-
X<Z,
Y<W,
Passos1 is Passos+1,
X1 is X+1,
Y1 is Y+1,
dist_diagonal((X1,Y1),(Z,W),Passos1,D).
I wasn't able to test since I have no definition of dist_ortogonal/3 predicate but I wrote a simple version of it and it worked so it should be working fine.
Related
I have been trying to figure out whether the following problem has a solution. Almost having given up, I would like to ask whether someone can confirm that there is no solution, or maybe give me a hint.
We have two vectors v and w in 3D space and know that the ratio of their magnitudes is ||v|| / ||w|| = 0.8019.
in 3D space an observer would see that they form an angle of 27.017 degrees.
on the other side, an observer in 2D (only seeing the x and z axis), observes an angle of 7.125 degrees between the vectors.
From their view, the vector coordinates are v = (x: 2, z: 1) and w = (x: 3, z: 2).
Is there somehow a way that the 2D observer can calculate the actual angle between these vectors in 3D space?
I would be more than happy for any input. All my tries have failed so far and I just want to know whether there could be a possible solution.
I have solved this problem and get that the values of y1 and y2 are given by this function:
eq1: 0.6439*y2^(2)-y1^(2)=9.785.
Therefore real angle can practically any value, the factor that would narrow this problem down to an actual solution would be the information about where the observer is in the 3d space so that he sees the angle of 27.017º, however, if this is the whole problem, then I can share my solution and process.
Some graphs that I created from my calculations:
The side view of the vectors is directly from the point of view of the x and z axis of the graph, therefore the coordinates of the (x1,z1) and (x2,z2) points(terminal points of the vectors), appear authentic, and not augmented, hence you can use them in your calculations to calculate the coordinates of z1 and z2, which you need to calculate the angle.
V = (x1, y1, z1) V = (2, y1, 1)
W = (x2, y2, z2) W = (3, y2, 2)
Since ||v|| / ||w|| = 0.8019
∴Then sqrt((x1^2)+(y1^2)+(z1^2))/sqrt((x2^2)+(y2^2)+(z2^2)) = 0.8019
∴(x1^2)+(y1^2)+(z1^2)/(x2^2)+(y2^2)+(z2^2) = 0.6430
∴4+(y1^2)+1/9+(y2^2)+4 = 0.6430
∴5+(y1^2) = 8.359 + 0.6430(y2^2)
∴13.359 = 0.6430(y2^2)-(y1^2)
This gives you therefore a function that calculates the other value of y given the some input y.
You can then graph this function using Geogebra.
For all the pairs of values on the curve, together with the fixed values of x and z for both of the vectors you can calculate that the ratio between the magnitudes of the two vectors is equal to 0.8019.
This problem has therefore infinitely many solutions for the angle as there are infinitely many values of z1 and z2 that satisfy the ratio; ||v|| / ||w|| = 0.8019.
Therefore the answer to this problem can be expressed as:
∀Θº∈R:Θº≥0
I'm currently trying to write a program to determine if a line is going through the area of a square of 4 points and I'm searching for a formula. I only found solutions for 3-dimensional planes with vectors and tried to apply them to my situation by calculating with pen and paper but I always seem to hit a dead end when a third value is being needed.
I think the best way to approach it is to calculate the distance of the line to the square. Which would be 0 if it is passing through (a part of it) obviously. But I can't seem to find the right words for the google and stack overflow search since this seems too basic to not have been answered before.
If anyone has a link or a suggestion on how to calculate this I would be really thankful.
For my formula testing I've been working with these simple values:
Line:
l = (0 , 0) + s * (10, 10)
Points of the Square:
A (5, 5)
B (6, 5)
C (6, 6)
D (5, 6)
EDIT:
Using the function of the reply I marked as the answer I got it to work. A problem I had was getting the correct input for the function. The variables a, b and c. This is how I got them in the end:
var a = 1 / x2
var b = -(1 / y2)
var c = y1/y2 - x1/x2
Here's an idea on how you can approach the this problem.
First, what does line passing through a square means in a coordinate system?
Line L passes through square ABCD if and only if L separates the diagonally opposite sides on ABCD (A&C or B&D). Now the problem simplifies to checking whether two given points are separated by a given line.
Let the equation of the line L be ax + by + c = 0. Define a function f(x,y) = ax + by + c. Point A=(x1,y1) and C=(x2,y2) are separated by line L if f(x1,y1) and f(x2,y2) have opposite signs. Additionally if they have the same sign it means for point are on the same side of the line.
Here's the Python code for the above idea:
# Function to check if two points
# lie on the opposite side of the line
def pointsAreOnOppositeSideOfLine(a, b, c, x1, y1, x2, y2):
fx1 = 0 # Variable to store a * x1 + b * y1 - c
fx2 = 0 # Variable to store a * x2 + b * y2 - c
fx1 = a * x1 + b * y1 - c
fx2 = a * x2 + b * y2 - c
# If fx1 and fx2 have same sign
if ((fx1 * fx2) <= 0):
return True
return False
I have two points which form one line: (1,4) and (3,6), and another two which form another line: (2,1) and (4,2). These lines are continuous and I can find their intersection points by finding the equation for each line, and then equating them to find the x value at the intersection point, and then the y value.
i.e. for the first line, the equation is y = x + 3, and the second is y = 0.5x. At the intersection the y values are the same so x + 3 = 0.5x. So x = -6. Subbing this back into either of the equations gives a y value of -3.
From those steps, I now know that the intersection point is (-6,-3). The problem is I need to do the same steps in Excel, preferably as one formula. Can anyone give me some advice on how I would start this?
Its long but here it is:
Define x1,y1 and x2,y2 for the 1st line and x3,y3 and x4,y4 for the second.
x = (x2y1-x1y2)(x4-x3)-(x4y3-x3y4)(x2-x1) / [ (x2-x1)(y4-y3) - (x4-x3)(y2-y1) ]
y = (x2y1-x1y2)(y4-y3)-(x4y3-x3y4)(y2-y1) / [ (x2-x1)(y4-y3) - (x4-x3)(y2-y1) ]
Note that the denominators are the same. They will be ZERO! when the system has no solution. So you may want to check that in another cell and conditionally compute the answer.
Essentially, this formula is derived by solving a system of equations for x and y by hand using generic points (x1,y1), (x2,y2), (x3,y3), and (x4,y4). Easier yet, is solving the system by hand using well developed linear algebra concepts.
Wikipedia outlines this procedure well: Line-line intersection.
Also, this website describes all the different formulas and lets you put in whatever data you have in any mixed format and provides many details of the solutions: Everything about 2 lines.
Here's a matrix based solution:
x - y = -3
0.5*x - y = 0
Written as a matrix equation (I apologize for the poor typesetting):
| 1.0 -1.0 |{ x } { -3 }
| 0.5 -1.0 |{ y } = { 0 }
You can invert this matrix or use LU decomposition to solve it to get the answer. That method will work for any number of cases where you have one equation for each unknown.
This is easy to do by hand:
Subtract the second equation from the first: 0.5*x = -3
Divide both sides by 0.5: x = -6
Substitute this result into the other equation: y = 0.5*x = -3
I have a line between two points a and b (3d vectors) and a third point p (from scalar projection).
I want to determine if the third point p is on the line between a and b or not between them.
What is the most efficient algorithm for the LuaJIT? Other languages would be fine too, but efficiency is important
Thanks to numberZero I got this code and it seems to work
function vec3d:is_between(a, b)
local ab = b - a
local ap = self - a
local lp = ap:len()
local lb = ab:len()
local dot = ap:dot(ab) / (lp * lb)
return lp < lb and dot > 0.95
end
If you already know the three points are collinear, you could compute the vector AP from a to p and the vector AB from a to b. If p is between a and b, then the dot product of AP and AB will equal 1 (i.e. the vectors point in the same direction) and the vector magnitude of AP will be less than that of AB.
If you don't know whether the points are collinear, it's easy to check. You can determine the collinearity of three points xi =
(xi, yi, zi) for i = 1, 2, 3 by implementing a test on the ratio of distances:
x2 - x1 : y2 - y1 : z2 -
z1 = x3 - x1 : y3 -
y1 : z3 - z1.
as explained in this Wolfram Mathworld article on Collinearity. From the same article, an even easier condition
is obtained by noting that the area of a triangle determined by three points will be zero iff they are collinear (including the degenerate cases of two or all three points being concurrent), i.e....in expanded form
x1( y2 - y3 ) + x2( y3 -
y1) + x3( y1 - y2 ) = 0.
If you know that the points lie on a single line, you may just calculate a dot product, and if 0 ≤ 〈AB, AP〉 ≤ 〈AB, AB〉, then P is between A and B. But note that if P is not exactly on the line, this method may give strange results. But it should be the fastest.
You may also want to calculate 2 values, distance from P to AB and (orthogonal) projection of AP onto AB. The latter is AP_AB = 〈AP, AB〉 / 〈AB, AB〉 (in AB units, so that if AP and AB are collinear, AP = AB * AP_AB), and the former is ρ(P, AB) = ρ(AP, AB * AP_AB) = |AP - AB * AP_AB|. So, if the ρ(P, AB) is small, you decide that if 0 ≤ AP_AB ≤ 1, then P is between A and B
I know this isn't exactly programming related per se, but programmers are the most
probable of all people who will recognize this maybe.
I have the following (X and Y are arrays, both with 3 elements), and I cannot recognize (although it reminds me of a few things, but none quite!) what is being done here. Does it ring any bells for anyone else ?
I gather you can disregard the lower part; the upper should probably give it away ... but I still cannot see it.
At first it reminded me of linear interpolation in 3d space ...
SUBROUTINE TRII(X,Y,XR,YR)
DIMENSION X(3),Y(3)
D=X(1)*(X(2)**2-X(3)**2)+
> X(2)*(X(3)**2-X(1)**2)+
> X(3)*(X(1)**2-X(2)**2)
D1=Y(1)*(X(2)*X(3)**2-X(3)*X(2)**2)+
> Y(2)*(X(3)*X(1)**2-X(1)*X(3)**2)+
> Y(3)*(X(1)*X(2)**2-X(2)*X(1)**2)
D2=Y(1)*(X(2)**2-X(3)**2)+
> Y(2)*(X(3)**2-X(1)**2)+
> Y(3)*(X(1)**2-X(2)**2)
D3=X(2)*(Y(3)-Y(1))+
> X(1)*(Y(2)-Y(3))+
> X(3)*(Y(1)-Y(2))
A=D1/D
B=D2/D
C=D3/D
YR=A+B*XR+C*XR**2
RETURN
END
SUBROUTINE TRIM(X,Y,XR,YR,XM,YM)
DIMENSION X(3),Y(3)
D=X(1)*(X(2)**2-X(3)**2)+
> X(2)*(X(3)**2-X(1)**2)+
> X(3)*(X(1)**2-X(2)**2)
D1=Y(1)*(X(2)*X(3)**2-X(3)*X(2)**2)+
> Y(2)*(X(3)*X(1)**2-X(1)*X(3)**2)+
> Y(3)*(X(1)*X(2)**2-X(2)*X(1)**2)
D2=Y(1)*(X(2)**2-X(3)**2)+
> Y(2)*(X(3)**2-X(1)**2)+
> Y(3)*(X(1)**2-X(2)**2)
D3=X(2)*(Y(3)-Y(1))+
> X(1)*(Y(2)-Y(3))+
> X(3)*(Y(1)-Y(2))
A=D1/D
B=D2/D
C=D3/D
XR=-B/(2.*C)
YR=A+B*XR+C*XR**2
XM=XR
IF(XR.GT.X(1).OR.XR.LT.X(3))XM=X(1)
YM=A+B*XM+C*XM**2
IF(YM.LT.Y(1))XM=X(1)
IF(YM.LT.Y(1))YM=Y(1)
RETURN
END
">" is a continuation sign.
The code run as follows
Routine TRII takes as input the coordinates of three points (x,y) and interpolates a parabola using Lagrange interpolation. Also takes as input the coordinate XR. Returns in YR the value at XR for the interpolating parabola.
I guess the name of the routine comes from "TRI" (Croatian for "three" (points)) and "I" for Interpolation.
Routine TRIM also calculates the same parabola, and returns the minimun value of the function in the interval {X(1),X(3)}.The name comes from "TRI" and "M" (minimum)
(I "really" executed the program) >)
Note that this is FORTRAN code and the parameters are passed by reference, so the results are returned back in the same parameters (very odd!)
Edit
Just for fun, let's run TRII
TRII[X_, Y_, XR_] :=
Module[{D0, D1, D2, D3, A, B, C},
D0 = X[[1]]*(X[[2]]^2 - X[[3]]^2) +
X[[2]]*(X[[3]]^2 - X[[1]]^2) +
X[[3]]*(X[[1]]^2 - X[[2]]^2);
D1 = Y[[1]]*(X[[2]]*X[[3]]^2 - X[[3]]*X[[2]]^2) +
Y[[2]]*(X[[3]]*X[[1]]^2 - X[[1]]*X[[3]]^2) +
Y[[3]]*(X[[1]]*X[[2]]^2 - X[[2]]*X[[1]]^2);
D2 = Y[[1]]*(X[[2]]^2 - X[[3]]^2) +
Y[[2]]*(X[[3]]^2 - X[[1]]^2) +
Y[[3]]*(X[[1]]^2 - X[[2]]^2);
D3 = X[[2]]*(Y[[3]] - Y[[1]]) +
X[[1]]*(Y[[2]] - Y[[3]]) +
X[[3]]*(Y[[1]] - Y[[2]]);
A = D1/D0;
B = D2/D0;
C = D3/D0;
Return[A + B*XR + C*XR^2];];
X = RandomReal[1, 3];
Y = RandomReal[1, 3];
Show[Plot[TRII[X, Y, x], {x, 0, 1}],
ListPlot[Transpose[{X, Y}], PlotMarkers -> Automatic]]
D is the determinant of the matrix:
| x(1) x(1)² 1 |
D = det | x(2) x(2)² 1 |
| x(3) x(3)² 1 |
In D1, the rightmost column has been replaced with Y:
| x(1) x(1)² Y(1) |
D1 = det | x(2) x(2)² Y(2) |
| x(3) x(3)² Y(3) |
In D2, and D3 it's the first and second columns, respectively. Is it easier to recognize now? Looks a lot like using Cramer's rule to solve a linear equation to me.
Edit: To be more precise: (A, B, C) is the solution to the system:
A + x(1)*B + x(1)²*C = Y(1)
A + x(2)*B + x(2)²*C = Y(2)
A + x(3)*B + x(3)²*C = Y(3)
YR is the square of the solution to the quadratic equation (nb, different x!):
C*x² + B*x + A = 0
I feel like this should be obvious now, but I can't quite grasp it...
This code represents a kind of interpolation/quadratic curve fitting on three 2d points together with a way to compute the minimum or maximum value of such a fitted quadratic within the interval itself. I guess that TRII stands for triple (point)-interpolation and TRIM stands for triple (point) minimum or maximum.
To be more precised TRII solves the problem :- find a quadratic curve that passes through the points (x1,y1),(x2,y2) and (x3,y3) in the form Y=A+BX+CX^2 and compute the Y value of the quadratic at the point XR and return as YR. This is basically a way to interpolate smoothly between three 2d points. It is often used to find a better approximation for the max or min value of a set of discrete data points.
All the D, D1, D2, D3 stuff is to solve the matrix equation:
(1 X1 X1^2) *(A) = (Y1)
(1 X2 X2^2) *(B) = (Y2)
(1 X3 X3^2) *(C) = (Y3)
using Cramers rule as mentioned in one of the other comments, D is the matrix determinant and D1, D2, D3 are co-factors.
TRIM again computes the quadratic Y=A+BX+CX^2 and then finds a max/min of this quadratic (XM, YM). This is done by initially finding the point where the quadratic has a turning point: if F(X)=A+BX+CX^2, F'(XR)=B+2*C*XR=0, or XR=-B/2*C, YR=A+BXR+CXR^2. There is then some logic to force the returned XM, YM min or max values to lie within certain bounds.
The code:
XM=XR
.
.
.
IF(YM.LT.Y(1))YM=Y(1)
Is a little weird since if we assume that GT and LT mean greater than and less than respectively then we need to assume that X3'<'X1 otherwise the condition (XR.GT.X(1).OR.XR.LT.X(3)) is trivial and XM,YM are set to X1, Y1.
So X3'<'X1 and the condition says that if the quadratics max/min value is outside the interval (X1,X3) then set (XM,YM) to (X1, Y1) as before. If not then if Y1 is above the min/max value in Y then again set (XM,YM) to (X1, Y1).
It is hard to understand what this means and I suspect the code may be wrong! Any thoughts?
Ivan
I'm not sure what language this is, but it's clear that this is some sort of solver for quadratic equations. The XR and YR expressions are a dead giveaway:
XR = -B / (2.*C)
YR = A + B*XR + C*XR**2
Without knowing what the X(1..3) and Y(1..3) expressions are, however, it's not going to be possible to infer too much more about what the A/B/C coefficients represent, however. Lots of things use quadratic equations -- area of a circle given the radius, intensity of light at a given distance, et cetera. More contextual data is required.
Update: The OP indicated that he can't be too much more specific for secrecy reasons. Here are some hints, though:
What does the subroutine return? How are those results used later on? That may lead to better insights.
It appears that Y(1) is some sort of magic lower bound for the result of this computation. Notice that if YM is less than Y(1), then both XM and YM are set to X(1) and Y(1), respectively.
The "D" expressions look like this, in more natural syntax:
d = x1 * [x2^2 - x3^2] + x2 * [x3^2 - x1^2] + x3 * [x1^1 - x2^2]
d1 = y1 * [x2*x3^2 - x3*x2^2] + y2 * [x3*x1^2 - x1*x3^2] + y3 * [x1*x2^2 - x1*x2^2]
d2 = y1 * [x2^2 - x3^2] + y2 * [x3^2 - x1^2] + y3 * [x1^2 - x2^2]
d3 = x2 * [y3 - y1] + x1 * [y2 - y3] * x3 * [y1 - y2]
This looks very much like some sort of matrix operation; D is almost certainly for "determinant". But there are other things that have the same mathematical relationship.
This is a way to solve linear equation systems, specifically cramers rule. Also have a look at the rule of sarrus. After that, you seem to construct a quadratic equation out of it.