2016-03-05 126 views
0

以下是處理草圖。座標之間的距離 - 角度> 180(Processing/Java)

死點座標爲0,0。

這意味着最左邊的x座標爲-250,最右邊的x座標爲250。

我想在屏幕中心移動鼠標,並在半徑上出現相對座標(即座標45,0處的鼠標應將標記點標記爲90,0)。

下面的代碼工作,但只適用於屏幕的右側,簡而言之,角度高達180.什麼是缺少這個工作在右側?

void draw(){ 
    background(0); 
    fill(255); 
    stroke(255); 
    strokeWeight(5); 
    translate(width/2,height/2); 

    // mark center 
    point(0,0); 
    strokeWeight(12); 

    // mark mouse position (follow with point) 
    float x = mouseX - width/2; 
    float y = mouseY - height/2; 
    point(x,y); 

    // trace point on radius of circle same radius as width 
    float radius = width/2; 

    float sinAng = y/radius; 
    float cosAng = x/radius; 
    float m = sinAng/cosAng; 
    float angle = atan(m); 
    float boundaryX = cos(angle)*width/2; 
    float boundaryY = sin(angle)*height/2; 

    stroke(255,0,0); 
    point(boundaryX,boundaryY); 

} 
+0

請澄清_「鼠標在座標45,0應標記點在90,0」_和您的意思是「不起作用」在畫布的左側。 –

回答

2

你計算m如果失去象限......

-x/-y = X/Y

使用x的符號只需要修正的角度向右象限和y。

+0

這也可以使用[Math#atan2(double,double)]來完成(https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2%28double,% 20double%29)使用相同原理的方法。 – Obicere

0

您可以使用atan2()函數以較少的步驟完成此操作。

atan2()函數有兩個參數:兩點之間在y距離,兩點之間的距離x,並返回這些點之間的角度:你可以擺脫幾行

angle = atan2(y1-y0, x1-x0); 

在程序中通過只是在做這樣的:

float x = mouseX - width/2; 
float y = mouseY - height/2; 
float angle = atan2(y, x); 
float boundaryX = cos(angle)*width/2; 
float boundaryY = sin(angle)*height/2; 

無需計算sinAngcosAng,或m變量。

相關問題