返回介绍

solution / 0400-0499 / 0469.Convex Polygon / README_EN

发布于 2024-06-17 01:04:00 字数 4303 浏览 0 评论 0 收藏 0

469. Convex Polygon

中文文档

Description

You are given an array of points on the X-Y plane points where points[i] = [xi, yi]. The points form a polygon when joined sequentially.

Return true if this polygon is convex and false otherwise.

You may assume the polygon formed by given points is always a simple polygon. In other words, we ensure that exactly two edges intersect at each vertex and that edges otherwise don't intersect each other.

 

Example 1:

Input: points = [[0,0],[0,5],[5,5],[5,0]]
Output: true

Example 2:

Input: points = [[0,0],[0,10],[10,10],[10,0],[5,5]]
Output: false

 

Constraints:

  • 3 <= points.length <= 104
  • points[i].length == 2
  • -104 <= xi, yi <= 104
  • All the given points are unique.

Solutions

Solution 1

class Solution:
  def isConvex(self, points: List[List[int]]) -> bool:
    n = len(points)
    pre = cur = 0
    for i in range(n):
      x1 = points[(i + 1) % n][0] - points[i][0]
      y1 = points[(i + 1) % n][1] - points[i][1]
      x2 = points[(i + 2) % n][0] - points[i][0]
      y2 = points[(i + 2) % n][1] - points[i][1]
      cur = x1 * y2 - x2 * y1
      if cur != 0:
        if cur * pre < 0:
          return False
        pre = cur
    return True
class Solution {
  public boolean isConvex(List<List<Integer>> points) {
    int n = points.size();
    long pre = 0, cur = 0;
    for (int i = 0; i < n; ++i) {
      var p1 = points.get(i);
      var p2 = points.get((i + 1) % n);
      var p3 = points.get((i + 2) % n);
      int x1 = p2.get(0) - p1.get(0);
      int y1 = p2.get(1) - p1.get(1);
      int x2 = p3.get(0) - p1.get(0);
      int y2 = p3.get(1) - p1.get(1);
      cur = x1 * y2 - x2 * y1;
      if (cur != 0) {
        if (cur * pre < 0) {
          return false;
        }
        pre = cur;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool isConvex(vector<vector<int>>& points) {
    int n = points.size();
    long long pre = 0, cur = 0;
    for (int i = 0; i < n; ++i) {
      int x1 = points[(i + 1) % n][0] - points[i][0];
      int y1 = points[(i + 1) % n][1] - points[i][1];
      int x2 = points[(i + 2) % n][0] - points[i][0];
      int y2 = points[(i + 2) % n][1] - points[i][1];
      cur = 1L * x1 * y2 - x2 * y1;
      if (cur != 0) {
        if (cur * pre < 0) {
          return false;
        }
        pre = cur;
      }
    }
    return true;
  }
};
func isConvex(points [][]int) bool {
  n := len(points)
  pre, cur := 0, 0
  for i := range points {
    x1 := points[(i+1)%n][0] - points[i][0]
    y1 := points[(i+1)%n][1] - points[i][1]
    x2 := points[(i+2)%n][0] - points[i][0]
    y2 := points[(i+2)%n][1] - points[i][1]
    cur = x1*y2 - x2*y1
    if cur != 0 {
      if cur*pre < 0 {
        return false
      }
      pre = cur
    }
  }
  return true
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文