Leetcode: Convex Polygon

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition).

Note:

There are at least 3 and at most 10,000 points.
Coordinates are in the range -10,000 to 10,000.
You may assume the polygon formed by given points is always a simple polygon (Simple polygon definition). In other words, we ensure that exactly two edges intersect at each vertex, and that edges otherwise don‘t intersect each other.
Example 1:

[[0,0],[0,1],[1,1],[1,0]]

Answer: True
Explanation:


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

Answer: False 
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 is that vector pi+1-pi always 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 not change sign when traversing sequentially along polygon vertices.

Note that for any 2D vectors v1v2,

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

which is the determinant of 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 180 degrees counterclockwise to v2.

 1 public class Solution {
 2     public boolean isConvex(List<List<Integer>> points) {
 3         // For each set of three adjacent points A, B, C, find the cross product AB · BC. If the sign of
 4         // all the cross products is the same, the angles are all positive or negative (depending on the
 5         // order in which we visit them) so the polygon is convex.
 6         boolean gotNegative = false;
 7         boolean gotPositive = false;
 8         int numPoints = points.size();
 9         int B, C;
10         for (int A = 0; A < numPoints; A++) {
11             // Trick to calc the last 3 points: n - 1, 0 and 1.
12             B = (A + 1) % numPoints;
13             C = (B + 1) % numPoints;
14
15             int crossProduct =
16                 crossProductLength(
17                     points.get(A).get(0), points.get(A).get(1),
18                     points.get(B).get(0), points.get(B).get(1),
19                     points.get(C).get(0), points.get(C).get(1));
20             if (crossProduct < 0) {
21                 gotNegative = true;
22             }
23             else if (crossProduct > 0) {
24                 gotPositive = true;
25             }
26             if (gotNegative && gotPositive) return false;
27         }
28
29         // If we got this far, the polygon is convex.
30         return true;
31     }
32
33     // Return the cross product AB x BC.
34     // The cross product is a vector perpendicular to AB and BC having length |AB| * |BC| * Sin(theta) and
35     // with direction given by the right-hand rule. For two vectors in the X-Y plane, the result is a
36     // vector with X and Y components 0 so the Z component gives the vector‘s length and direction.
37     private int crossProductLength(int Ax, int Ay, int Bx, int By, int Cx, int Cy)
38     {
39         // Get the vectors‘ coordinates.
40         int ABx = Bx - Ax;
41         int ABy = By - Ay;
42         int BCx = Cx - Bx;
43         int BCy = Cy - By;
44
45         // Calculate the Z coordinate of the cross product.
46         return (ABx * BCy - ABy * BCx);
47     }
48 }
时间: 2025-01-13 12:22:16

Leetcode: Convex Polygon的相关文章

[LeetCode] Convex Polygon 凸多边形

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). Note: There are at least 3 and at most 10,000 points. Coordinates are in the range -10,000 to 10,000. You may assume the

LeetCode469 - Convex Polygon - Medium (Python)

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). [[0,0],[0,1],[1,1],[1,0]] Answer: True [[0,0],[0,10],[10,10],[10,0],[5,5]] Answer: False 思路:这题问的是给一系列polygon的点,判断其是否是conv

hunnu11562:The Triangle Division of the Convex Polygon(第n个卡特兰数取模)

Problem description   A convex polygon with n edges can be divided into several triangles by some non-intersect diagonals. We denote d(n) is the number of the different ways to divide the convex polygon. For example,when n is 6,there are 14 different

HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))

The Triangle Division of the Convex Polygon 题意:求 n 凸多边形可以有多少种方法分解成不相交的三角形,最后值模 m. 思路:卡特兰数的例子,只是模 m 让人头疼,因为 m 不一定是素数,所以不一定存在逆元. 解法:式子为f(n) =  ( C( 2*(n-2),  (n-2) ) / (n-1))   % m :令 p = n-2, 式子可化为:f(p) = ((2*p)! / ( p! * (p+1)! ) ) % m; 对 s!分解质因素,统计个

HUNAN 11562 The Triangle Division of the Convex Polygon(大卡特兰数)

http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11562&courseid=0 求n边形分解成三角形的方案数. 就是求n-2个卡特兰数,从大神那盗取了一份模板,效率极高.同时也很复杂. 1 #include <cstdio> 2 #include <cmath> 3 #include <stdlib.h> 4 #include <memory.h> 5 type

HNU 13101 The Triangle Division of the Convex Polygon 卡特兰数第n项%m(m可为非素数

题目链接:点击打开链接 首先要n-=2,然后就是一个卡特兰数了. 上一题用的是 h(n) = h(n-1) * (4n-2)/(n+1); 这题用的是 h(n) = (2n)! * n! / (n+1)!; 然后对阶乘分解质因数: 点击打开链接 分解完了直接快速幂. #include<stdio.h> #include<iostream> #include<cmath> #include<cstring> using namespace std; #defi

Scrambled Polygon(差集排序)

Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7799   Accepted: 3707 Description A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the

codeforces D. PolandBall and Polygon

D. PolandBall and Polygon time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. P

POJ 2007 Scrambled Polygon(计算几何 叉积排序啊)

题目链接:http://poj.org/problem?id=2007 Description A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the vertices of the polygon. When one starts at any vertex of a close