Collinear Points

Collinear Points

Point.java

照要求补API即可。

import java.util.Comparator;
import edu.princeton.cs.algs4.StdDraw;

public class Point implements Comparable<Point> {
   

    private final int x;     // x-coordinate of this point
    private final int y;     // y-coordinate of this point

    /**
     * Initializes a new point.
     *
     * @param  x the <em>x</em>-coordinate of the point
     * @param  y the <em>y</em>-coordinate of the point
     */
    public Point(int x, int y) {
   
        /* DO NOT MODIFY */
        this.x = x;
        this.y = y;
    }

    /**
     * Draws this point to standard draw.
     */
    public void draw() {
   
        /* DO NOT MODIFY */
        StdDraw.point(x, y);
    }

    /**
     * Draws the line segment between this point and the specified point
     * to standard draw.
     *
     * @param that the other point
     */
    public void drawTo(Point that) {
   
        /* DO NOT MODIFY */
        StdDraw.line(this.x, this.y, that.x, that.y);
    }

    /**
     * Returns the slope between this point and the specified point.
     * Formally, if the two points are (x0, y0) and (x1, y1), then the slope
     * is (y1 - y0) / (x1 - x0). For completeness, the slope is defined to be
     * +0.0 if the line segment connecting the two points is horizontal;
     * Double.POSITIVE_INFINITY if the line segment is vertical;
     * and Double.NEGATIVE_INFINITY if (x0, y0) and (x1, y1) are equal.
     *
     * @param  that the other point
     * @return the slope between this point and the specified point
     */
    public double slopeTo(Point that) {
   
        if (compareTo(that) == 0) {
   
            return Double.NEGATIVE_INFINITY;
        }
        if (this.x == that.x) {
   
            return Double.POSITIVE_INFINITY;
        }
        if (this.y == that.y) {
   
            return +0.0;
        }

        return 1.0 * (this.y - that.y) / (this.x - that.x);
    }

    /**
     * Compares two points by y-coordinate, breaking ties by x-coordinate.
     * Formally, the invoking point (x0, y0) is less than the argument point
     * (x1, y1) if and only if either y0 < y1 or if y0 = y1 and x0 < x1.
     *
     * @param  that the other point
     * @return the value <tt>0</tt> if this point is equal to the argument
     *         point (x0 = x1 and y0 = y1);
     *         a negative integer if this point is less than the argument
     *         point; and a positive integer if this point is greater than the
     *         argument point
     */
    public int compareTo(Point that) {
   
        if (this.y < that.y) {
   
            return -1;
        } else if (this.y > that.y) {
   
            return 1;
        } else if (this.x < that.x) {
   
            return -1;
      
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++的实现代码: ```c++ #include <iostream> #include <vector> #include <algorithm> #include <map> #include <cmath> using namespace std; const double eps = 1e-9; struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} bool operator<(const Point& p) const { if (fabs(x - p.x) > eps) return x < p.x; return y < p.y; } }; double cross(const Point& O, const Point& A, const Point& B) { return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x); } vector<Point> convex_hull(vector<Point> P) { int n = P.size(), k = 0; vector<Point> H(2 * n); sort(P.begin(), P.end()); for (int i = 0; i < n; ++i) { while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } for (int i = n - 2, t = k + 1; i >= 0; --i) { while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } H.resize(k); return H; } bool cmp(const Point& p1, const Point& p2) { if (p1.x != p2.x) return p1.x < p2.x; return p1.y < p2.y; } int main() { int n; cin >> n; vector<Point> points(n); for (int i = 0; i < n; ++i) { double x, y; cin >> x >> y; points[i] = Point(x, y); } map<pair<Point, Point>, vector<Point>> slopes; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (points[i] < points[j]) { slopes[make_pair(points[i], points[j])].push_back(points[j]); } else { slopes[make_pair(points[j], points[i])].push_back(points[i]); } } } vector<Point> candidates; for (auto& slope : slopes) { auto& points_on_slope = slope.second; if (points_on_slope.size() < 3) continue; sort(points_on_slope.begin(), points_on_slope.end(), cmp); for (int i = 0; i < points_on_slope.size(); ++i) { for (int j = i + 1; j < points_on_slope.size(); ++j) { candidates.push_back(Point((points_on_slope[i].x + points_on_slope[j].x) / 2, (points_on_slope[i].y + points_on_slope[j].y) / 2)); } } } sort(candidates.begin(), candidates.end(), cmp); auto last = unique(candidates.begin(), candidates.end()); candidates.erase(last, candidates.end()); for (auto& candidate : candidates) { vector<Point> collinear_points; for (auto& slope : slopes) { auto& points_on_slope = slope.second; if (points_on_slope.size() < 3) continue; if (fabs(cross(candidate, slope.first.first, slope.first.second)) < eps) { collinear_points.push_back(slope.first.first); collinear_points.push_back(slope.first.second); collinear_points.insert(collinear_points.end(), points_on_slope.begin(), points_on_slope.end()); } } if (collinear_points.size() >= 4) { collinear_points = convex_hull(collinear_points); cout << collinear_points.size() << " "; for (auto& point : collinear_points) { cout << point.x << " " << point.y << " "; } cout << endl; return 0; } } cout << "No collinear points found." << endl; return 0; } ``` 这个程序先读入平面上的N个点,然后对于任意两个点 $P_i$ 和 $P_j$,计算它们所在的直线的斜率(即 $\frac{y_j - y_i}{x_j - x_i}$),将这个斜率作为键,将所有与这个斜率相同的点都添加到对应的值中。这个过程的时间复杂度是 $O(N^2 \log N)$。 然后,对于所有的斜率,找到所有可能的中点,即对于每个斜率,找到所有满足横坐标为两个点的横坐标之和的一半,纵坐标为两个点的纵坐标之和的一半的点。这个过程的时间复杂度是 $O(N^2)$。 对于每个中点,枚举所有与它所在直线相同的点集,如果这些点集中有 $4$ 个或更多的点共线,就将它们计算凸包,并输出。这个过程的时间复杂度是 $O(N^2 \log N)$。 因此,整个程序的时间复杂度是 $O(N^2 \log N)$。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值