Signed area of a triangle
Signed area of a triangle
Definition
Let three points be given: , , .
Signed area of a triangle
Let three points be given: , , .
Consider the triangle .
Signed area is the usual area of a triangle, but:
If we can compute the signed area, then we automatically can:
Compare the directions of two segments:
It turns out it is enough to compute one expression:
This number:
This is the doubled signed area.
Because the actual area of the triangle equals:
But in all turn-direction checks, division by 2 is not required, so almost always they use exactly the value .
long long triangle_area_2(
long long x1, long long y1,
long long x2, long long y2,
long long x3, long long y3
) {
return (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
}
double triangle_area(
long long x1, long long y1,
long long x2, long long y2,
long long x3, long long y3
) {
return abs(triangle_area_2(x1, y1, x2, y2, x3, y3)) / 2.0;
}
bool clockwise(
long long x1, long long y1,
long long x2, long long y2,
long long x3, long long y3
) {
return triangle_area_2(x1, y1, x2, y2, x3, y3) < 0;
}
bool counter_clockwise(
long long x1, long long y1,
long long x2, long long y2,
long long x3, long long y3
) {
return triangle_area_2(x1, y1, x2, y2, x3, y3) > 0;
}
bool collinear(
long long x1, long long y1,
long long x2, long long y2,
long long x3, long long y3
) {
return triangle_area_2(x1, y1, x2, y2, x3, y3) == 0;
}