How do I detect triangle and rectangle intersection? - 2d-games

I am developing an Android game and I would like to know how to detect the collision of a rectangle knowing its position (x and y), width and height and a triangle knowing x,y, width and height. Triangles are always right triangles as the result of dividing a rectangle through its diagonal so the (x,y) parameter will be a position in the hypotenuse, not the center of the triangle.
Any help would be appreciated!

I´ve finally done this using the function intersect for two lines. Lines are defined with initial point (x,y) and final point (x,y)
// a1 is line1 start, a2 is line1 end, b1 is line2 start, b2 is line2 end
static boolean intersects(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2)
{
Vector2 intersection = Vector2.Zero();
Vector2 b = Vector2.Subtract(a2,a1);
Vector2 d = Vector2.Subtract(b2,b1);
float bDotDPerp = b.getX() * d.getY() - b.getY() * d.getX();
// if b dot d == 0, it means the lines are parallel so have infinite intersection points
if (bDotDPerp == 0)
return false;
Vector2 c = Vector2.Subtract(b1,a1);
float t = (c.getX() * d.getY() - c.getY() * d.getX()) / bDotDPerp;
if (t < 0 || t > 1)
return false;
float u = (c.getX() * b.getY() - c.getY() * b.getX()) / bDotDPerp;
if (u < 0 || u > 1)
return false;
intersection = Vector2.Sum(a1,Vector2.Multiply(b,t));
return true;
}
To know if a triagle intersects a rectangle you check the intersection of every line from the triangle with every line from the rectangle with function above.

Have a look at the great polygon intersection library. There is a C++ version that you may be able to use on Android.
Another possibility is to rasterize an image of the rectangle and another image with the triangle and finally check pixel by pixel if there is any intersection between the two images.

Related

Finding a specific point on an OBB (Oriented Bounding Box)

Hello, I've got a question which I cannot solve so I need a bit help.
In the picture above you can see an Oriented Bounding Box specified by 4 points (A, B, C, D). There is also a point in space called P. If I cast a ray from P against the OBB the ray is going to intersect the OBB at some point. This point of intersection is called Q in the picture. By the way the ray is always going to be x-axis aligned which means its directional vector is either (1, 0) or (-1,0) if normalized. My goal is to find the point of intersection - Q. Is there a way (if possible computationaly inexpensive) to do so?
Thanks in advance.
One way to do this is to consider each side of the bounding box to be a linear equation of the form y = ax + b, where a is the slope and b is the y-intercept. Then consider the ray from P to be an equation of the form y = c, where c is a constant. Then compare this equation to each of the four other equations to see where it intersects each one. One of these intersections will be our Q, if a Q exists; it's possible that the ray will miss the bounding box entirely. We will need to do a few checks:
Firstly, eliminate all potential Q's that are on the wrong side of P.
Secondly, check each of the four intersections to make sure they are within the bounds of the lines that they represent, and eliminate the ones that are not.
Finally, if any potential Q's remain, the one closest to P will be our Q. If no potential Q's remain, this means that the ray from P misses the bounding box entirely.
For example...
The line drawn from D to B would have a slope equal to (B.y - D.y) / (B.x - D.x) and a y-intercept equal to B.y - B.x * slope. Then the entire equation is y = (B.y - D.y) / (B.x - D.x) * x + B.y - B.x * (B.y - D.y) / (B.x - D.x). Set this equation equal to y = P.y and solve for x:
x = (P.y - B.y + B.x*(B.y - D.y)/(B.x - D.x))*(B.x - D.x)/(B.y - D.y)
The result of this equation will give you the x-value of the intersection. The y-value is P.y. Do this for each of the other 3 lines as well: A-B, A-C, C-D. I will refer to these intersections as Q(D-B), Q(A-B), Q(A-C), and Q(C-D) respectively.
Next, eliminate candidate-Q's that are on the wrong side of P. In our example, this eliminates Q(A-B), since it is way off to the right side of the screen. Mathematically, Q(A-B).x > P.x.
Then eliminate all candidate-Q's that are not on the line they represent. We can do this check by finding the lowest and highest x-values and y-values given by the two points that represent the line. For example, to check that Q(A-C) is on the line A-C, check that C.x <= Q(A-C).x <= A.x and C.y <= Q(A-C).y <= A.y. Q(A-C) passes the test, as well as Q(D-B). However, Q(C-D) does not pass, as it is way off to the left side of the screen, far from the box. Therefore, Q(C-D) is eliminated from candidacy.
Finally, of the two points that remain, Q(A-C) and Q(D-B), we choose Q(D-B) to be our winner, because it is closest to P.
We now can say that the ray from P hits the bounding box at Q(D-B).
Of course, when you implement this in code, you will need to account for divisions by zero. If a line is perfectly vertical, there does not exist a point-slope equation of the line, so you will need to create a separate formula for this case. If a line is perfectly horizontal, it's respective candidate-Q should be automatically eliminated from candidacy, as the ray from P will never touch it.
Edit:
It would be more efficient to only do this process with lines whose two points are on vertically opposite sides of point P. If both points of a line are above P, or they are both below P, they would be eliminated from candidacy from the beginning.
Find the two sides that straddle p on Y. (Test of the form (Ya < Yp) != (Yb < Yp)).
Then compute the intersection points of the horizontal by p with these two sides, and keep the first to the left of p.
If the ray points to the left(right) then it must intersect an edge that connects to the point in the OOB with max(min) x-value. We can determine which edge by simply comparing the y-value of the ray with the y value of the max(min) point and its neighbors. We also need to consider OBBs that are actually axis-aligned, and thus have two points with equal max(min) x-value. Once we have the edge it's simple to confirm that the ray does in fact intersect the OBB and calculate its x-value.
Here's some Java code to illustrate (ideone):
static double nearestX(Point[] obb, int y, int dir)
{
// Find min(max) point
int n = 0;
for(int i=1; i<4; i++)
if((obb[n].x < obb[i].x) == (dir == -1)) n = i;
// Determine next or prev edge
int next = (n+1) % 4;
int prev = (n+3) % 4;
int nn;
if((obb[n].x == obb[next].x) || (obb[n].y < y) == (obb[n].y < obb[next].y))
nn = next;
else
nn = prev;
// Check that the ray intersects the OBB
if(Math.abs(y) > Math.abs(obb[nn].y)) return Double.NaN;
// Standard calculation of x from y for line segment
return obb[n].x + (y-obb[n].y)*(obb[nn].x-obb[n].x)/(obb[nn].y-obb[n].y);
}
Test:
public static void main(String[] args)
{
test("Diamond", new Point[]{p(0, -2), p(2, 0), p(0, 2), p(-2,0)});
test("Square", new Point[]{p(-2, -2), p(2, -2), p(2, 2), p(-2,2)});
}
static void test(String label, Point[] obb)
{
System.out.println(label + ": " + Arrays.toString(obb));
for(int dir : new int[] {-1, 1})
{
for(int y : new int[] {-3, -2, -1, 0, 1, 2, 3})
System.out.printf("(% d, % d) = %.0f\n", y , dir, nearestX(obb, y, dir));
System.out.println();
}
}
Output:
Diamond: [(0,-2), (2,0), (0,2), (-2,0)]
(-3, -1) = NaN
(-2, -1) = 0
(-1, -1) = 1
( 0, -1) = 2
( 1, -1) = 1
( 2, -1) = 0
( 3, -1) = NaN
(-3, 1) = NaN
(-2, 1) = 0
(-1, 1) = -1
( 0, 1) = -2
( 1, 1) = -1
( 2, 1) = 0
( 3, 1) = NaN
Square: [(-2,-2), (2,-2), (2,2), (-2,2)]
(-3, -1) = NaN
(-2, -1) = 2
(-1, -1) = 2
( 0, -1) = 2
( 1, -1) = 2
( 2, -1) = 2
( 3, -1) = NaN
(-3, 1) = NaN
(-2, 1) = -2
(-1, 1) = -2
( 0, 1) = -2
( 1, 1) = -2
( 2, 1) = -2
( 3, 1) = NaN

How to calculate coordinates of third point in a triangle (2D) knowing 2 points coordinates, all lenghts and all angles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have a triangle and I know the coordinates of two vertices: A=(x1,y1),B=(x2,y2)
All the angles: ABC=90∘,CAB=30∘ and BCA=60∘ and all the edge lengths. How can I find the coordinates of the third vertex C=(x3,y3)?
I know there are two solutions (I want both).
You know p1 and p2. You know the internal angles.
Make a ray from p1 trough p2, and rotate it CW or CCW 30° around p1.
Make a line trough p1 and p2, and rotate it 90° around p2.
Calculate the intersections.
You get the points:
x3 = x2 + s*(y1 - y2)
y3 = y2 + s*(x2 - x1)
and
x3 = x2 + s*(y2 - y1)
y3 = y2 + s*(x1 - x2)
where s = 1/sqrt(3) ≈ 0.577350269
In a 30-60-90 right triangle, smallest leg (the smallest side adjacent the 90 degree angle) has length of 1/2 of the hypotenuse (the side opposite to 90 degree angle), so since you have the side lengths, you can determine which leg is the line segment AB.
From that you deduce where do the angles go.
Then to compute the coordinate you just need to pick the point on the circle of the radius with the correct radius length at the correct angle.
Two solutions come from measuring the angle clock-wise or counter-clockwise, and result in symmetrical triangles, with the edge AB being the line of symmetry.
Since you already have given the angles, compute the length of AB via quadratic formula
L(AB) = Sqrt[(x1-x2)^2 + (y1-y2)^2].
Now, let x = L(AC) = 2*L(BC) so since it is the right triangle,
L(AC)^2 = L(BC)^2 + L(AB)^2,
x^2 = (0.5x)^2 + L(AB)^2, so L(AB) = x*Sqrt[3]/2,
and since you already computed L(AB) you now have x.
The angle of the original AB is a = arctan([y2-y1]/[x2-x1]).
Now you can measure 30 degrees up or down (use a+30 or a-30 as desired)
and mark the point C on the circle (centered at A) of radius x (which we computed above) at the angle a +/- 30.
Then, C has coordinates
x3 = x1 + x*cos(a+30)
y3 = y1 + x*sin(a+30)
or you can use (a-30) to get the symmetrical triangle.
Here is the code to return points of full polygon if two points and number of sides are provided as input.
This is written for Android(Java) and the logic can be re-used for other languages
private static final float angleBetweenPoints(PointF a, PointF b) {
float deltaY = b.y - a.y;
float deltaX = b.x - a.x;
return (float) (Math.atan2(deltaY, deltaX));
}
private static PointF pullPointReferenceToLineWithAngle(PointF a, PointF b,
float angle) {
float angleBetween = angleBetweenPoints(b, a);
float distance = (float) Math.hypot(b.x - a.x, b.y - a.y);
float x = (float) (b.x + (distance * Math.cos((angleBetween + angle))));
float y = (float) (b.y + (distance * Math.sin((angleBetween + angle))));
return new PointF(x, y);
}
private static List<PointF> pullPolygonPointsFromBasePoints(PointF a,
PointF b, int noOfSides) {
List<PointF> points = new ArrayList<>();
points.add(a);
points.add(b);
if (noOfSides < 3) {
return points;
}
float angleBetweenTwoSides = (float) ((((noOfSides - 2) * 180) / noOfSides)
* Math.PI / 180);
for (int i = 3; i <= noOfSides; i++) {
PointF nextPoint = pullPointReferenceToLineWithAngle(
points.get(i - 3), points.get(i - 2), angleBetweenTwoSides);
points.add(nextPoint);
}
return points;
}
Usage is onDraw method:
PointF a = new PointF(100, 600);
PointF b = new PointF(300, 500);
int noOfSides = 3;
List<PointF> polygonPoints = pullPolygonPointsFromBasePoints(a, b,
noOfSides);
drawPolyPoints(canvas, noOfSides, polygonPoints);
This is a right angled triangle. The angle ABC is 90 degrees, so calculate the vector joining A to B and call this AA and normalise it:
AA = (x2-x1,y2-y1) / |(x2-x1,y2-y1)|
A unit vector perpendicular to AA is given by
BB = (-(y2-y1),x2-x1) / |(x2-x1,y2-y1)|
Because AC is perpendicular to AB all you can obtain your first point P1 as
P1 = (x2,y2) + K * BB
where K is the scalar value equal to the length of side AC (which you say you already know in the question). Your second solution point P2 is then simply given by going in the negative BB direction
P2 = (x2,y2) - K * BB

How to adjust player sprite speed correctly? (Basically a math question?)

Background: I have a bird view's JavaScript game where the player controls a space ship by touching a circle -- e.g. touch to the left of the circle center, and the ship will move left, touch the top right and it will move to the top right and so on... the further away from the circle center of pseudo joystick, the more speed in that direction. However, I'm not directly adjusting the ship's speed, but rather set a targetSpeed.x and targetSpeed.y value, and the ship will then adjust its speed using something like:
if (this.speed.x < this.targetSpeed.x) {
this.speed.x += this.speedStep;
}
else if (this.speed.x > this.targetSpeed.x) {
this.speed.x -= this.speedStep;
}
... and the same for the y speed, and speedStep is a small value to make it smoother and not too abrupt (a ship shouldn't go from a fast leftwards direction to an immediate fast rightwards direction).
My question: Using above code, I believe however that the speed will be adjusted quicker in diagonal directions, and slower along the horizontal/ vertical lines. How do I correct this to have an equal target speed following?
Thanks so much for any help!
var xdiff = targetSpeed.x - speed.x;
var ydiff = targetSpeed.y - speed.y;
var angle = Math.atan2(ydiff, xdiff);
speed.x += speedStep * Math.cos(angle);
speed.y += speedStep * Math.sin(angle);
Assuming you already checked that the touch is inside the circle, and that the edge of the circle represents max speed, and that the center of the circle is circleTouch == [0, 0]
In some C++-like pseudo code:
Scalar circleRadius = ...;
Scalar maxSpeed = ...;
Scalar acceleration = ...;
Vector calculateTargetSpeed( Vector circleTouch ) {
Vector targetSpeed = maxSpeed * circleTouch / circleRadius;
return targetSpeed;
}
Vector calculateNewSpeed( Vector currentSpeed, Vector targetSpeed ) {
Vector speedDiff = targetSpeed - currentSpeed;
Vector newSpeed = currentSpeed + acceleration * normalized(speedDiff);
return newSpeed;
}
// Divide v by its length to get normalized vector (length 1) with same x/y ratio
Vector normalized( Vector v ) {
return v / length(v);
}
// Pythagoras for the length of v
Scalar length( Vector v ) {
Scalar length = sqrt(v.x * v.x + v.y * v.y); // or preferably hypot(v.x, v.y)
return length;
}
This is just off the top of my head, and i haven't tested it. The other answer is fine, i just wanted to give an answer without trigonometry functions. :)

Detection of Triangle Collision in 2D Space

How can I programmatically detect whether or not two triangles touch each other, given their vertices on a 2D coordinate plane? This includes touching points or edges, as well as if one triangle is completely inside the other one.
You can prove that the two triangles do not collide by finding an edge (out of the total 6 edges that make up the two triangles) that acts as a separating line where all the vertices of one triangle lie on one side and the vertices of the other triangle lie on the other side. If you can find such an edge then it means that the triangles do not intersect otherwise the triangles are colliding.
Here is a Matlab implementation of the triangle collision function. You can find the theory of the sameside function here: http://www.blackpawn.com/texts/pointinpoly/default.html
function flag = triangle_intersection(P1, P2)
% triangle_test : returns true if the triangles overlap and false otherwise
% P1, P2: a 3 by 2 array (each), describing the vertices of a triangle,
% the first column corresponds to the x coordinates while the second column
% corresponds to the y coordinates
function flag = sameside(p1,p2,a,b)
% sameside : returns true if the p1,p1 lie on same sides of the
% edge ab and false otherwise
p1(3) = 0; p2(3) = 0; a(3) = 0; b(3) = 0;
cp1 = cross(b-a, p1-a);
cp2 = cross(b-a, p2-a);
if(dot(cp1, cp2) >= 0)
flag = true;
else
flag = false;
end
end
% Repeat the vertices for the loop
P1(4:5,:) = P1(1:2,:);
P2(4:5,:) = P2(1:2,:);
flag = true;
% Testing all the edges of P1
for i=1:3
if(~sameside(P1(i,:), P2(1,:), P1(i+1,:), P1(i+2,:)) ...
&& sameside(P2(1,:), P2(2,:), P1(i+1,:), P1(i+2,:)) ...
&& sameside(P2(2,:), P2(3,:), P1(i+1,:), P1(i+2,:)))
flag = false; return;
end
end
% Testing all the edges of P2
for i=1:3
if(~sameside(P2(i,:), P1(1,:), P2(i+1,:), P2(i+2,:)) ...
&& sameside(P1(1,:), P1(2,:), P2(i+1,:), P2(i+2,:)) ...
&& sameside(P1(2,:), P1(3,:), P2(i+1,:), P2(i+2,:)))
flag = false; return;
end
end
end
In short, Hassan's answer is fastest.
https://jsfiddle.net/eyal/gxw3632c/
This is the fastest code in javascript:
// check that all points of the other triangle are on the same side of the triangle after mapping to barycentric coordinates.
// returns true if all points are outside on the same side
var cross2 = function(points, triangle) {
var pa = points.a;
var pb = points.b;
var pc = points.c;
var p0 = triangle.a;
var p1 = triangle.b;
var p2 = triangle.c;
var dXa = pa.x - p2.x;
var dYa = pa.y - p2.y;
var dXb = pb.x - p2.x;
var dYb = pb.y - p2.y;
var dXc = pc.x - p2.x;
var dYc = pc.y - p2.y;
var dX21 = p2.x - p1.x;
var dY12 = p1.y - p2.y;
var D = dY12 * (p0.x - p2.x) + dX21 * (p0.y - p2.y);
var sa = dY12 * dXa + dX21 * dYa;
var sb = dY12 * dXb + dX21 * dYb;
var sc = dY12 * dXc + dX21 * dYc;
var ta = (p2.y - p0.y) * dXa + (p0.x - p2.x) * dYa;
var tb = (p2.y - p0.y) * dXb + (p0.x - p2.x) * dYb;
var tc = (p2.y - p0.y) * dXc + (p0.x - p2.x) * dYc;
if (D < 0) return ((sa >= 0 && sb >= 0 && sc >= 0) ||
(ta >= 0 && tb >= 0 && tc >= 0) ||
(sa+ta <= D && sb+tb <= D && sc+tc <= D));
return ((sa <= 0 && sb <= 0 && sc <= 0) ||
(ta <= 0 && tb <= 0 && tc <= 0) ||
(sa+ta >= D && sb+tb >= D && sc+tc >= D));
}
var trianglesIntersect4 = function(t0, t1) {
return !(cross2(t0,t1) ||
cross2(t1,t0));
}
I wrote the above fiddle to test a few different techniques and compare the speed. All the techniques are based on some combination of three different tools:
Barycentric point-in-triangle test: Convert a point from x,y space to u,v space where u,v are two sides of the triangle. Then test if the point is inside the triangle (0,0) (0,1) (1,0), which is easy.
Same-side point-in-triangle test: This test tells you if an angle is more or less than 180 degrees. If the triangle is a,b,c and your point is p, you check if the angle pab and bac are both more or both less than 180. You need to do this for ab, bc, and ca. If any are true, the point is outside. This test is slower than barycentric for one point.
Line segment intersection: Check if the line segment a,b intersects line segment c,d. To do that, you find the point where the two lines cross and then check that those lines are in the bounding box of a,b and b,c. This is about as fast as Barycentric.
Those are the tools. Now to find out if triangles intersect, there are 3 ways that I tested:
8 line intersection and 2 point-in-triangle: You only need 8 line intersection and not all 9 because there can't be just 1 intersection. After that, you need 2 point-in-triangle in case 1 triangle is entirely inside the other.
6 line intersection and 4 point-in-triangle: If you draw it out, you can see that you can completely ignore one side of one of the triangles for line intersection and then just do all the other triangles points for point-in-triangle. Because line intersection and barycentric are about the same speed, this isn't much better than #1. But if you used same-side, it will be faster because same side point-in-triangle is slower.
9 same-side point-in-triangle tests: You can use either barycentric or same side. For either, you modify the point-in-triangle to test 3 points at the same time and you don't want to just test that they are all three outside the triangle, you need to test that they are all 3 outside on the same side. Because we're re-using a lot of information, we can save time in calculations. Again, barycentric seems slightly faster than same side here, too.
Use Line Line intersection
https://www.topcoder.com/community/data-science/data-science-tutorials/geometry-concepts-line-intersection-and-its-applications/#line_line_intersection
Also consider the possibility that some vertex might be touching one of the sides of the other triangle.
http://www.blackpawn.com/texts/pointinpoly/default.html
function SameSide(p1,p2, a,b)
cp1 = CrossProduct(b-a, p1-a)
cp2 = CrossProduct(b-a, p2-a)
if DotProduct(cp1, cp2) >= 0 then return true
else return false
function PointInTriangle(p, a,b,c)
if SameSide(p,a, b,c) and SameSide(p,b, a,c)
and SameSide(p,c, a,b) then return true
else return false
Or look at this link and scroll down
http://compsci.ca/v3/viewtopic.php?t=6034

How to calculate both positive and negative angle between two lines?

There is a very handy set of 2d geometry utilities here.
The angleBetweenLines has a problem, though. The result is always positive. I need to detect both positive and negative angles, so if one line is 15 degrees "above" or "below" the other line, the shape obviously looks different.
The configuration I have is that one line remains stationary, while the other line rotates, and I need to understand what direction it is rotating in, by comparing it with the stationary line.
EDIT: in response to swestrup's comment below, the situation is actually that I have a single line, and I record its starting position. The line then rotates from its starting position, and I need to calculate the angle from its starting position to current position. E.g if it has rotated clockwise, it is positive rotation; if counterclockwise, then negative. (Or vice versa.)
How to improve the algorithm so it returns the angle as both positive or negative depending on how the lines are positioned?
Here's the implementation of brainjam's suggestion. (It works with my constraints that the difference between the lines is guaranteed to be small enough that there's no need to normalize anything.)
CGFloat angleBetweenLinesInRad(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {
CGFloat a = line1End.x - line1Start.x;
CGFloat b = line1End.y - line1Start.y;
CGFloat c = line2End.x - line2Start.x;
CGFloat d = line2End.y - line2Start.y;
CGFloat atanA = atan2(a, b);
CGFloat atanB = atan2(c, d);
return atanA - atanB;
}
I like that it's concise. Would the vector version be more concise?
#duffymo's answer is correct, but if you don't want to implement cross-product, you can use the atan2 function. This returns an angle between -π and π, and you can use it on each of the lines (or more precisely the vectors representing the lines).
If you get an angle θ for the first (stationary line), you'll have to normalize the angle φ for the second line to be between θ-π and θ+π (by adding ±2π). The angle between the two lines will then be φ-θ.
This is an easy problem involving 2D vectors. The sine of the angle between two vectors is related to the cross-product between the two vectors. And "above" or "below" is determined by the sign of the vector that's produced by the cross-product: if you cross two vectors A and B, and the cross-product produced is positive, then A is "below" B; if it's negative, A is "above" B. See Mathworld for details.
Here's how I might code it in Java:
package cruft;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* VectorUtils
* User: Michael
* Date: Apr 18, 2010
* Time: 4:12:45 PM
*/
public class VectorUtils
{
private static final int DEFAULT_DIMENSIONS = 3;
private static final NumberFormat DEFAULT_FORMAT = new DecimalFormat("0.###");
public static void main(String[] args)
{
double [] a = { 1.0, 0.0, 0.0 };
double [] b = { 0.0, 1.0, 0.0 };
double [] c = VectorUtils.crossProduct(a, b);
System.out.println(VectorUtils.toString(c));
}
public static double [] crossProduct(double [] a, double [] b)
{
assert ((a != null) && (a.length >= DEFAULT_DIMENSIONS ) && (b != null) && (b.length >= DEFAULT_DIMENSIONS));
double [] c = new double[DEFAULT_DIMENSIONS];
c[0] = +a[1]*b[2] - a[2]*b[1];
c[1] = +a[2]*b[0] - a[0]*b[2];
c[2] = +a[0]*b[1] - a[1]*b[0];
return c;
}
public static String toString(double [] a)
{
StringBuilder builder = new StringBuilder(128);
builder.append("{ ");
for (double c : a)
{
builder.append(DEFAULT_FORMAT.format(c)).append(' ');
}
builder.append("}");
return builder.toString();
}
}
Check the sign of the 3rd component. If it's positive, A is "below" B; if it's negative, A is "above" B - as long as the two vectors are in the two quadrants to the right of the y-axis. Obviously, if they're both in the two quadrants to the left of the y-axis the reverse is true.
You need to think about your intuitive notions of "above" and "below". What if A is in the first quadrant (0 <= θ <= 90) and B is in the second quadrant (90 <= θ <= 180)? "Above" and "below" lose their meaning.
The line then rotates from its
starting position, and I need to
calculate the angle from its starting
position to current position. E.g if
it has rotated clockwise, it is
positive rotation; if
counterclockwise, then negative. (Or
vice versa.)
This is exactly what the cross-product is for. The sign of the 3rd component is positive for counter-clockwise and negative for clockwise (as you look down at the plane of rotation).
One 'quick and dirty' method you can use is to introduce a third reference line R. So, given two lines A and B, calculate the angles between A and R and then B and R, and subtract them.
This does about twice as much calculation as is actually necessary, but is easy to explain and debug.
// Considering two vectors CA and BA
// Computing angle from CA to BA
// Thanks to code shared by Jaanus, but atan2(y,x) is used wrongly.
float getAngleBetweenVectorsWithSignInDeg(Point2f C, Point2f A, Point2f B)
{
float a = A.x - C.x;
float b = A.y - C.y;
float c = B.x - C.x;
float d = B.y - C.y;
float angleA = atan2(b, a);
float angleB = atan2(d, c);
cout << "angleA: " << angleA << "rad, " << angleA * 180 / M_PI << " deg" << endl;
cout << "angleB: " << angleB << "rad, " << angleB * 180 / M_PI << " deg" << endl;
float rotationAngleRad = angleB - angleA;
float thetaDeg = rotationAngleRad * 180.0f / M_PI;
return thetaDeg;
}
That function is working in RADS
There are 2pi RADS in a full circle (360 degrees)
Thus I believe the answear you are looking for is simply the returned value - 2pi
If you are asking to have that one function return both values at the same time, then you are asking to break the language, a function can only return a single value. You could pass it two pointers that it can use to set the value of so that the change can persist after the frunction ends and your program can continue to work. But not really a sensible way of solving this problem.
Edit
Just noticed that the function actually converts the Rads to Degrees as it returns the value. But the same principle will work.

Resources