C # Judging the concave and convex nature of polygons

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.