(1)程序运行时,由用户输入一个整数N,一个浮点数Min,一个浮点数Max,整数N表示线段的条数,Min表示最小坐标值,Max表示最大坐标值。(2)然后程序随机生成N条直线段的端点坐标,每一条直线段两个端点的坐标都界于Min和Max之间,所有直线段以随机颜色显示在屏幕上。(3)最后程序自动找出这N条随机直线段切割平面形成的所有封闭多边形,并且求出所有多边形的面积并显示出来。(4)输入输出方式可以采用文件,也可以采用菜单,对话框等形式。
我现在写给你
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
// 获取用户输入
Scanner scanner = new Scanner(System.in);
System.out.print("输入线段条数:");
int n = scanner.nextInt();
System.out.print("输入最小坐标值:");
double min = scanner.nextDouble();
System.out.print("输入最大坐标值:");
double max = scanner.nextDouble();
// 随机生成直线段
Random random = new Random();
List<Line> lines = new ArrayList<>();
for (int i = 0; i < n; i++) {
double x1 = min + (max - min) * random.nextDouble();
double y1 = min + (max - min) * random.nextDouble();
double x2 = min + (max - min) * random.nextDouble();
double y2 = min + (max - min) * random.nextDouble();
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
lines.add(new Line(x1, y1, x2, y2, color));
}
// 绘制直线段并求出封闭多边形的面积
JFrame frame = new JFrame("随机直线段");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
JPanel panel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
for (Line line : lines) {
g.setColor(line.color);
g.drawLine((int) line.x1, (int) line.y1, (int) line.x2, (int) line.y2);
}
}
};
frame.add(panel);
List<Point[]> polygons = getPolygons(lines);
for (Point[] points : polygons) {
double area = getPolygonArea(points);
System.out.println("多边形面积:" + area);
}
}
private static List<Point[]> getPolygons(List<Line> lines) {
// 省略实现
}
private static double getPolygonArea(Point[] points) {
double area = 0;
for (int i = 0; i < points.length; i++) {
Point p1 = points[i];
Point p2 = points[(i + 1) % points.length];
area += p1.x * p2.y - p1.y * p2.x;
}
return Math.abs(area) / 2;
}
}
class Line {
double x1, y1, x2, y2;
Color color;
Line(double x1, double y1, double x2, double y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
}
class Point {
double x, y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
}
java基础-面向对象
非常详细,借鉴下
https://blog.csdn.net/codeglory/article/details/124330749
为了实现上述功能,你可以设计如下的类:
1.直线段类(Line):该类应该包含直线段的两个端点坐标以及直线段的颜色。
2.平面图形类(PlaneFigure):该类是一个抽象类,应该包含计算多边形面积的抽象方法。
3.多边形类(Polygon):该类继承自平面图形类,包含多边形的边的信息,并实现计算多边形面积的方法。
本地编写了参考代码:
import java.awt.Color;
import java.util.List;
// 直线段类
class Line {
private double x1;
private double y1;
private double x2;
private double y2;
private Color color;
public Line(double x1, double y1, double x2, double y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
public double getX1() {
return x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
public double getY1() {
return y1;
}
public void setY1(double y1) {
this.y1 = y1;
}
public double getX2() {
return x2;
}
public void setX2(double x2) {
this.x2 = x2;
}
public double getY2() {
return y2;
}
public void setY2(double y2) {
this.y2 = y2;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
// 平面图形类
abstract class PlaneFigure {
// 计算多边形面积的抽象方法
public abstract double getArea();
}
// 多边形类
class Polygon extends PlaneFigure {
private List<Line> sides;
public Polygon(List<Line> sides) {
this.sides = sides;
}
public List<Line> getSides() {
return sides;
}
public void setSides(List<Line> sides) {
this.sides = sides;
}
@Override
public double getArea() {
// 具体的计算多边形面积的方法可能会比较复杂,这里就不做赘述了。
// 你可以参考一些几何算法来实现这个方法。
}
}
你可以使用Java的AWT或Swing组件来绘制直线段,使用Java的Random类来生成随机的坐标和颜色。
具体的步骤如下:
在Java程序中导入AWT或Swing组件库。
创建一个继承JFrame的窗口类,在窗口的paint方法中绘制直线段。
使用Java的Random类生成随机的坐标和颜色。
利用AWT或Swing组件中的drawLine方法在窗口上绘制直线段。
在Java程序的main方法中创建窗口对象,并调用窗口的setVisible方法显示窗口。
使用Java的Scanner类从控制台读入用户输入的整数N、浮点数Min和浮点数Max。
在窗口的paint方法中使用循环语句生成N条随机直线段。
使用Java的算法或第三方库(比如JTS Topology Suite)计算出所有直线段切割平面形成的封闭多边形的面积。
在窗口的paint方法或其他方法中显示出所有多边形的面积。
希望这些步骤能帮到你!
参考:
有帮助的话采纳一下哦!
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
// 直线段类
class Line {
double x1, y1, x2, y2;
int color;
}
// 多边形类
class Polygon {
List<Line> edges;
double area;
}
// 图形生成器类
class ShapeGenerator {
// 生成 N 条直线段
List<Line> generateShapes(int n, double min, double max) {
List<Line> lines = new ArrayList<>();
Random rnd = new Random();
for (int i = 0; i < n; i++) {
Line line = new Line();
line.x1 = min + (max - min) * rnd.nextDouble();
line.y1 = min + (max - min) * rnd.nextDouble();
line.x2 = min + (max - min) * rnd.nextDouble();
line.y2 = min + (max - min) * rnd.nextDouble();
line.color = rnd.nextInt();
lines.add(line);
}
return lines;
}
}
// 多边形查找器类
class PolygonFinder {
List<Polygon> findPolygons(List<Line> lines) {
// 省略实现
}
}
// 图形显示器类
class ShapeDisplayer {
void display(List<Polygon> polygons) {
// 我省略实现了
}
}
// 主类
public class Main {
public static void main(String[] args) {
//这里你可以用scanner输入值,我省略了
int n = ...;
double min = ...;
double max = ...;
ShapeGenerator shapeGenerator = new ShapeGenerator();
List<Line> lines = shapeGenerator.generateShapes(n, min, max);
PolygonFinder polygonFinder = new PolygonFinder();
List<Polygon> polygons = polygonFinder.findPolygons(lines);
ShapeDisplayer shapeDisplayer = new ShapeDisplayer();
shapeDisplayer.display(polygons);
}
}
佣金太少了
1、如何在 Java 中获取用户的输入,可以使用 Scanner 类进行输入。例如:
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
double min = scanner.nextDouble();
double max = scanner.nextDouble();
2、如何在 Java 中生成随机数,可以使用 java.util.Random 类。例如:
Random random = new Random();
double randomValue = min + (max - min) * random.nextDouble();
3、如何在 Java 中绘制图形,可以使用 java.awt.Graphics 类或 javax.swing.JComponent 类进行绘制。
4、如何在 Java 中计算多边形的面积,可以使用 Shoelace 公式进行计算。具体实现如下:
double area = 0;
for (int i = 0; i < points.length; i++) {
Point p1 = points[i];
Point p2 = points[(i + 1) % points.length];
area += p1.x * p2.y - p1.y * p2.x;
}
area = Math.abs(area) / 2;
5、还需要了解如何使用文件或对话框进行输入输出。
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
// 获取用户输入
Scanner scanner = new Scanner(System.in);
System.out.print("输入线段条数:");
int n = scanner.nextInt();
System.out.print("输入最小坐标值:");
double min = scanner.nextDouble();
System.out.print("输入最大坐标值:");
double max = scanner.nextDouble();
// 随机生成直线段
Random random = new Random();
List<Line> lines = new ArrayList<>();
for (int i = 0; i < n; i++) {
double x1 = min + (max - min) * random.nextDouble();
double y1 = min + (max - min) * random.nextDouble();
double x2 = min + (max - min) * random.nextDouble();
double y2 = min + (max - min) * random.nextDouble();
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
lines.add(new Line(x1, y1, x2, y2, color));
}
// 绘制直线段并求出封闭多边形的面积
JFrame frame = new JFrame("随机直线段");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
JPanel panel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
for (Line line : lines) {
g.setColor(line.color);
g.drawLine((int) line.x1, (int) line.y1, (int) line.x2, (int) line.y2);
}
}
};
frame.add(panel);
List<Point[]> polygons = getPolygons(lines);
for (Point[] points : polygons) {
double area = getPolygonArea(points);
System.out.println("多边形面积:" + area);
}
}
private static List<Point[]> getPolygons(List<Line> lines) {
// 省略实现
}
private static double getPolygonArea(Point[] points) {
double area = 0;
for (int i = 0; i < points.length; i++) {
Point p1 = points[i];
Point p2 = points[(i + 1) % points.length];
area += p1.x * p2.y - p1.y * p2.x;
}
return Math.abs(area) / 2;
}
}
class Line {
double x1, y1, x2, y2;
Color color;
Line(double x1, double y1, double x2, double y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
}
class Point {
double x, y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
}
上述示例代码假设已经实现了 getPolygons 方法,该方法负责将直线段分组成封闭多边形。具体实现方法可以使用扫描线算法或者欧拉算法。
望采纳。