2011-06-13 58 views
2

我正在使用Raphaeljs創建一個用於創建圖表的Web應用程序。其中一種形狀是鑽石,它只是一個旋轉45度的矩形。我需要測試一個圓圈是否落在旋轉後的圖像中,我不知道如何去做。如何測試一個圓是否在旋轉的對象中

回答

2

如果你有一個鑽石:

var diamond = paper.rect(rect_x, rect_y, size, size); 
diamond.rotate(45); 

然後在水平與兩個角具有座標(NX1,NY)和(NX2,NY),其中

ny = rect_y + rect_w/2; 
nx1 = rect_x + rect_w*(1-Math.sqrt(2))/2; 
nx2 = rect_x + rect_w*(1+Math.sqrt(2))/2; 

然後創建這個圓內半徑小的鑽石。

var circle = paper.circle(cx, cy, r); 
nx1 += r*Math.sqrt(2); 
nx2 -= r*Math.sqrt(2); 

Diagram showing circle in diamond

然後你測試圓心是否是此鑽石的四邊之間:

if (cy < ny - nx1 + cx && 
    cy > ny - nx2 + cx && 
    cy > ny + nx1 - cx && 
    cy < ny + nx2 - cx) 
    {Circle is inside the diamond} 
+0

啊,現在看來如此明顯!謝謝! – dave 2011-06-20 19:11:17

相關問題