Class PhysicalRectangle

java.lang.Object
io.leandev.appfuse.measure.PhysicalRectangle

public class PhysicalRectangle extends Object

代表具有物理單位的矩形區域

Rectangle 不同,此類別使用 Length 物件來表示座標和尺寸, 提供更強的類型安全性和單位轉換支援。

使用範例:

PhysicalRectangle rect = new PhysicalRectangle(
    Length.ofCentimeter(0),
    Length.ofCentimeter(0),
    Length.ofCentimeter(21),
    Length.ofCentimeter(29.7)
);

double area = rect.getArea(Length.Unit.MILLIMETER);  // 面積(平方毫米)
boolean fits = rect.fits(Paper.A4);                   // 是否適合 A4 紙張
  • Constructor Details

    • PhysicalRectangle

      public PhysicalRectangle(Length x, Length y, Length width, Length height)
      建立具有物理單位的矩形
      Parameters:
      x - 左上角 X 座標
      y - 左上角 Y 座標
      width - 寬度(必須為正數)
      height - 高度(必須為正數)
      Throws:
      IllegalArgumentException - 如果任何參數為 null 或寬高為非正數
  • Method Details

    • ofPaper

      public static PhysicalRectangle ofPaper(Paper paper)
      使用指定紙張尺寸建立矩形(從原點開始)
      Parameters:
      paper - 紙張規格
      Returns:
      對應紙張尺寸的矩形
    • getX

      public Length getX()
    • getY

      public Length getY()
    • getWidth

      public Length getWidth()
    • getHeight

      public Length getHeight()
    • getArea

      public double getArea(Length.Unit unit)
      計算矩形面積
      Parameters:
      unit - 結果單位
      Returns:
      面積(單位的平方)
    • getPerimeter

      public double getPerimeter(Length.Unit unit)
      計算矩形周長
      Parameters:
      unit - 結果單位
      Returns:
      周長
    • fits

      public boolean fits(Paper paper)
      檢查此矩形是否適合指定的紙張尺寸(不考慮位置)
      Parameters:
      paper - 紙張規格
      Returns:
      如果寬度和高度都不超過紙張尺寸則返回 true
    • fits

      public boolean fits(Paper paper, Length margin)
      檢查此矩形是否適合指定的紙張尺寸(包含邊距)
      Parameters:
      paper - 紙張規格
      margin - 四邊邊距
      Returns:
      如果加上邊距後仍適合紙張則返回 true
    • toRectangle

      public Rectangle<Double> toRectangle(Length.Unit unit)
      將矩形轉換為指定單位的 Rectangle<Double>
      Parameters:
      unit - 目標單位
      Returns:
      轉換後的 Rectangle
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object