# 469. Convex Polygon

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

Note:

1. There are at least 3 and at most 10,000 points.
2. Coordinates are in the range -10,000 to 10,000.
3. 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]]

Explanation:
``````

Example 2:

``````[[0,0],[0,10],[10,10],[10,0],[5,5]]

Explanation:
``````

``````class Solution {
public:
bool isConvex(vector<vector<int>>& points) {
long long n = points.size(), pre = 0, cur = 0;
for (int i = 0; i < n; ++i) {
int dx1 = points[(i + 1) % n][0] - points[i][0];
int dx2 = points[(i + 2) % n][0] - points[i][0];
int dy1 = points[(i + 1) % n][1] - points[i][1];
int dy2 = points[(i + 2) % n][1] - points[i][1];
cur = dx1 * dy2 - dx2 * dy1;
if (cur != 0) {
if (cur * pre < 0) return false;
else pre = cur;
}
}
return true;
}
};
``````

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation