197 glm::vec2{rectangle.right(), rectangle.top()}, |
197 glm::vec2{rectangle.right(), rectangle.top()}, |
198 glm::vec2{rectangle.right(), rectangle.bottom()} |
198 glm::vec2{rectangle.right(), rectangle.bottom()} |
199 }; |
199 }; |
200 } |
200 } |
201 |
201 |
202 bool geom::convex(const std::vector<glm::vec3>& polygon) |
202 bool geom::isConvex(const std::vector<glm::vec3>& polygon) |
203 { |
203 { |
204 const int n = polygon.size(); |
204 const int n = polygon.size(); |
205 auto polygonRing = iter::ring(polygon, n); |
205 auto polygonRing = iter::ring(polygon, n); |
206 std::vector<glm::vec3> crosses; |
206 std::vector<glm::vec3> crosses; |
207 crosses.resize(n); |
207 crosses.resize(n); |
208 for (int i = 0; i < n; i += 1) |
208 for (int i = 0; i < n; i += 1) |
209 { |
209 { |
210 crosses[i] = glm::cross(polygonRing[i - 1] - polygonRing[i], polygonRing[i + 1] - polygonRing[i]); |
210 crosses[i] = glm::cross(polygonRing[i - 1] - polygonRing[i], polygonRing[i + 1] - polygonRing[i]); |
211 } |
211 } |
212 for (int i = 0; i < n - 1; i += 1) |
212 return not std::any_of( |
213 { |
213 crosses.begin() + 1, |
214 const float dotp = glm::dot(crosses[0], crosses[i + 1]); |
214 crosses.end(), |
215 if (dotp < 0 or qFuzzyIsNull(dotp)) |
215 [&crosses](const glm::vec3& vector) |
216 { |
216 { |
217 return false; |
217 return glm::dot(crosses[0], vector) < 1e-6; |
218 } |
218 }); |
219 } |
219 } |
220 return true; |
|
221 } |
|