Calculating an angle relative to a reference line - geometry

PLEASE SEE PICTURE -angle relative to baselinewe've been struggling with this for a while. Assuming we have distance between our measurement point P0 and P3-P4 and as also between P0 and our reference line P1-p2. Can we calculate the angle of p3-p4 relative to p1-p2? Once we figure this out, we'll see our to program it for IOS. Thanks for anyone who can point me in the right direction. [PLEASE SEE PICTURE relative angle measurement][2]

If P0 is a known point and P1-P2 and P3-P4 are defined lines in a 2D coordinate system (ie P0 to each line a defined vector) then you can certainly calculate the angle between the two lines. However if all you know is distance from P0 to each line as a function of a point on either line then you cannot calculate the angle distance alone will not give you what you need without some point of reference.

Related

Is the minimal distance of a line to a point always orthongonal to the line

I have a point and a (curved) line. Now I want to find the distance to the line where the direction from the point to the point on the line stands orthogonal on the line.
Intuitively I think that the shortest connection between the closest point on the line and the point is always orthogonal, but I'm not 100% sure that my geometry intuition is correct. Can you confirm that for finding the orthogonal connection between a point and a point on a line it is enough to check the closed point on the line?
What you wrote can be true, subject to conditions.
Your curve must be either closed, or start/end infinitely far away (like the shape of y=1/x or y=x^2). Otherwise, the closest distance can be to an end point of the curve.
The curve must be smooth. For instance, a triangle is not smooth, the normal is not defined at the 3 vertices and the closest distance can be a distance to the vertex. Another example, cubic Bézier splines may contain a singularity where the normal is not defined, see the top right picture:
Again, closest distance might be a distance to that singularity point.
Also, don’t forget it can be multiple points on the curve with orthogonal connection. You gonna have to find all of them, and use the minimum distance found. Moreover, in some cases “all of them” can be “infinitely many”, if the curve contains a piece of a circular arc.

Finding inflection points along arbitrary axis

I'm trying to find the inflection points relative to given axis (green line) given a series of (blue) points that form a path. We can see from the diagram below there are two inflection points (pink lines), or places where the path changes direction relative to the task axis.
The tricky part is that the direction of the axis may change, so it has to work going in any direction. My first attempt takes 2 consecutive points along the path, gets the direction between them, and then compares it to the direction of the axis.
Skipping a few boring steps (like recording previous points), the pseudocode looks like this:
foreach point cur_point in path:
direction = prev_point - cur_point
(normalize direction)
relative_direction = dir_towards_axis - direction
(normalize relative direction)
if the sign of either x or y has changed between relative_direction and prev_relative_direction
# we have found an inflection point
Unfortunately this does not work. Any pointers on where I'm going wrong?
To find needed points, you can detect sign change of cross product between axis and current curve direction (orientation/handedness test).
axis_direction = axis_end - axis_start = B - A
....
direction = cur_point - prev_point
cross = cross_product(direction, axis_direction) =
direction.x * axis_direction.y - direction.y * axis_direction.x
Note: If your curve is continuous and analytic, closed-form formula might exist
Have a look at https://pomax.github.io/bezierinfo/#aligning - the idea is to align your baseline with the x or y axis, by moving every point by {-p1.x, -p1.y} and then rotating all the coordinates that define your curve (provided your curve is invariant to affine transforms!) so that your last coordinate lies on the axis (for which you use the atan2 function to find the angle to rotate by, which virtually all programming languages come with).
Once axis-aligned, you can perform whatever analysis "with respect to the axis" you need done.

What is the reference point for measuring angles in OpenCV?

I'm trying to infer an object's direction of movement using dense optical flow in OpenCV. I'm using calcOpticalFlowFarneback() to get flow coordinates and cartToPolar() to acquire vector angles which would indicate direction.
To interpret the results I need to know the reference point for measuring the angle. I have found this blog post indicating that the range of angles is 360°. That tells me that the angle measurement would go along the lines of the unit circle. I couldn't make out much more than that.
The documentation for cartToPolar() doesn't cover this and my attempts at testing it have failed.
It seems that the angle produced by cartToPolar() is in reference to the unit circle rotated clockwise by 90° centered on the image coordinate starting point in the top left corner. It would look like this.
I came to this conclusion by using the dense optical flow example provided by OpenCV. I replaced the line hsv[...,0] = ang*180/np.pi/2 with hsv[...,0] = ang*180/np.pi to get correct angle conversion from radians. Then I tested a video with people moving from top right to bottom left and vice versa. I sampled the dominant color with GIMP and got RGB values which I converted to HSV values. Hue value corresponds to the angle in degrees.
People moving from top right to bottom left produced an angle of about 300° and people moving the other way round produced an angle of about 120°. This hinted at the way the unit circle is positioned.
Looking at the code, fastAtan32f is used to compute the angles. and that seems to be a atan2 implementation.

Find a line intersecting a known line at right angle, given a point in spherical system

I want to calculate intersecting of a line [x1, y1], [x2, y2] at right angle for given a point [xp, xy] in spherical geometry (the line and the point lays on a sphere - the earth - and are given as latlon).
My question is easy to describe by a picture:
The formula for 'flat' geometric calculation on a plane you can find here for ex.:
https://stackoverflow.com/a/6630923/2797243
You can represent a line that travels around a sphere as a plane; the line is equal to the intersection of the plane and the sphere.
To find the perpendicular line through the point:
Find the plane of your first line
Project your point onto this plane
Calculate a new plane from three points: your original point, the projected point, and the center of the sphere.
The new plane represents the line perpendicular to the first line through the point.
The mathematical correct description is answered below by Pubby.
For my case (calculations with tiny distances, maximum a few kilometers) the approximation based on the Mercator projection is accurate enough.
A great source with a lot of code-examples you can find here:
http://wiki.openstreetmap.org/wiki/Mercator

how to determine polygon rotation angle

I am writing a program (.net) to create a stadium style layout and need to determine the angle of rotation for each polygon compared to the horizontal.
This is so i can construct the contents of the polygon and also rotate this correctly to fit inside.
Given the below image as an example to simulate each variant of the facing direction (indicated by the red line) how could i determine the the rotation angle needed to get the shape to have the red line on top as is already shown by shape 5.
http://i40.tinypic.com/16ifhoo.gif
I have found logic to determine the angle of the points that make up the red line, but I also need to know the rotation to get it back to horizontal.
I'm not sure if i need some central reference point for all polygons to help.
How could I best solve this?
If you know the angle of the red line for some polygon (a, say), then the polygon is on one side or other of that line. So:
Use the average colour of some pixels near the line on both sides to determine which is the case.
If the polygon is above the line, the rotation angle is 180+a.
If the polygon is below the line, the rotation is a.
where above and below correspond to the smaller-angle side and larger-angle sides of the line according to how you measure a.
I would try to calculate the normal vectors on each red line (eg. 0 degrees for polygon 5, 45 degrees for 4, 90 degrees for 3, etc.) and then the angle you need to rotate that normal - and thus the matching polygon - so that the normal "points up" should be very simple.
Unfortunately I don't have the needed formulae available for you off the top of my head, but Googling "normal vector" and/or searching for it on Wikipedia should get you started just fine, I think. Possibly in the direction of the so called 'cross product'.
No central reference point for all polygons should be needed for this (normal direction is not related to absolute coordinates).
sin, cos, tan functions allow you to convert from triangle edge ratio to degrees.
Imagine, one end of red line is at (x1,y1) and other end is at (x2,y2). You can treat red line as hipotenuse of rectangular triangle and use arctan to get degrees.
Ratio between catheti is (x2-x1) / (y2 - y1). Rotation of red line then is arctan((x2-x1) / (y2 - y1)). Watch out for situations when y1-y1 is 0!
Let's try one example from your picture, polygon 6 with coords (55, 65) and (65, 55). Type in google: "arctan((65-55)/(55-65)) in degrees"

Resources