坐标之间的距离 - 角度> 180(处理/ Java)

时间:2016-03-05 19:19:14

标签: java processing

以下是处理草图。

死点中心坐标为0,0。

这意味着远左x坐标为-250,最右边x坐标为250. y的相似尺寸。

我想围绕屏幕中心移动鼠标并使相对坐标出现在半径上(即坐标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);  

}

2 个答案:

答案 0 :(得分:2)

你在计算m ...时失去了象限。

-x / -y = x / y

使用x和y的符号纠正右象限的角度。

答案 1 :(得分: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;

无需计算sinAngcosAngm变量。