JavaFX - 交集运算



与并集运算类似,交集运算最初用于集合论。顾名思义,此运算定义为将两个不同集合相交成一个集合。因此,将从两个或多个集合中检索公共元素;忽略重复元素。然后,各种计算机编程技术采用此概念。

例如,它也用作 C、C++、Java、Python 等编程语言中的运算符或方法。类似地,JavaFX 也提供对二维图形的交集运算。

JavaFX 中的交集运算

JavaFX 允许您对二维图形执行交集运算,其中,两个或多个图形的区域相互交叉,并且这些图形的公共区域作为结果获得。因此,此运算将两个或多个图形作为输入,并返回它们之间的交集区域,如下所示。

Intersection Operation

您可以使用名为 `intersect()` 的方法对图形执行交集运算。由于这是一个静态方法,因此应使用类名(Shape 或其子类)调用它,如下所示。

Shape shape = Shape.intersect(circle1, circle2); 

以下是交集运算的示例。在这里,我们绘制两个圆形并对其执行交集运算。将此代码保存在名为 `IntersectionExample.java` 的文件中。

示例

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Shape; 
         
public class IntersectionExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing Circle1 
      Circle circle1 = new Circle();
      
      //Setting the position of the circle 
      circle1.setCenterX(250.0f); 
      circle1.setCenterY(135.0f); 
      
      //Setting the radius of the circle 
      circle1.setRadius(100.0f); 
      
      //Setting the color of the circle 
      circle1.setFill(Color.DARKSLATEBLUE);     
       
      //Drawing Circle2 
      Circle circle2 = new Circle();         
      
      //Setting the position of the circle 
      circle2.setCenterX(350.0f); 
      circle2.setCenterY(135.0f); 
      
      //Setting the radius of the circle  
      circle2.setRadius(100.0f); 
      
      //Setting the color of the circle 
      circle2.setFill(Color.BLUE);  
       
      //Performing intersection operation on the circle 
      Shape shape = Shape.intersect(circle1, circle2); 
      
      //Setting the fill color to the result 
      shape.setFill(Color.DARKSLATEBLUE); 
       
      //Creating a Group object  
      Group root = new Group(shape); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Intersection Example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene);
      
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}       

使用以下命令从命令提示符编译并执行保存的 java 文件。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls IntersectionExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls IntersectionExample

输出

执行后,上述程序将生成一个 JavaFX 窗口,显示以下输出:

Intersection Operation Output

示例

现在,让我们尝试对两个椭圆形执行交集运算(以检索它们之间的公共区域)。将此文件保存在名为 `EllipseIntersectionOperation.java` 的位置。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.scene.shape.Ellipse; 
import javafx.scene.shape.Shape; 
         
public class EllipseIntersectionOperation extends Application {  
   @Override 
   public void start(Stage stage) { 
      Ellipse ellipse1 = new Ellipse();               
      ellipse1.setCenterX(250.0f); 
      ellipse1.setCenterY(100.0f); 
      ellipse1.setRadiusX(150.0f); 
      ellipse1.setRadiusY(75.0f); 
      ellipse1.setFill(Color.BLUE);     

      Ellipse ellipse2 = new Ellipse();      
      ellipse2.setCenterX(350.0f); 
      ellipse2.setCenterY(100.0f); 
      ellipse2.setRadiusX(150.0f); 
      ellipse2.setRadiusY(75.0f);     
      ellipse2.setFill(Color.RED);  
 
      Shape shape = Shape.intersect(ellipse1, ellipse2); 
      
      //Setting the fill color to the result 
      shape.setFill(Color.DARKSLATEBLUE); 
       
      //Creating a Group object  
      Group root = new Group(shape); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Intersection Example"); 
         
      //Adding scene to the stage  
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}      

使用以下命令从命令提示符编译并执行保存的 java 文件。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseIntersectionOperation.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseIntersectionOperation

输出

执行后,上述程序将生成一个 JavaFX 窗口,显示以下输出:

Intersection Operation Ellipse
广告
© . All rights reserved.