在Java中,Line2D類表示二維空間中的直線。如果你需要將Line2D對象的坐標進行轉換,可以使用以下方法:
要將Line2D對象沿x軸和y軸移動到新的位置,可以使用以下代碼:
import java.awt.geom.Line2D;
public class Main {
public static void main(String[] args) {
Line2D line = new Line2D.Double(1, 2, 3, 4);
// 平移向量
double dx = 5;
double dy = -3;
// 計算新的起點和終點
double newStartX = line.getX1() + dx;
double newStartY = line.getY1() + dy;
double newEndX = line.getX2() + dx;
double newEndY = line.getY2() + dy;
// 創建新的Line2D對象
Line2D newLine = new Line2D.Double(newStartX, newStartY, newEndX, newEndY);
System.out.println("原始直線: (" + line.getX1() + ", " + line.getY1() + ") - (" + line.getX2() + ", " + line.getY2() + ")");
System.out.println("平移后的直線: (" + newLine.getX1() + ", " + newLine.getY1() + ") - (" + newLine.getX2() + ", " + newLine.getY2() + ")");
}
}
要將Line2D對象沿x軸和y軸進行縮放,可以使用以下代碼:
import java.awt.geom.Line2D;
public class Main {
public static void main(String[] args) {
Line2D line = new Line2D.Double(1, 2, 3, 4);
// 縮放因子
double scaleX = 2;
double scaleY = 0.5;
// 計算新的起點和終點
double newStartX = line.getX1() * scaleX;
double newStartY = line.getY1() * scaleY;
double newEndX = line.getX2() * scaleX;
double newEndY = line.getY2() * scaleY;
// 創建新的Line2D對象
Line2D newLine = new Line2D.Double(newStartX, newStartY, newEndX, newEndY);
System.out.println("原始直線: (" + line.getX1() + ", " + line.getY1() + ") - (" + line.getX2() + ", " + line.getY2() + ")");
System.out.println("縮放后的直線: (" + newLine.getX1() + ", " + newLine.getY1() + ") - (" + newLine.getX2() +