Fast way to get relative position of point and line - math

I have a line and point (2D).
Ok, using Paul Bourke formula I can get the distance of ortogonal projection of point to the line.
( Also I can know if the solution is or not inside the segment of line. )
OK, But I'd like to know if the point is at right or left side relative to the line direction.
I can get the angle between line and ortogonal line using
Math.acos (crossproduct/moduleProduct) but crossproduct is zero so I can't use it...
Any idea ? Have to use trigonometric approach or maybe is there a trick using vector data ?
Th. in advance,

You can calculate 2d vectors cross product and look at its sign.
Given two vectors v1 and v2 you can calculate 2d cross product as (v1.X*v2.Y) - (v1.Y*v2.X)
If you have point (x, y) and line (x0, y0)-(x1, y1) then ((x-x0)*(y1-y0)) - ((y-y0)*(x1-x0)) is 2d vector cross product. It will be negative if the point is in the left hemiplane.

Related

How I can find direction vector of line if I know two angles (azimuth and polar) and one start point(x0,y0,x0)?

I have one start point x0,y0,z0 and two angles. Theta(polar(0-180)) and Phi(azimuth (0-360)). And now I need to find direction of vector of reflected line 3d space box with parameters (4,5,6)
I tried to calculate using geometry formula and use cos and sin, adjacent, opposite. But it gave me wrong answer for my further cycle calculation I suppose I need some equation to write. Some kind of i, j, k. I could not calculate slope as I don’t have enough input.

Get angle between two 2d lines. (With respect to the direction of the lines)

I want to get the angle between two lines with respect to direction of the line. A normal dot product of the 2 vectors of the lines always returns the lowest angle between the two lines, which is not what I'm looking for.
Example image:
Blue lines: The lines I have
Red lines: indication of the direction
Green lines: failed attempt at a curving line that indicates the direction of the rotation I want.
p0~p3: the x, y coordinates of the points of the lines.
a: The angle I am looking to get.
I want to build a function that gives me the correct rotation, use any programming language you prefer.
Maybe there is a more elegant solution to this, but the following works:
Compute the dot product and the cross product of the two vectors.
If the cross product is negative, compute the inner angle (called "alpha") using the dot product, otherwise if the cross product is positive, compute the outer angle as 360°-alpha.
Note that the cross product depends on the order of operands; the above is valid assuming the order as in your drawing.

How to calculate the orientation of an object moving along a hermite curve

I struggle with the orientation of an object I am moving along a hermite curve.
I figured out how to move it at constant speed at also have the tangent of my curve, which would be the forward vector of the moving object. My problem is: How do I know the up and right vector? The easiest way would be to start at a given rotation and then step through the curve always taking the last rotation as a reference for the next one, like in this reference:
Camera movement along a splinepdf
But this would result in an uncontrollable rotation at the end of the spline. What I am trying to do is to create an algorithm which gives you the correct orientation at any point of the curve, without stepping through it. Ideally it would use the orientation of the two controlpoints for the current segment as a reference.
I thought of using some kind of pre-calculated data, which is created from the two orientations of the controlpoints and the current curve segments form, but didn't manage to come up with a solution.
I would be happy to get any answers or just ideas how to approach this problem.
Let C(t) be the camera trajectory, with tangent vector T(t). The tangent vector controls the pitch and the yaw. What you are missing is roll control.
Define an auxiliary trajectory D(t) that "parallels" C(t) and use the vector CD(t). The up vector is given by U(t)=T(t) /\ CD(t) (normalized), and the right vector by U(t) /\ T(t) (normalized).
OK i came up with a solution using frenet frames. I define an orientation for each of my control points, then i calculate a number of points along the spline for each segment. Each points orientation is then calculated using the previous points orientation. The orientatin of the first point equals the orientation of the control point.
Here is a very nice description of the procedure.
After calculating each points orientation, you can interpolate them so the last points orientation matches the orientation of the next controlpoint.

Area of polygon - clockwise

From this threa Determine the centroid of multiple points I came to know that area of polygon can also be negative if we start in clockwise direction. Why it can be negative?
It is a product of the maths. You can use the sign if you wish to, or use an absolute value for the area.
You often get a similar effect with dot products and cross products. This can be effective, for example determining the orientation of a polygon in 3d (does the 'outside' side of the polygon face towards me or away from me?)
The sign tells you some useful information, that you can either use or discard. For example, what is the area below the curve sin(x) and above the x axis, for x over the interval [0,pi]. Yes, this is simply a definite integral. In MATLAB, I'd do it as:
>> quad(#sin,0,pi)
ans =
2
But suppose I computed that same definite integral, with limits of integration [pi,0]? Clearly, we would get -2.
>> quad(#sin,pi,0)
ans =
-2
And of course this makes sense. In either case, we can assure that we get the positive area by ignoring the sign. But the sign tells us something in that integral.
The sign computed for the area of a polygon is indeed useful in some problems. In the case of a triangle, a cross product will yield a vector that points in the direction orthogonal to the plane of the triangle containing the vectors. The magnitude of the vector will be twice the area of that triangle. Note that this vector can point in one of two directions orthogonal to a given plane, which one is indicated by the right hand rule. You can think of that sign as indicating which direction the vector pointed.

How can I find the arctan of a right triangle using only the hypotenuse?

Okay, so I need to make C go the shortest path from A to B. A to B is the hypotenuse of my right triangle, and I need to give C the arctan of said triangle. How do I do this, and does the formula have a name?
It's not clear exactly what you're asking, but I think you're trying to find the angle of the A-B line. I'm going to make the assumption that you know, or can figure out the (x,y) coordinates of both A and B, because otherwise you won't be able to solve the issue.
It sounds like you've outlined the majority of the solution...the angle will be equal to the arctan of the (y/x) distance. So if we consider A(y) to be the y coordinate of A, then you're looking at something like:
arctan ((A(y) - B(y)) / (A(x) - B(x)))
Does that help? Or are you looking for something slightly different?
EDIT: One thing to be aware of is the order in which you consider the terms (whether you're going from A to B or vice versa), etc. You will have to be thoughtful about this or you could end up with some sign problems.
Most systems have Arctan2(dy, dx) which gives you an angle in a full circle (and takes care of verticals), so you would say Arctan2((By - Ay), (Bx - Ax)) to get the direction in radians (counterclockwise from East). For degrees multiply by 360/(2*PI).
Just make sure A != B.
Arctan would result in degrees or radians so your A and B most likely have coordinates like (x, y). Then you do arctan((By - Ay) / (Bx - Ax)) if I remember correctly, here Bx is the x coordinate of B etc.
If A and B do not have coordinates, you cannot get degrees out meaningfully.
If you only have one length and there is no hidden assumption here (like say, one side of the triangle has been normalized): you can't.
An interesting hidden assumption might be:
All distances are integers
The triangle is at least as long as it is tall.
Then the problem is merely hard.
If A and B are points, then the angle you want is presumable the one taken to the x-axis, and you get it by (using the fortranish names):
atan((B.y - A.y)/(B.x - A.x))
or if you have it in your library
atan2((B.y - A.y),(B.x - A.x))
which handles the divide by zero cases neatly...
If A to B is the hypotenuse of your right triangle, A to B will also be the shortest path from A to B because it is a straight line between the points.
You can calculate the arctangent of either non-right angle by dividing the length of the adjacent side by the length of the opposite side because it's the inverse of the tangent. But, with the information you've described, you will be lacking either the numerator or the denominator.
There are an infinite number of right triangles with a hypotenuse of a given length.

Resources