我打破我的脖子試圖解決這個問題... 該點應該位於鼠標指向的地方的邊界上。換句話說,在矩形邊界線和矩形中心與鼠標指針之間的一條線之間交叉。不知何故,我需要使用三角形,因爲矩形可能會旋轉。下面的代碼(在處理)讓我畫點,但僅在南邊界...如何在矩形的邊框上繪製點?
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)將提供從矩形中心到鼠標光標的線的角度(因爲在他的代碼中,鼠標相對於矩形的中心而定位)。最後,您的解決方案有四種變體,每個象限一個變體。 –