实现思路:

  1. 首先需要定义一个Point类,表示点的坐标。
  2. 定义一个Line类,表示线段,包含起点和终点两个属性。
  3. 生成n条线段,可以使用随机数生成两个点的坐标,然后构造线段对象。注意,起点和终点不能重合。
  4. 对于每条线段,需要判断是否被其他线段遮挡。可以使用线段相交的判断方法,如果相交则认为被遮挡。
  5. 遍历所有线段,找出最短且不被遮挡的线段,保存起点和终点坐标。
  6. 输出最短线段的起点和终点坐标。

以下是具体的实现代码:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        int n = (int) (Math.random() * 31) + 10; // 随机生成10到40之间的整数n

        List<Line> lines = generateLines(n); // 生成n条线段

        Line shortestLine = findShortestLine(lines); // 找出最短且不被遮挡的线段

        System.out.println("最短线段的起点坐标: (" + shortestLine.getStart().getX() + ", " + shortestLine.getStart().getY() + ")");
        System.out.println("最短线段的终点坐标: (" + shortestLine.getEnd().getX() + ", " + shortestLine.getEnd().getY() + ")");
    }

    // 生成n条线段
    public static List<Line> generateLines(int n) {
        List<Line> lines = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            Point start = generatePoint();
            Point end = generatePoint();
            while (start.equals(end)) {
                end = generatePoint(); // 如果起点和终点坐标重合,则重新生成终点坐标
            }
            Line line = new Line(start, end);
            lines.add(line);
        }
        return lines;
    }

    // 生成一个随机点
    public static Point generatePoint() {
        int x = (int) (Math.random() * 100);
        int y = (int) (Math.random() * 100);
        return new Point(x, y);
    }

    // 判断两条线段是否相交
    public static boolean isIntersect(Line line1, Line line2) {
        double x1 = line1.getStart().getX();
        double y1 = line1.getStart().getY();
        double x2 = line1.getEnd().getX();
        double y2 = line1.getEnd().getY();
        double x3 = line2.getStart().getX();
        double y3 = line2.getStart().getY();
        double x4 = line2.getEnd().getX();
        double y4 = line2.getEnd().getY();

        double d = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3);
        if (d == 0) {
            return false; // 平行或共线
        }

        double u = ((x3 - x1) * (y4 - y3) - (y3 - y1) * (x4 - x3)) / d;
        double v = ((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) / d;

        return u >= 0 && u <= 1 && v >= 0 && v <= 1;
    }

    // 找出最短且不被遮挡的线段
    public static Line findShortestLine(List<Line> lines) {
        Line shortestLine = null;
        double shortestDistance = Double.MAX_VALUE;

        for (Line line1 : lines) {
            boolean isCovered = false;
            double distance = line1.getLength();

            for (Line line2 : lines) {
                if (line1 != line2 && isIntersect(line1, line2)) {
                    isCovered = true;
                    break; // 如果被遮挡,则跳出内层循环
                }
            }

            if (!isCovered && distance < shortestDistance) {
                shortestDistance = distance;
                shortestLine = line1;
            }
        }

        return shortestLine;
    }
}

class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Point point = (Point) obj;
        return Double.compare(point.x, x) == 0 && Double.compare(point.y, y) == 0;
    }

    @Override
    public int hashCode() {
        return Double.hashCode(x) * 31 + Double.hashCode(y);
    }
}

class Line {
    private Point start;
    private Point end;

    public Line(Point start, Point end) {
        this.start = start;
        this.end = end;
    }

    public Point getStart() {
        return start;
    }

    public Point getEnd() {
        return end;
    }

    public double getLength() {
        double dx = end.getX() - start.getX();
        double dy = end.getY() - start.getY();
        return Math.sqrt(dx * dx + dy * dy);
    }
}

注意:由于生成的线段是随机的,每次运行结果可能不同

用java实现:在两个点之间随机生成n条由线段组成的折线或线段10=n=40找出最短的一条并且这段线段不能被其他线段遮挡

原文地址: http://www.cveoy.top/t/topic/hzuR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录