利用Java语言用Java小程序使用边界标志算法给一个顶点坐标为(52,20), (158,20), (195,130), (100,200), (5,130)的凸多边形填充颜色,给出代码
时间: 2023-11-26 13:03:30 浏览: 38
好的,我会尽力回答你的问题。以下是利用Java语言用Java小程序使用边界标志算法给一个顶点坐标为(52,20), (158,20), (195,130), (100,200), (5,130)的凸多边形填充颜色的代码:
```
import java.awt.*;
import javax.swing.*;
public class ConvexPolygon extends JPanel {
private static final long serialVersionUID = 1L;
private Point[] vertices;
private double[] slopes;
public ConvexPolygon(Point[] vertices) {
this.vertices = vertices;
slopes = new double[vertices.length];
for (int i = 0; i < vertices.length; i++) {
slopes[i] = getSlope(vertices[i], vertices[(i + 1) % vertices.length]);
}
setBackground(Color.WHITE);
}
private double getSlope(Point p1, Point p2) {
if (p1.x == p2.x) {
return Double.POSITIVE_INFINITY;
}
return ((double) (p2.y - p1.y)) / ((double) (p2.x - p1.x));
}
public void paintComponent(Graphics graph) {
super.paintComponent(graph);
Graphics2D g = (Graphics2D) graph;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int yMin = Integer.MAX_VALUE;
int yMax = Integer.MIN_VALUE;
for (int i = 0; i < vertices.length; i++) {
if (vertices[i].y < yMin) {
yMin = vertices[i].y;
}
if (vertices[i].y > yMax) {
yMax = vertices[i].y;
}
}
for (int y = yMin; y <= yMax; y++) {
int xMin = Integer.MAX_VALUE;
int xMax = Integer.MIN_VALUE;
for (int i = 0; i < vertices.length; i++) {
Point p1 = vertices[i];
Point p2 = vertices[(i + 1) % vertices.length];
if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
int x = (int) Math.round(p1.x + (double) (y - p1.y) / getSlope(p1, p2));
if (x < xMin) {
xMin = x;
}
if (x > xMax) {
xMax = x;
}
}
}
if (xMin <= xMax) {
g.drawLine(xMin, y, xMax, y);
}
}
}
public static void main(String[] args) {
Point[] vertices = { new Point(52, 20), new Point(158, 20), new Point(195, 130), new Point(100, 200), new Point(5, 130) };
JFrame frame = new JFrame("Convex Polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ConvexPolygon(vertices));
frame.setSize(250, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这段代码实现了凸多边形的填充,其中用到了边界标志算法。如果你有什么问题或疑问,请随时问我。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)