new version
// https : // forum.processing.org/two/discussion/10474/find-angle-between-2-points
// https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/16
final int UNDEFINED = -1;
PVector centerPoint; // the fixed red point
// make an int dir a String dir
String[] textFromDir = {
"NONE",
"Right",
"Right, down",
"down",
"down, left",
"left",
"left, up",
"up",
"up, right",
};
String text1="";
int dirFromMouse=0;
//---------------------------------------------------------------------------------------
void setup() {
size(1200, 600);
centerPoint = new PVector(width/2, height/2);
}
void draw() {
background(0);
// draw a simple cross at centerPoint
crossAtPV(centerPoint);
// show centerPoint in red
ellipsePV(centerPoint);
//get mouse pvector
PVector mousePV=new PVector(mouseX, mouseY);
// get angle
float angle = angleBetweenPV_PV(centerPoint, mousePV);
angle=fixAngle(angle);
// check Area !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
float angleInDegrees=degrees(angle);
float scliceDegree = 360/8 ;
dirFromMouse = UNDEFINED;
// dist mouse center must be < 240
if (centerPoint.dist(mousePV)<200) {
if ( isBetween (angleInDegrees, 0*scliceDegree, 1*scliceDegree) ) dirFromMouse = 0;
else if ( isBetween (angleInDegrees, 1*scliceDegree, 2*scliceDegree) ) dirFromMouse = 1;
else if ( isBetween (angleInDegrees, 2*scliceDegree, 3*scliceDegree) ) dirFromMouse = 2;
else if ( isBetween (angleInDegrees, 3*scliceDegree, 4*scliceDegree) ) dirFromMouse = 3;
else if ( isBetween (angleInDegrees, 4*scliceDegree, 5*scliceDegree) ) dirFromMouse = 4;
else if ( isBetween (angleInDegrees, 5*scliceDegree, 6*scliceDegree) ) dirFromMouse = 5;
else if ( isBetween (angleInDegrees, 6*scliceDegree, 7*scliceDegree) ) dirFromMouse = 6;
else if ( isBetween (angleInDegrees, 7*scliceDegree, 8*scliceDegree) ) dirFromMouse = 7;
else {
// ignore
}
}//if
// show red lines
for (int i = 0; i < 9; i++) {
stroke(255, 2, 2); // RED
line(centerPoint.x, centerPoint.y,
cos(radians(i*scliceDegree- 360/16))*90 +centerPoint.x, sin(radians(i*scliceDegree- 360/16))*90 +centerPoint.y );
}
if (dirFromMouse>UNDEFINED) {
// show yellow triangle (when mouse on)
stroke(255, 2, 2);
int i2 = dirFromMouse+1;
fill(240, 255, 44); // YELLOW
stroke(255, 2, 2); // RED
trianglePV ( centerPoint,
getPointOnCircle ( centerPoint, radians(dirFromMouse*scliceDegree- 360/16), 90 ),
getPointOnCircle ( centerPoint, radians(i2 *scliceDegree- 360/16), 90 ) );
}
// show the red triangle
fill(255, 0, 0); // red
triangleMy(angle);
fill(255); // white
// upper left corner
text(angle
+"\n"
+degrees(angle)
+ "\n"
+ dirFromMouse
+ ": "
+ textFromDir[ dirFromMouse+1 ],
23, 23);
// upper right corner: on mousePressed()
fill(255);
text(text1,
width-210, 23);
}
// ------------------------------------------------------------------------------------------------
void mousePressed() {
switch(dirFromMouse) {
case UNDEFINED:
text1="";
break;
default:
text1 = "We got "
+textFromDir[ dirFromMouse+1 ];
break;
}//switch
}//func
// --------------------------------------------------------------------
boolean isBetween(float inputValue, float down, float up) {
return
inputValue>down- 360/16 &&
inputValue<up- 360/16;
}
// --------------------------------------------------------------------
float angleBetweenPV_PV(PVector centerPV, PVector movingPV) {
// calc angle : the core of the sketch
PVector d = new PVector();
// calc angle
// delta
d.x = movingPV.x - centerPV.x;
d.y = movingPV.y - centerPV.y;
// angle
float angle1 = atan2(d.y, d.x);
return angle1;
}
void triangleMy(float ang) {
pushMatrix();
translate(centerPoint.x, centerPoint.y);
rotate(ang);
fill(255); // white
triangle(60, 0,
80, -30,
80, 30);
popMatrix();
}
void ellipsePV(PVector pv) {
fill(255, 0, 0); // red
ellipse(pv.x, pv.y, 10, 10);
}
void trianglePV(PVector pv1, PVector pv2, PVector pv3) {
triangle (pv1.x, pv1.y,
pv2.x, pv2.y,
pv3.x, pv3.y);
}
void crossAtPV(PVector pv) {
stroke(255);
line(pv.x, 0, pv.x, height);
line(0, pv.y, width, pv.y);
}
PVector getPointOnCircle ( PVector center, float angle, float radius ) {
// calcs a point on a circle from some data
return
new PVector(
cos(angle)*radius +center.x,
sin(angle)*radius +center.y );
}
float fixAngle(float angle1) {
// repairs / beautify angle
// if > 2xPI
if (angle1>TWO_PI)
angle1-=TWO_PI;
if (angle1>TWO_PI)
angle1-=TWO_PI;
// if < 0 (negative angles like -40 should be expressed as positive angles like 320)
if (angle1<0.0) {
angle1 = TWO_PI + angle1;
}
if (angle1<0.0)
angle1 = TWO_PI + angle1;
return angle1;
}
//