maths
Geometry, graphs and interpolation
Loading...
Searching...
No Matches
geometry.hpp
Go to the documentation of this file.
1
15#ifndef GEOMETRY_HPP_
16 #define GEOMETRY_HPP_
17
18#include "Type.hpp"
19#include <cmath>
20#include <limits>
21
37inline bool is_convex(Vector2f A, Vector2f B, Vector2f C, bool clockwise = true) {
38 //calculate the vector from B to A and B to C
39 Vector2f BA = {A.x - B.x, A.y - B.y};
40 Vector2f BC = {C.x - B.x, C.y - B.y};
41 //calculate the cross product of BA and BC
42 double cross = BA.cross(BC);
43
44 // 1. if it's a clockwise direction, then the cross product should be negative to be a convex angle
45 // 2. if it's a counter clockwise direction, then the cross product should be positive to be a convex angle
46 // 180 degree in this case is an exception, it's neither convex nor concave, but we can consider it as concave.
47 if (std::abs(cross) < epsilond)
48 return false; // straight line → neither convex nor concave
49 else if (clockwise) {
50 return cross < 0; // is convex
51 } else {
52 return cross > 0;
53 }
54
55 return clockwise ? (cross < 0) : (cross > 0);
56}
57
74inline bool is_concave(Vector2f A, Vector2f B, Vector2f C, bool clockwise = true) {
75 //if it's not convex, then it's concave
76 //prefere to inverse clockwise rather than inverse the result of is_convex to handle the 180 degree case
77 return is_convex(A, B, C, !clockwise);
78}
79
82#endif /* !GEOMETRY_HPP_ */
bool is_convex(Vector2f A, Vector2f B, Vector2f C, bool clockwise=true)
check if the angle is convex or concave
Definition geometry.hpp:37
bool is_concave(Vector2f A, Vector2f B, Vector2f C, bool clockwise=true)
check if the angle is concave
Definition geometry.hpp:74