gpt4 book ai didi

java - Java 中的形状和线段

转载 作者:行者123 更新时间:2023-11-30 06:20:20 25 4
gpt4 key购买 nike

我有一个形状。我基本上是在尝试使用线段作为二等分将一个区域分成两个区域。

public Shape divide(Shape a, Point2D p1, Point2D p2)  {    

Shape str = new BasicStroke().createStrokedShape(new Line2D.Double(p1,p2));
Shape line = new Shape(str);
Shape temp = a;
line.intersect(temp);
temp.exclusiveOr(line);
// temp is the shape with the line intersecting it

AffineTransform t = new AffineTransform();
double angle = Math.atan2(p2.getY() - p1.getY(), p2.getX() - p1.getX());

t.rotate(angle, p1.getX(), p1.getY());
temp = temp.createTransformedArea(t);

return Shape ;

}

我想使用线段将形状一分为二,但不确定如何去做,我正在查看相交方法: http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Area.html但仍然不确定如何从一个区域获得两个区域。我希望返回类似的内容:

return  firstHalf secondHalf;

最佳答案

我会这样做。请注意,当从右下角开始的点结束到左上角点的左侧时,代码有一个错误。留给用户作为练习。

enter image description here

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

class SplitArea {

int s = 100;
JPanel gui = new JPanel(new BorderLayout());
BufferedImage[] images = new BufferedImage[4];
Point p1 = new Point(s / 4, s / 4);
Point p2 = new Point(s * 3 / 4, s * 3 / 4);
Ellipse2D ellipse = new Ellipse2D.Float(
s / 5, s / 5, s * 3 / 5, s * 3 / 5);
Rectangle2D bg = new Rectangle2D.Float(0, 0, s, s);

SplitArea() {
JToolBar tb = new JToolBar();
gui.add(tb, BorderLayout.PAGE_START);
final JToggleButton tob = new JToggleButton("Primary Point");
tb.add(tob);

JPanel view = new JPanel(new GridLayout(1, 0, 4, 4));
gui.add(view, BorderLayout.CENTER);
for (int ii = 0; ii < images.length; ii++) {
BufferedImage bi = new BufferedImage(
s, s, BufferedImage.TYPE_INT_RGB);
images[ii] = bi;
JLabel l = new JLabel(new ImageIcon(bi));
if (ii == 0) {
l.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
if (tob.isSelected()) {
p1 = e.getPoint();
} else {
p2 = e.getPoint();
}
drawImages();
}
});
}
view.add(l);
}

drawImages();
}

public final void drawImages() {
Graphics2D g;

// image 0
g = images[0].createGraphics();
g.setColor(Color.BLACK);
g.fill(bg);

g.setColor(Color.CYAN);
g.fill(ellipse);
g.setColor(Color.WHITE);
g.draw(ellipse);

g.setColor(Color.red);
drawPoint(g, p1);
drawPoint(g, p2);

g.dispose();

int xDiff = p1.x - p2.x;
int yDiff = p1.y - p2.y;
Point2D xAxis;
Point2D xSAxis;
if (xDiff == 0) {
xAxis = new Point2D.Double(p1.x, 0);
xSAxis = new Point2D.Double(p1.x, s);
} else if (yDiff == 0) {
xAxis = new Point2D.Double(0, p1.y);
xSAxis = new Point2D.Double(s, p1.y);
} else {
System.out.println("Not vertical or horizontal!");
// will throw a NaN if line is vertical
double m = (double) yDiff / (double) xDiff;
System.out.println("m: " + m);

double b = (double) p1.y - (m * (double) p1.x);
System.out.println("b: " + b);

// crosses x axis at..
xAxis = new Point2D.Double(0d, b);
double pointS = (s - b) / m;
xSAxis = new Point2D.Double(pointS, s);
}

// image 1
g = images[1].createGraphics();
g.setColor(Color.BLACK);
g.fill(bg);

g.setColor(Color.CYAN);
g.fill(ellipse);
g.setColor(Color.WHITE);
g.draw(ellipse);

g.setColor(Color.YELLOW);
System.out.println(xAxis);
System.out.println(xSAxis);
g.drawLine(
(int) xAxis.getX(), (int) xAxis.getY(),
(int) xSAxis.getX(), (int) xSAxis.getY());

g.setColor(Color.red);
drawPoint(g, p1);
drawPoint(g, p2);

g.dispose();

// image 2
g = images[1].createGraphics();
g.setColor(Color.BLACK);
g.fill(bg);

g.setColor(Color.CYAN);
g.fill(ellipse);
g.setColor(Color.WHITE);
g.draw(ellipse);

g.setColor(Color.YELLOW);
System.out.println(xAxis);
System.out.println(xSAxis);
g.drawLine(
(int) xAxis.getX(), (int) xAxis.getY(),
(int) xSAxis.getX(), (int) xSAxis.getY());

g.setColor(Color.red);
drawPoint(g, p1);
drawPoint(g, p2);

g.dispose();

// split the regions
Rectangle2D.Double all = new Rectangle2D.Double(0, 0, s, s);
Area a1 = new Area(all);
Area a2 = new Area(all);
GeneralPath aPart = new GeneralPath();
aPart.moveTo(0, 0);
aPart.lineTo(0, s);
aPart.lineTo(xSAxis.getX(), xSAxis.getY());
aPart.lineTo(xAxis.getX(), xAxis.getY());
aPart.closePath();
a1.subtract(new Area(aPart));
a2.subtract(a1);

Area ellipsePartA = new Area(ellipse);
ellipsePartA.subtract(a1);
Area ellipsePartB = new Area(ellipse);
ellipsePartB.subtract(a2);

// image 3
g = images[2].createGraphics();
g.setColor(Color.BLACK);
g.fill(bg);

g.setColor(Color.CYAN);
g.fill(ellipsePartA);
g.setColor(Color.WHITE);
g.draw(ellipsePartA);

g.setColor(Color.red);
drawPoint(g, p1);
drawPoint(g, p2);

g.dispose();

// image 4
g = images[3].createGraphics();
g.setColor(Color.BLACK);
g.fill(bg);

g.setColor(Color.CYAN);
g.fill(ellipsePartB);
g.setColor(Color.WHITE);
g.draw(ellipsePartB);

g.setColor(Color.red);
drawPoint(g, p1);
drawPoint(g, p2);

g.dispose();

gui.repaint();
}

public final void drawPoint(Graphics g, Point2D p) {
g.setColor(new Color(255, 0, 0, 128));
int x = (int) p.getX();
int y = (int) p.getY();
g.drawLine(x - 1, y, x - 5, y);
g.drawLine(x + 1, y, x + 5, y);
g.drawLine(x, y - 1, x, y - 5);
g.drawLine(x, y + 1, x, y + 5);
}

public Area[] split(Area a, Point2D p1, Point2D p2) {

Shape str = new BasicStroke().createStrokedShape(new Line2D.Double(p1, p2));
Area line = new Area(str);
Area temp = a;
line.intersect(temp);
temp.exclusiveOr(line);
// temp is the shape with the line intersecting it

Area[] areas = {new Area(temp)};

return areas;
}

public JComponent getGui() {
return gui;
}

public static void main(String[] args) {
Runnable r = new Runnable() {

@Override
public void run() {
SplitArea sa = new SplitArea();
JOptionPane.showMessageDialog(null, sa.getGui());
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}

关于java - Java 中的形状和线段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21941156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com