如何合并两个多边形以便输出是多边形而不是路径(javafx)?
How to unite two polygons so that output is polygon not path (javafx)?
我有两个多边形 class 对象,我想使用 javafx.scene.shape.Shape 中的 union() 方法合并它们,我需要输出也是多边形 class 对象。我发现了几个输出为路径对象的示例:
Path path = Path.union(shape1, shape2)
或者是否可以将路径转换为多边形?我尝试 google 但没有成功。
这就是我要说的。
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
public class JavaFXApplication31 extends Application {
@Override
public void start(Stage primaryStage) {
Polygon p1 = new Polygon();
p1.getPoints().addAll(new Double[]{
60.0, 60.0,
80.0, 70.0,
70.0, 80.0 });
Polygon p2 = new Polygon();
p2.getPoints().addAll(new Double[]{
40.0, 40.0,
60.0, 50.0,
50.0, 60.0 });
AnchorPane root = new AnchorPane();
Button btn = new Button();
btn.setText("Click me!'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Polygon unioned!");
Shape union = Polygon.union(p1, p2);
union.setFill(Color.BLUE);
root.getChildren().add(union);
}
});
Button btn2 = new Button();
btn2.setText("New Polygon union!'");
btn2.setLayoutX(100);
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Polygon p3 = createNewPolygonUnion(p1, p2);
p3.setFill(Color.RED);
root.getChildren().add(p3);
}
});
root.getChildren().addAll(p1, p2, btn, btn2);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
Polygon createNewPolygonUnion(Polygon one, Polygon two)
{
Polygon p3 = new Polygon();
ArrayList<Double> onePoints = new ArrayList();
ArrayList<Double> twoPoints = new ArrayList();
ArrayList<Double> threePoints = new ArrayList();
for(int i = 0; i < one.getPoints().size(); i++)
{
onePoints.add(one.getPoints().get(i));
twoPoints.add(two.getPoints().get(i));
}
for(int i = 0; i < onePoints.size(); i++)
{
threePoints.add(onePoints.get(i) + twoPoints.get(i));
}
p3.getPoints().addAll(threePoints);
return p3;
}
}
Original two polygons.
Union of the two original polygons. The union of the two polygons does not
result in a polygon.
Union from a method I created. This method uses the points from the
original two polygons and create a new polygon.
当你对两个多边形进行联合时,你会得到一个 Path 对象,如你所说。可以将此路径对象转换为多边形对象(不是最好的代码但可行)。
//createing both polygon objects p1 and p2
Polygon p1 = new Polygon();
p1.getPoints().addAll(new Double[]{
0.0, 0.0,
20.0, 10.0,
10.0, 20.0 });
Polygon p2 = new Polygon();
p2.getPoints().addAll(new Double[]{
10.0, 0.0,
25.0, 10.0,
5.0, 20.0 });
// Taking the union that results in a Path object
Path p3 = (Path) Polygon.union(p1, p2);
// Array of points for the new polygon
Double[] points = new Double[(p3.getElements().size() - 1)*2];
int i = 0;
// going through all the path elements in the path and adding the x and y coordinates to points
for(PathElement el : p3.getElements()){
if(el instanceof MoveTo){
MoveTo mt = (MoveTo) el;
points[i] = mt.getX();
points[i+1] = mt.getY();
}
if(el instanceof LineTo){
LineTo lt = (LineTo) el;
points[i] = lt.getX();
points[i+1] = lt.getY();
}
i += 2;
}
// creating new Polygon with these points
Polygon newPolygon = new Polygon();
newPolygon.getPoints().addAll(points);
如果多边形不相交,这可能不起作用,但否则应该可以。如果没有交集,您将有两个 MoveTo 语句而不是一个。
我有两个多边形 class 对象,我想使用 javafx.scene.shape.Shape 中的 union() 方法合并它们,我需要输出也是多边形 class 对象。我发现了几个输出为路径对象的示例:
Path path = Path.union(shape1, shape2)
或者是否可以将路径转换为多边形?我尝试 google 但没有成功。
这就是我要说的。
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
public class JavaFXApplication31 extends Application {
@Override
public void start(Stage primaryStage) {
Polygon p1 = new Polygon();
p1.getPoints().addAll(new Double[]{
60.0, 60.0,
80.0, 70.0,
70.0, 80.0 });
Polygon p2 = new Polygon();
p2.getPoints().addAll(new Double[]{
40.0, 40.0,
60.0, 50.0,
50.0, 60.0 });
AnchorPane root = new AnchorPane();
Button btn = new Button();
btn.setText("Click me!'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Polygon unioned!");
Shape union = Polygon.union(p1, p2);
union.setFill(Color.BLUE);
root.getChildren().add(union);
}
});
Button btn2 = new Button();
btn2.setText("New Polygon union!'");
btn2.setLayoutX(100);
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Polygon p3 = createNewPolygonUnion(p1, p2);
p3.setFill(Color.RED);
root.getChildren().add(p3);
}
});
root.getChildren().addAll(p1, p2, btn, btn2);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
Polygon createNewPolygonUnion(Polygon one, Polygon two)
{
Polygon p3 = new Polygon();
ArrayList<Double> onePoints = new ArrayList();
ArrayList<Double> twoPoints = new ArrayList();
ArrayList<Double> threePoints = new ArrayList();
for(int i = 0; i < one.getPoints().size(); i++)
{
onePoints.add(one.getPoints().get(i));
twoPoints.add(two.getPoints().get(i));
}
for(int i = 0; i < onePoints.size(); i++)
{
threePoints.add(onePoints.get(i) + twoPoints.get(i));
}
p3.getPoints().addAll(threePoints);
return p3;
}
}
Original two polygons.
Union of the two original polygons. The union of the two polygons does not result in a polygon.
Union from a method I created. This method uses the points from the original two polygons and create a new polygon.
当你对两个多边形进行联合时,你会得到一个 Path 对象,如你所说。可以将此路径对象转换为多边形对象(不是最好的代码但可行)。
//createing both polygon objects p1 and p2
Polygon p1 = new Polygon();
p1.getPoints().addAll(new Double[]{
0.0, 0.0,
20.0, 10.0,
10.0, 20.0 });
Polygon p2 = new Polygon();
p2.getPoints().addAll(new Double[]{
10.0, 0.0,
25.0, 10.0,
5.0, 20.0 });
// Taking the union that results in a Path object
Path p3 = (Path) Polygon.union(p1, p2);
// Array of points for the new polygon
Double[] points = new Double[(p3.getElements().size() - 1)*2];
int i = 0;
// going through all the path elements in the path and adding the x and y coordinates to points
for(PathElement el : p3.getElements()){
if(el instanceof MoveTo){
MoveTo mt = (MoveTo) el;
points[i] = mt.getX();
points[i+1] = mt.getY();
}
if(el instanceof LineTo){
LineTo lt = (LineTo) el;
points[i] = lt.getX();
points[i+1] = lt.getY();
}
i += 2;
}
// creating new Polygon with these points
Polygon newPolygon = new Polygon();
newPolygon.getPoints().addAll(points);
如果多边形不相交,这可能不起作用,但否则应该可以。如果没有交集,您将有两个 MoveTo 语句而不是一个。