Leetcode:convex Polygon

Source: Internet
Author: User

if  This  3 and at the mosta -10,000 to a. Assume the polygon formed by given points are always a simple polygon (simple polygon definition). In the other words, we ensure that exactly the edges intersect at all vertex, and that edges otherwise don' t intersect E Ach other. Example 1: [[0,0],[0,1],[1,1],[1,0]]answer:true
Explanation:


2: [[0,0],[0,10],[10,10],[10,0],[5,5


Explanation:

Https://discuss.leetcode.com/topic/70706/beyond-my-knowledge-java-solution-with-in-line-explanation

https://discuss.leetcode.com/topic/70664/ C-7-line-o-n-solution-to-check-convexity-with-cross-product-of-adajcent-vectors-detailed-explanation

The key observation for convexity are that vector pi+1-pi all turns to the same direction to Pi+2-pi formed by any 3 Sequentially adjacent vertices, i.e., cross product (PI+1-PI) x (PI+2-PI) does no change sign when traversing sequential Ly along polygon vertices.

Note that for any 2D vectors v1, v2,

    • V1 x v2 = det ([v1, v2])

Which is the determinant of the 2x2 matrix [v1, v2]. And the sign of Det ([v1, v2]) represents the positive z-direction of right-hand system from v1 to v2 . So det ([v1, v2]) ≥0 if and only if v1 turns at most of the degrees counterclockwise to v2< /c11>.

1  Public classSolution {2      Public BooleanIsconvex (list<list<integer>>points) {3         //for each set of three adjacent points A, B, C, find the cross product AB Bc. If the sign of4         //All the same, the angles be all positive or negative (depending on the5         //order in which we visit them) so the polygon is convex.6         BooleanGotnegative =false;7         BooleanGotpositive =false;8         intNumpoints =points.size ();9         intB, C;Ten          for(intA = 0; A < numpoints; a++) { One             //Trick to calc the last 3 points:n-1, 0 and 1. AB = (A + 1)%numpoints; -C = (B + 1)%numpoints; -      the             intCrossproduct = - Crossproductlength ( -Points.get (a). Get (0), Points.get (a). Get (1), -Points.get (b). Get (0), Points.get (b). Get (1), +Points.get (c). Get (0), Points.get (c). Get (1)); -             if(Crossproduct < 0) { +Gotnegative =true; A             } at             Else if(Crossproduct > 0) { -Gotpositive =true; -             } -             if(Gotnegative && gotpositive)return false; -         } -      in         //If We got this far, the polygon is convex. -         return true; to     } +      -     //Return The cross product AB x BC. the     //The Cross product are a vector perpendicular to AB and BC have length | ab| * | bc| * Sin (theta) and *     //With direction given by the right-hand rule. For both vectors in the X-y plane, the result is a $     //vector with X and Y are 0 so the Z component gives the vector ' s length and direction.Panax Notoginseng     Private intCrossproductlength (intAxintAy,intBxintBy,intCx,intCy) -     { the         //Get the vectors ' coordinates. +         intABx = Bx-Ax; A         intABy = by-Ay; the         intBCx = Cx-Bx; +         intBcy = Cy-by ; -      $         //Calculate The Z coordinate of the cross product. $         return(ABX * Bcy-aby *BCx); -     } -}

Leetcode:convex Polygon

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.