2012-12-23 15 views
0

我想實施我的項目this sketch關於拖動一個框。如何測試光標在圓上? (處理器)

而是一成箱的,我有好幾個圈各有形式

ellipse(lemma23.x, lemma23.y, diameterLemma23, diameterLemma23); 

    ellipse(law3.x, law3.y, diameterLaw3, diameterLaw3); 

    ellipse(law2.x, law2.y, diameterLaw2, diameterLaw2); 

如何測試如果光標在一個圈子中爲不同的座標繪製的?

這裏是我的項目的屏幕截圖:

enter image description here

我想測試時,光標(或附近)的圓圈,這樣我可以通過拖動改變其位置。

The entire sketch is in pastebin

回答

1

我從你的問題中的example開始。繪製多個形狀有幾個主要區別:

  1. 您必須檢查遊標是否在每個形狀內。
  2. 你必須繪製每個形狀。
  3. 你可能想擔心重疊,但我沒有。

在下面的代碼,我建立在例如直接雖然我除去點擊時,其改變框的顏色的幾行,我重組代碼到MovingEllipse類,使多個橢圓可以是輕鬆繪製。 (此代碼繪製兩個橢圓。)

請注意,draw()中的代碼檢查每個橢圓的鼠標位置,但是,我想這可以改進(也許通過創建一個橢圓位置數組並循環遍歷該陣列)。此外,爲使此代碼正常工作,需要像mouseDragged方法那樣複製mousePressedmouseReleased方法。 (我試圖讓我的例子簡要說明。)

無論如何,這是一種繪製多個橢圓並檢測哪一個應該移動的方法。希望能幫助到你!

int esize = 75; 

MovingEllipse e1 = new MovingEllipse(0.0, 0.0, esize, 0.0, 0.0); 
MovingEllipse e2 = new MovingEllipse(0.0, 0.0, esize, 0.0, 0.0); 

void setup() 
{ 
    size(640, 360); 
    e1.eX = width/2.0; // Center of ellipse 1. 
    e1.eY = height/2.0; 

    e2.eX = width/4.0; // Center of ellipse 2. 
    e2.eY = height/4.0; 
} 

void draw() 
{ 
    background(0); 

    // Test if the cursor is over the ellipse. 
    if (mouseX > e1.eX-esize && mouseX < e1.eX+esize && 
     mouseY > e1.eY-esize && mouseY < e1.eY+esize) { 
    e1.overBox = true; 
    e2.overBox = false; 
    } else if (mouseX > e2.eX-esize && mouseX < e2.eX+esize && 
     mouseY > e2.eY-esize && mouseY < e2.eY+esize) { 
    e2.overBox = true; 
    e1.overBox = false; 
    } else { 
    e1.overBox = false; 
    e2.overBox = false; 
    } 

    // Draw the ellipse(s). 
    e1.update(e1.eX, e1.eY, e1.overBox); 
    e2.update(e2.eX, e2.eY, e2.overBox); 
} 

void mouseDragged() { 
    e1.mouseDragged(); 
    e2.mouseDragged(); 
} 
// Don't forget to repeat this for mousePressed and mouseReleased! 
// ... 

class MovingEllipse { 
    float eX, eY;    // Position of ellipse. 
    int eSize;    // Radius. For a circle use eSize for both x and y radii. 
    float xOffset, yOffset; // Where user clicked minus center of ellipse. 
    boolean locked, overBox; // Flags used for determining if the ellipse should move. 

    MovingEllipse (float ex, float ey, int esize, float xoff, float yoff) { 
    eX = ex; 
    eY = ey; 
    eSize = esize; 
    xOffset = xoff; 
    yOffset = yoff; 
    } 

    void update(float ex, float ey, boolean over) { 
    eX = ex; 
    eY = ey; 
    overBox = over; 
    // Draw the ellipse. By default, (eX, eY) represents the center of the ellipse. 
    ellipse(eX, eY, eSize, eSize); 
    } 

    void mousePressed() { 
    if(overBox) { 
     locked = true; 
    } else { 
     locked = false; 
    } 
    xOffset = mouseX-eX; 
    yOffset = mouseY-eY; 
    } 
    void mouseDragged() { 
    if(locked) { 
     eX = mouseX-xOffset; 
     eY = mouseY-yOffset; 
    } 
    } 
    void mouseReleased(){ 
    locked = false; 
    } 
} 
+0

謝謝,加里。這按預期工作,我學到了很多東西。但在我的項目中,我會有數十個省略號,現在我正在考慮實施一種算法,以便我可以將最大的圈子(引用次數最多)置於中心位置和其他位置。我會先嚐試,如果我卡住了,我會在這裏問。再次感謝代碼。 – Zeynel

0

只要檢查光標與圓心的距離是否在命中半徑範圍內。命中半徑可以大於圓的半徑以捕獲接近命中。

+0

我該如何測試每個圓?每個都有不同的半徑。 – Zeynel

+0

'circles.FindFirst(circle => distance(circle.center,cursor)<= circle.hitRadius))' –

+0

對不起,這些都對我沒有意義。什麼是「圈子」,「FindFirst」,「hitRadius」? – Zeynel