Recently the project needs to add the ability to draw polygons freely by mouse click, in order to prevent users from selecting the point of Polygon, combined with the requirements by the following methods:
We know that the convex polygon of any n vertex can be decomposed into a (n-2) triangle, the inner angle of a triangle and 180°, the inner angle of all triangles and (n-2) *180°, which is the same for convex or concave polygons, but for a convex polygon, There is no internal angle greater than outer corner, and concave polygons exist.
BOOL Ishollow (list<vector3> curvelooppoints) {//Use angle and judge bump: the inner angle of the convex polygon and for (n-2) *180°var num = curve Looppoints.count; float anglesum = 0.0f; for (int i = 0; i < num; i++) {Vector3 e1; if (i = = 0) {e1 = Curvelooppoints[num-1]-curvelooppoints[i]; } else {e1 = curvelooppoints[i-1]-curvelooppoints[i]; } VECTOR3 E2; if (i = = num-1) {e2 = Curvelooppoints[0]-curvelooppoints[i]; } else {e2 = curvelooppoints[i + 1]-curvelooppoints[i]; }//Standardized E1. Normalize (); E2. Normalize (); Compute points by float MDOT = Vector3.dot (e1, E2); Calculate the angle of curvature float theta = Mathf.acos (MDOT); Note Calculate the internal angle anglesum + = theta; }//Calculate internal angle and float convexanglesum = (float) ((num-2)) * MATHF.PI; Determine convexity if (Anglesum < (convexanglesum-(num * 0.00001))) {return true;//is concave} return false;//otherwise convex}
C # Judging the concave and convex nature of polygons