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
廣告