Java实现两点间随机生成折线或线段,找出最短无遮挡路线
以下是一个使用Java Swing实现的简单示例程序,可以生成随机的折线或线段,并找出最短的一条线段,且不被其他线段遮挡:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
public class ShortestLineFinder extends JFrame {
private JPanel linePanel;
private JButton generateButton;
private JButton resetButton;
private List<Line> lines;
private Line shortestLine;
public ShortestLineFinder() {
setTitle('Shortest Line Finder');
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
linePanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (lines != null) {
for (Line line : lines) {
g2d.setStroke(new BasicStroke(1));
g2d.setColor(Color.BLACK);
g2d.drawLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY());
}
if (shortestLine != null) {
g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.RED);
g2d.drawLine(shortestLine.getStartX(), shortestLine.getStartY(), shortestLine.getEndX(), shortestLine.getEndY());
}
}
}
};
generateButton = new JButton('Generate Lines');
resetButton = new JButton('Reset');
generateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
generateLines();
}
});
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
reset();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(generateButton);
buttonPanel.add(resetButton);
setLayout(new BorderLayout());
add(linePanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
private void generateLines() {
Random random = new Random();
lines = new ArrayList<>();
int numLines = random.nextInt(31) + 10; // Generate between 10 and 40 lines
for (int i = 0; i < numLines; i++) {
int startX = random.nextInt(linePanel.getWidth());
int startY = random.nextInt(linePanel.getHeight());
int endX = random.nextInt(linePanel.getWidth());
int endY = random.nextInt(linePanel.getHeight());
lines.add(new Line(startX, startY, endX, endY));
}
findShortestLine();
linePanel.repaint();
}
private void findShortestLine() {
shortestLine = null;
for (Line line : lines) {
boolean isShortest = true;
for (Line otherLine : lines) {
if (line != otherLine && line.isIntersecting(otherLine)) {
isShortest = false;
break;
}
}
if (isShortest) {
if (shortestLine == null || line.getLength() < shortestLine.getLength()) {
shortestLine = line;
}
}
}
}
private void reset() {
lines = null;
shortestLine = null;
linePanel.repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ShortestLineFinder().setVisible(true);
}
});
}
private class Line {
private int startX;
private int startY;
private int endX;
private int endY;
public Line(int startX, int startY, int endX, int endY) {
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
}
public int getStartX() {
return startX;
}
public int getStartY() {
return startY;
}
public int getEndX() {
return endX;
}
public int getEndY() {
return endY;
}
public int getLength() {
return (int) Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
}
public boolean isIntersecting(Line otherLine) {
int x1 = startX;
int y1 = startY;
int x2 = endX;
int y2 = endY;
int x3 = otherLine.getStartX();
int y3 = otherLine.getStartY();
int x4 = otherLine.getEndX();
int y4 = otherLine.getEndY();
int denominator = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
if (denominator == 0) {
return false;
}
int numerator1 = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
int numerator2 = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
float ua = (float) numerator1 / denominator;
float ub = (float) numerator2 / denominator;
return ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1;
}
}
}
这个程序使用Java Swing库创建了一个简单的图形界面,包含一个用于显示线段的面板和两个按钮。点击'Generate Lines'按钮可以生成随机的折线或线段,并在面板上显示出来。程序会找出最短的一条线段,且不被其他线段遮挡,并用红色加粗显示出来。点击'Reset'按钮可以重新生成线段。
请注意,这个程序只是一个简单的示例,可能存在一些边界情况没有处理。如果有需要,你可以根据实际需求进行修改和完善。
原文地址: http://www.cveoy.top/t/topic/o2nG 著作权归作者所有。请勿转载和采集!