我打破我的脖子试图解决这个问题... 该点应该位于鼠标指向的地方的边界上。换句话说,在矩形边界线和矩形中心与鼠标指针之间的一条线之间交叉。不知何故,我需要使用三角形,因为矩形可能会旋转。下面的代码(在处理)让我画点,但仅在南边界...如何在矩形的边框上绘制点?
int size = 200;
void setup() {
size(1200,500);
smooth();
ellipseMode(CENTER);
rectMode(CENTER);
}
void draw() {
background(255);
noFill();
PVector center = new PVector(width/2,height/2);
PVector mouse = new PVector(mouseX, mouseY);
float mouseDistance = PVector.dist(center,mouse);
mouse.sub(center); // mouse coordinates relative to center point
float t = atan2(mouse.y, mouse.x);
float borderDistance = (size/2)/sin(t);
float bx = cos(t) * borderDistance;
float by = sin(t) * borderDistance;
pushMatrix();
translate(center.x, center.y);
strokeWeight(5);
stroke(255,0,0);
point(0,0);
point(mouse.x, mouse.y);
point(cos(t)*size/2, sin(t)*size/2);
point(bx,by);
strokeWeight(1);
stroke(0);
ellipse(0, 0, size, size);
rect(0,0, size, size);
stroke(0,255,0);
line(0, 0, mouse.x, mouse.y);
popMatrix();
}
+1令人敬畏的图表。请注意,如果鼠标位于矩形的左侧,则响应可能会有所不同,因此该分段与西墙而不是南墙相交......他将不得不弄清楚他正在为完全一般回答。 –
是的,这是一个很好的观点。同样对于OP:我现在只是测试了这个,希望我能记得HighSchool的所有东西:) – Tyler
要确定象限,他首先需要矩形中心与任何角落之间的角度 - 因此,atan(height /宽度)。这将产生半角(如果绿色位于角落,则为中心至绿色)。然后atan(mouse.y/mouse.x)将提供从矩形中心到鼠标光标的线的角度(因为在他的代码中,鼠标相对于矩形的中心而定位)。最后,您的解决方案有四种变体,每个象限一个变体。 –