# 469. Convex Polygon 凸多边形

@TOC

## # 题目描述

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:

## # 解题方法

### # 计算向量夹角

C++代码如下：

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

}
};
``````

## # 日期

2019 年 9 月 20 日 —— 是选择中国互联网式加班？还是外企式养生？