What is the different between both mysql code? - mysql-workbench

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
Equilateral: It's a triangle with sides of equal length.
Isosceles: It's a triangle with sides of equal length.
Scalene: It's a triangle with sides of differing lengths.
Not A Triangle: The given values of A, B, and C don't form a triangle.
My two different solutions are;
First Code:
Select CASE when (A >= B+C) or (B >= A+C) or (C >= B+A) then 'Not A Triangle' when A=B and B=C then 'Equilateral' when A=B or B=C or A=C then 'Isosceles' ELSE 'Scalene' END from TRIANGLES;
Outpute:
Equilateral
Equilateral
Isosceles
Equilateral
Isosceles
Equilateral
Scalene
Not A Triangle <- different
Scalene
Scalene
Scalene
Not A Triangle
Not A Triangle
Scalene
Equilateral
Second Code:
SELECT
CASE WHEN A <= B+C AND B <= A+C AND C <= A+B THEN CASE WHEN A = B AND B = C THEN 'Equilateral' when A=B or B=C or A=C then 'Isosceles' ELSE 'Scalene' END ELSE 'Not A Triangle' END FROM TRIANGLES;
Output:
Equilateral
Equilateral
Isosceles
Equilateral
Isosceles
Equilateral
Scalene
Isosceles <- different
Scalene
Scalene
Scalene
Not A Triangle
Not A Triangle
Scalene
Equilateral
Note: First one is correct and second one is incorrect. Can anyone tell me what is differnt in both code in mysql? I am very confused. Any example will be helpful.
thanks!!

Related

determine whether two points with known normals are facing each other or not (matlab)

I am trying to find a solution to the following problem but it is not clear how to solve it. Imagine that I have the following points in the space as in the image below:
If I consider that my only known information is the point positions and their normals I would like to determine whether two points (considering as a reference the position of the first point) are facing each other or not. For example from the above image for points a, b, c, d and e I have:
Point a faces points c and e but not points b and d.
Point b faces points d and e but not points a and c.
Point c faces point a but not points b, d and e.
Point d faces points b and e but not points a and c.
and finaly
Point e faces points a, b and d but not point c.
My first though was to play with the signed angles between the two normal vectors of each pair by using the solutions proposed here but this works for some pairs while not for others. The idea regarding what two point are facing each other is that if we consider a point as the origin then it faces another point if the other point is within the origin point's 180 degrees field of view and its normal vector is going inwards (kind of "towards") the origin point.
Any ideas what could help.
Thanks.
Update:
to try to be a bit more clear and answer some of the comments below. In principle its point in the space corresponds to the centroid of a face. However, I do not have this information beforehand (i.e. that each point corresponds to the center of a face, or the list of faces and their vertices). So in a higher level, if we were dealing with faces the problem would be how to determine if two faces are visible to each other or not but as I said the only information that I have now are the actual points in the space and their normals.
Sample points:
a = [26415.3720833199 11986.0504166605 739];
na = [0 0 1];
b = [27263.8100000023 11103.1983333336 1512.50000000021];
nb = [0.102791963903622 -0.994702876318771 0];
c = [28059.5700000001 11185.4316666667 962.499999999998];
nc = [-0.102791963903623 0.994702876318771 -9.06557542353252e-16];
d = [26606.7112499615 10390.7487916521 739];
nd = [0 0 1];
e = [27792.4499999996 9225.36499999984 2782];
ne = [0 0 -1];
You can solve your problem with a few simple dot products...
Based on your description, a point b is within the field of view (FOV) of another point a if the angle between the normal of a (i.e. na) and a vector going from a to b is less than or equal to 90 degrees. As described here, the angle can be found by taking the dot product of b-a and na, dividing by the length of b-a (and assuming the length of na is already 1), and taking the inverse cosine of the result. Putting it into an anonymous function, you have:
isInFOV = #(b, a, na) (acosd(dot(b-a, na)./norm(b-a)) <= 90);
You can then define a point b as "pointing toward" another point a if the component of nb (the normal of b) running along the vector going from b to a is positive. As described here, the component can be found by taking the dot product of a-b and nb and dividing by the length of a-b (and assuming the length of nb is already 1). Putting it into an anonymous function, you have:
isPointingToward = #(b, nb, a) (dot(a-b, nb)./norm(a-b) > 0);
We can then define whether a point a is "facing" another point b as:
isFacing = #(a, na, b, nb) (isInFOV(b, a, na) && isPointingToward(b, nb, a));
Note that I used the logical short circuit AND operator && since isPointingToward doesn't need to be evaluated if isInFOV already evaluates to false.
Vectorizing
You can reformulate the above equations to vectorize the operations, using functions like bsxfun or replacing a call to dot with standard matrix operations. This will allow you to check which points in a set a given point is facing. A vectorized version of the function isFacing is given below:
function index = isFacing(a, na, b, nb)
V = bsxfun(#minus, b, a); % Compute b-a for all b
V = bsxfun(#rdivide, V, sqrt(sum(V.^2, 2))); % Normalize each row
index = (acosd(V*na.') <= 90); % Find points in FOV of a
index(index) = (sum(V(index, :).*nb(index, :), 2) < 0); % Of those points in FOV,
% find those pointing
% towards a
end
Example
Using the sample data in the question:
pointMat = [26415.3720833199 11986.0504166605 739; ... % Point a
27263.8100000023 11103.1983333336 1512.50000000021; ... % Point b
28059.5700000001 11185.4316666667 962.499999999998; ... % Point c
26606.7112499615 10390.7487916521 739]; % Point d
normalMat = [0 0 1; ...
0.102791963903622 -0.994702876318771 0; ...
-0.102791963903623 0.994702876318771 -9.06557542353252e-16; ...
0 0 1];
p = [27792.4499999996 9225.36499999984 2782]; % Point e
np = [0 0 -1];
>> isFacing(p, np, pointMat, normalMat)
ans =
4×1 logical array
1 % Facing a
1 % Facing b
0 % Not facing c
1 % Facing d

Find intersection points between a segment and a convex polygon

I'm trying to build a function in MATLAB, in which you input a segment (defined by two points) and a polygon (4-sides) by indicating on an array its vertices.
I have the following code:
function intersection = intersectSegmentPolygon (s, p)
% Create a vector with X coords of vertices and same for Y coords.
xv = [p(1,1) p(2,1) p(3,1) p(4,1)];
yv = [p(1,2) p(2,2) p(3,2) p(4,2)];
% Read the segment
x = [s.A(1) s.B(1)];
y = [s.A(2) s.B(2)];
[in,on] = inpolygon(x,y,xv,yv);
% Return vectors containing the coords of the intersecting points
intersection = [x(on), y(on)];
I am intersted in obtaining the points at the position on (the intersecting points) but, obviously the function is only checking the points A and B (the initial and final coordinates of the segment), what can I do in order to check all the points contained on the segment AB? Thank you.
Use the parametric equation of the line segment, P = (1-t) A + t B, with 0<=t<=1.
Find the intersections between the polygon edges and the line of support of the segment, expressing the position of the intersection in terms of t (momentarily ignore the constraint on t).
You will find 0 or 2 intersections, not more, hence 0 or 2 values of t, forming an interval. The solution is given by the intersection of this interval with the interval [0,1], an elementary 1D problem.

how do i know position of point, perpendicular to line

There are 2 Point A(a,b,c), B(d,e,f)
There is the line AB, connecting A and B
Line CC' is perpendicular to line AB
I want to Point C 's position on line CC'
If length of line CC' is 1, what is C 's position?
How do I calculate C's position with unity?
A : Vector3(a,b,c)
B : Vector3(d,e,f)
AC' length = BC' length
CC' length = 1
AB is perpendicular to CC'
-> C Vector3(?,?,?)
As you describe it, the problem is undetermined. There are infinitely many solutions for C because you are working in 3D space. To visualize, imagine the point C orbiting around the axis AB. Regardless of where C is in its orbit, it will always be orthogonal to AB and length(CC') = 1. You need to further constrain the problem.
The Mathematics
First, we calculate the point C'. To do this, we take the vector from A to B, which is AB = B - A. Then, to get to C' we just travel half the distance of AB from point A:
C' = A + AB/2
Now we need to find a vector orthogonal to AB. Here we encounter the problem that I described initially. There are infinitely many such vectors so we need to further constrain the problem. Suppose that we can choose a vector v that is not colinear to the lines AB or C'C. Now we can find C'C by finding a vector orthogonal to both AB and v.
We know that the cross-product of two linearly independent vectors produces a vector that is orthogonal to both vectors. So all that is left to do is normalize the result so that the length is 1:
C'C = normalize(AB x v)
Finally, we can find point C by traveling from C' along the vector C'C:
C = C' + C'C
Unity Code
Here, I provide some untested code that simply implements the mathematics described above. I'm not awfully familiar with Unity so it is quite possible that there exists some built-in functions that would relieve some of the work:
Vector3 v = new Vector3(0, 0, 1); // Choose this as you wish
Vector3 AB = B - A;
Vector3 C_prime = A + AB / 2;
Vector3 C = C_prime + Vector3.Normalize(Vector3.cross(AB, v));

Efficient way to compute coincidence of space and time

Given a 3-dimensional polyline P = {(x1, y1, t1), ..., (xn, yn, tn)} and another polyline Q = {(x1, y1, t1), ..., (xm, ym, tm)} (m not necessarly equal to n, so polylines could have different length), coincidence in space and time occurs when the trajectories of moving objects P and Q, have some timing and positioning in common (Point A, as seen in example figure is a coincidence point cause (xa, ya, ta)==(xb, yb, tb) obviously coincidence point could be a point outside of initial sets of point)
The concept is quite simple and visual perspective easily identify where colocation happen. Hardest part is how to realize an algorithm that efficiently compute coincidence and return the calculated (remember: point could be outside given sets of points) x, y coords and time t of where colocation happens!! This algorithm will be developed in Matlab so I have all necessary to rapidly work.
Best regards
assuming that x, y, z are functions of t for all segments of each polyline, here's a brute-force start. in 4 dimensions: P has segments p1 from (x_start(t), y_start(t), z_start(t), t) to (x_end(t), y_end(t), z_end(t), t), and similarly Q
for each segment p of P
for each segment q of Q
if p intersects q (in 4 dimensions)
output intersection point
the intersection condition is:
there exists alpha and beta in [0,1] where alpha * px_start(t) + (1 - alpha) * (px_end(t) - px_start(t)) = beta * qx_start(t) + (1 - beta) * (qx_end(t) - qx_start(t)) and 2 more similar conditions for y and z
solvability of the intersection condition depends on what are the functions x(t), y(t), z(t) -- linear? polynomials? etc.

minimum distance to the center of a sphere using matlab

Given 2 points A and B belonging to a sphere with a given Radius R.
I want to find the sphere whose center has the minimum distance to a given point G.
Thanks
The centers of a sphere defined by two points and a radius is a circle. You can connect C (the center of the circle) and G and create a 90° projection on the circle plane. The minimum distance is where the projection intersects the circle tangent by 90°. There are two solutions. You have to take the smaller one.
The point C you want is in the plane that contains A, B, and G. You compute
AG = G - A;
BG = G - B;
N = cross(AG, BG);
N = N / norm( N ); % the normal to the plane
Now you solve for C in this plane. Three equations:
dot((C-G), N)=0;
sqrt(sum(A-C).^2) = R;
sqrt(sum(B-C).^2) = R;
Three unknowns are the three elements of C. You end up with two solutions, so compute the distance to G and pick the closer one.