2015-04-03 42 views
2

我想創建一個Rectangle,它的一面與另一面不同。如何計算一個點的矩形座標及其與四個邊緣的距離

(所有的線描繪的意思是直線)

正常的矩形狀new Rectangle(50 /*LocationX*/, 50 */LocationY*/, 50 /*SizeX*/, 100 /*SizeY*/);產生的,看起來是這樣的: enter image description here

不過,我要像new Rectangle(50 /*LocationX*/, 50 */LocationY*/, 25 /*25 from the centre point for the red line*/, 30 /*30 from the centre point for the blue line*/, 50 /*50 from centre for green line*/, 100 /*100 from centre for yellow line*/);

構造換句話說,我實際上希望保持形狀相同,但移動中心點。

我該怎麼做?

+0

所以,你只是想通過定義一個點的水平邊緣距離和同一點的垂直邊緣距離來定義一個矩形? – caveman 2015-04-03 16:05:53

回答

2

在java中,rectangles由左上角座標,寬度和高度定義。

如果我明白你的問題在這裏說明什麼你矩形:在矩形點的

  • pointXpointY座標。命名爲
  • distanceToTop距離到矩形的頂部(綠線)。
  • distanceToBottom距離到矩形的底部(黃線)。
  • distanceToLeft距離距離點到矩形的左側(紅線)。
  • distanceToRight距離右邊的矩形(藍線)。

給出。矩形的左上角具有座標:

(pointX - distanceToLeft, pointY - distanceToTop) 

而整個矩形有大小(寬,高)

(distanceToLeft + distanceToRight, distanceToTop + distanceToBottom) 

所以你的情況將是:

Rectangle r = new Rectangle(
    pointX - distanceToLeft,   // upper-left corner X 
    pointY - distanceToTop,   // upper-left corner Y 
    distanceToLeft + distanceToRight, // width 
    distanceToTop + distanceToBottom // height 
    ); 
+0

矩形總是從左上角開始? – Joehot200 2015-04-03 16:45:44

+0

@ Joehot200,是的。我添加了一個鏈接到Java文檔。 – Orace 2015-04-03 18:18:56

相關問題