# Check direction of the mouse compared to a point in space

**URL:** https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883
**Category:** Coding Questions
**Created:** [July 22, 2019, 9:44am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883 "2019-07-22T09:44:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 9:44am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/1 "2019-07-22T09:44:37Z")

</div>

Hi, i wonder how to check and know the directions (left,right,up and down) of the mouse compared to a point in space.

```auto
void setup(){
  size(300,300);
}

void draw(){
  background(255);
  PVector dotPos = new PVector(150,150);
  fill(255,0,0);
  ellipse(dotPos.x, dotPos.y, 10, 10);
  
  PVector mousePos = new PVector(mouseX,mouseY);
  line(dotPos.x, dotPos.y,mousePos.x,mousePos.y);
  
  if(mousePos.y < dotPos.y){
    text("up",10,10);
  }
  if(mousePos.y > dotPos.y){
    text("down",10,10);
  }
  if(mousePos.x < dotPos.x){
    text("left",10,10);
  }
  if(mousePos.x > dotPos.x){
    text("right",10,10);
  }
  
}

```

I know its the wrong way to do it, but I have difficulties with the Switch Statements ( i think this is the good way to do it but i don’t know how)

thanks for your help and time.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [July 22, 2019, 10:05am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/2 "2019-07-22T10:05:10Z")

</div>

just a rewrite with some code styling ideas

```auto
PVector dotPos = new PVector(150, 150);
PVector mousePos = new PVector(mouseX, mouseY);
String sDir1,sDir2;

void setup() {
  size(300, 300);
}

void draw() {
  background(255);
  draw_mousepos();
}

void draw_mousepos() {
  noStroke();
  mousePos.set(mouseX, mouseY);
  fill(200, 0, 0);
  circle(dotPos.x, dotPos.y, 10);
  fill(0, 0, 200);
  circle(mousePos.x, mousePos.y, 10);
  stroke(0,200,0);
  line(dotPos.x, dotPos.y, mousePos.x, mousePos.y);

  if (mousePos.y < dotPos.y) sDir1 = "up";
  else sDir1 = "down";

  if (mousePos.x < dotPos.x) sDir2 = "left";
  else sDir2 = "right";
  text(sDir1+" "+sDir2, 10, 10);
}

```

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 10:17am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/3 "2019-07-22T10:17:07Z")

</div>

Thanks, I did not think about that, i will try it on my code

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 10:45am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/4 "2019-07-22T10:45:39Z")

</div>

its solve one of my future problem (checking to have up left/right and down left/right), but i need to have a way to check when its up,down,left and right only too.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [July 22, 2019, 10:53am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/5 "2019-07-22T10:53:28Z")

</div>

? not understand the question,  
you do ( needed to ) have 2 separate variables  
well you not need to use string variables at first, but the KNOWLEDGE is stored?  
while your above code not stored anything AND even overwrite text at same position…

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 11:00am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/6 "2019-07-22T11:00:41Z")

</div>

sorry if my question was not clear, what i looking for is a way to check when the mouse is up,down,left and right of the point. I know my first code is wrong and it overwrite text, it was to point my problem and my bad code

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 22, 2019, 11:01am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/7 "2019-07-22T11:01:22Z")

</div>

> [@vincentstrebelle](#):
>
> down,left and right **only**

Remark

It would be very hard to move the mouse y for example exactly to the y position of the point

Instead you should check for example

- if mouse X is smaller than your point X and
- the distance of your mouse Y from the point Y is smaller than a threshold of let‘s say 30

`.....dist(0,mouseY, 0, dotPos.y) < 30.....`

then you’re LEFT

Chrisir

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 11:05am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/8 "2019-07-22T11:05:34Z")

</div>

ok, i start to understand how to solve my problem. i need to have four “area” one for each directions and when the mouse is in one of these area my text say the correct direction (area).

![Untitled](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/78c2780dabacc628df3ac5ee2f0c6a1c9b5c3376.jpeg)  
something like that

am i right ?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 22, 2019, 11:14am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/9 "2019-07-22T11:14:16Z")

</div>

```auto

PVector dotPos = new PVector(150, 150);
PVector mousePos = new PVector();
String sDir1;

void setup() {
  size(300, 300);
  background(255);
}

void draw() {
  background(255);
  draw_mousepos();
}

// -------------------------------------------------------------

void draw_mousepos() {

  mousePos.set(mouseX, mouseY);

  noStroke();
  fill(200, 0, 0);
  ellipse(dotPos.x, dotPos.y, 10, 10);
  fill(0, 0, 200);
  ellipse(mousePos.x, mousePos.y, 10, 10);

  stroke(0, 200, 0);
  line(dotPos.x, dotPos.y, 
    mousePos.x, mousePos.y);

  sDir1 = "";

  if (mousePos.y < dotPos.y-3 && near( mouseX, dotPos.x ) ) sDir1 = "up";
  else if (mousePos.y > dotPos.y+3 && near( mouseX, dotPos.x ) ) sDir1 = "down";
  else if (mousePos.x < dotPos.x-3 && near( mouseY, dotPos.y ) ) sDir1 = "left";
  else if (mousePos.x > dotPos.x+3 && near( mouseY, dotPos.y ) ) sDir1 = "right";

  fill(0);
  text(sDir1, 10, 10);
}

boolean near( float a, float b) {
  return
    dist(0, a, 0, b) < 30;
}
//

```

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 11:21am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/10 "2019-07-22T11:21:46Z")

</div>

ok so in theory i was right 🙂  
thanks i gonna test that

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 22, 2019, 11:27am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/11 "2019-07-22T11:27:15Z")

</div>

> ok so in theory i was right

No, the checked areas are not round but rectangles in my version:

![Unbenannt](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c4181624e6556746feb20835c06476acb604cce9.png)

there are overlapping areas, here the order of the if-else-clauses decides…

```auto

PVector dotPos = new PVector(150, 150);
PVector mousePos = new PVector();
String sDir1;

void setup() {
  size(300, 300);
  background(255);
}

void draw() {
  background(255);
  draw_mousepos();
}

// -------------------------------------------------------------

void draw_mousepos() {

  mousePos.set(mouseX, mouseY);

  noStroke();
  fill(200, 0, 0);
  ellipse(dotPos.x, dotPos.y, 10, 10);
  fill(0, 0, 200);
  ellipse(mousePos.x, mousePos.y, 10, 10);

  stroke(0, 200, 0);
  // top 
  fill(255, 0, 0, 10); // red
  rect(dotPos.x-30, 0, 
    60, dotPos.y - 3);
  // bottom
  fill(0, 255, 0, 10); //green 
  rect(dotPos.x-30, dotPos.y + 3, 
    60, 300);

  // right
  fill(0, 0, 255, 10); // blue 
  rect(dotPos.x+3, dotPos.y - 30, 
    660, 60);
  // left 
  fill(255, 255, 0, 10); 
  rect(0, dotPos.y - 30, 
    dotPos.x-3, 60);

  stroke(0, 200, 0);
  line(dotPos.x, dotPos.y, 
    mousePos.x, mousePos.y);

  sDir1 = "";

  if (mousePos.y < dotPos.y-3 && near( mouseX, dotPos.x ) ) sDir1 = "up";
  else if (mousePos.y > dotPos.y+3 && near( mouseX, dotPos.x ) ) sDir1 = "down";
  else if (mousePos.x < dotPos.x-3 && near( mouseY, dotPos.y ) ) sDir1 = "left";
  else if (mousePos.x > dotPos.x+3 && near( mouseY, dotPos.y ) ) sDir1 = "right";

  fill(0);
  text(sDir1, 10, 10);
}

boolean near( float a, float b) {
  return
    dist(0, a, 0, b) < 30;
}
//

```

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 11:31am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/12 "2019-07-22T11:31:55Z")

</div>

ok wow so my problem is harder that i imagined

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 22, 2019, 11:33am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/13 "2019-07-22T11:33:22Z")

</div>

> [@vincentstrebelle](#):
>
> imagined

it’s not hard at all.

What do you mean?

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 11:52am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/14 "2019-07-22T11:52:37Z")

</div>

well it create a lot of void between each rectangle, im sure i can modify it by making them wider.

thanks to you, now i can clearly display what im looking for.

this is what im looking for:  
4 directions:  
 ![Untitled](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fbba3c1673996ad9478e8cf0530e52895f7609e3.jpeg)  
8 directions:  
 ![Untitled2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c432a45e8c67bb7889a41cb7c02e86a4c1001973.jpeg)

i’m deeply sorry for wasting yours times by not express myself correctly.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 22, 2019, 11:53am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/15 "2019-07-22T11:53:31Z")

</div>

in this version we also check if the mouse is near (100) the reference point in the first place, before checking the areas

```auto

PVector dotPos = new PVector(150, 150);
PVector mousePos = new PVector();
String sDir1;

void setup() {
  size(300, 300);
  background(255);
}

void draw() {
  background(255);
  draw_mousepos();
}

// -------------------------------------------------------------

void draw_mousepos() {

  mousePos.set(mouseX, mouseY);

  noStroke();
  fill(200, 0, 0);
  ellipse(dotPos.x, dotPos.y, 10, 10);
  fill(0, 0, 200);
  ellipse(mousePos.x, mousePos.y, 10, 10);

  showFourRects(); 

  stroke(0, 200, 0);
  line(dotPos.x, dotPos.y, 
    mousePos.x, mousePos.y);

  sDir1 = "";

  if (mousePos.dist(dotPos) < 100) {
    if (mousePos.y < dotPos.y-3 && near( mouseX, dotPos.x ) ) sDir1 = "up";
    else if (mousePos.y > dotPos.y+3 && near( mouseX, dotPos.x ) ) sDir1 = "down";
    else if (mousePos.x < dotPos.x-3 && near( mouseY, dotPos.y ) ) sDir1 = "left";
    else if (mousePos.x > dotPos.x+3 && near( mouseY, dotPos.y ) ) sDir1 = "right";
    else { //ignore
    }
  }//if

  fill(0);
  text(sDir1, 10, 10);
}

boolean near( float a, float b) {
  return
    dist(0, a, 0, b) < 30;
}

void showFourRects() {

  stroke(0, 200, 0);
  // top 
  fill(255, 0, 0, 10); // red
  rect(dotPos.x-30, dotPos.y - 100, 
    60, 100 - 3);

  // bottom
  fill(0, 255, 0, 10); //green 
  rect(dotPos.x-30, dotPos.y + 3, 
    60, 100-3 );

  // right
  fill(0, 0, 255, 10); // blue 
  rect(dotPos.x+3, dotPos.y - 30, 
    100-3, 60);

  // left 
  fill(255, 255, 0, 10); // other color 
  rect(dotPos.x-100, dotPos.y - 30, 
    100-3, 60);

  // show circle to symbolie: if (mousePos.dist(dotPos) < 100) {
  stroke(0);
  noFill();
  ellipse( dotPos.x, dotPos.y, 200, 200);
}
//

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 22, 2019, 12:13pm UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/16 "2019-07-22T12:13:39Z")

</div>

totally new approach

uses the angle between mouse and center point

(angle 0 is on the right side, not on the top in processing)

(also remember, it’s in radians mostly, not degrees )

```auto

// https : // forum.processing.org/two/discussion/10474/find-angle-between-2-points

PVector centerPoint; // the fixed red point 

// make an int a String 
String[] textFromDir = {
  "Right", 
  "Right,down", 
  "down", 
  "down, left", 
  "left", 
  "left up", 
  "up", 
  "up right", 
}; 

void setup() {
  size(1200, 600);
  centerPoint = new PVector(width/2, height/2);
}

void draw() {  
  background(0);

  // draw arc 
  fill(255); 
  //arc( centerPoint.x, centerPoint.y, 
  // 430, 430, 
  // radians(60), radians(90) );

  // draw a simple cross at centerPoint 
  crossAtPV(centerPoint); 

  // show centerPoint in red 
  ellipsePV(centerPoint); 

  // get angle 
  float angle = angleBetweenPV_PV(centerPoint, new PVector(mouseX, mouseY));

  angle=fixAngle(angle);

  // check Area !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  int dir=0;
  float angleInDegrees=degrees(angle);
  float scliceDegree = 360/8 ; 

  dir = 0;

  if ( isBetween (angleInDegrees, 0*scliceDegree, 1*scliceDegree) ) dir = 0;
  else if ( isBetween (angleInDegrees, 1*scliceDegree, 2*scliceDegree) ) dir = 1;
  else if ( isBetween (angleInDegrees, 2*scliceDegree, 3*scliceDegree) ) dir = 2;

  else if ( isBetween (angleInDegrees, 3*scliceDegree, 4*scliceDegree) ) dir = 3;
  else if ( isBetween (angleInDegrees, 4*scliceDegree, 5*scliceDegree) ) dir = 4;
  else if ( isBetween (angleInDegrees, 5*scliceDegree, 6*scliceDegree) ) dir = 5;

  else if ( isBetween (angleInDegrees, 6*scliceDegree, 7*scliceDegree) ) dir = 6;
  else if ( isBetween (angleInDegrees, 7*scliceDegree, 8*scliceDegree) ) dir = 7;

  // show the found angle
  fill(255, 0, 0); // red
  triangleMy(angle);
  fill(255); // white

  text(angle
    +"\n"
    +degrees(angle) + "\n" + dir + ": " + textFromDir[dir], 23, 23);
}

// --------------------------------------------------------------------

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 shield 
  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 crossAtPV(PVector pv) {
  stroke(255);
  line(pv.x, 0, pv.x, height);
  line(0, pv.y, width, pv.y);
}
// 
float fixAngle(float angle1) {
  // 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;
}
//

```

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 22, 2019, 12:20pm UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/17 "2019-07-22T12:20:01Z")

</div>

amazing just amazing, i will try it on my code. Many thanks for your patience and answers.  
i will check the link too.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 22, 2019, 9:23pm UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/18 "2019-07-22T21:23:18Z")

</div>

new version

```auto
// 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;
}
//

```

---

<div class="post-metadata">

### Author: ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)
#### Post date: [July 23, 2019, 10:19am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/19 "2019-07-23T10:19:34Z")

</div>

There is a way to get rid of those **if-else statements** for checking the directions to shrinkify the codes and makes life easier.

I came with this.

```auto

final int DIRS = 16;
//final int DIRS = 8;
//final int DIRS = 4;

// IF 16 DIRECTIONS ARE NEEDED
String[] dirName ={
  "W", 
  "WNW", 
  "NW", 
  "NNW", 
  "N", 
  "NNE", 
  "NE", 
  "ENE", 
  "E", 
  "ESE", 
  "SE", 
  "SSE", 
  "S", 
  "SSW", 
  "SW", 
  "WSW", 
  "W"
};

// IF 8 DIRECTIONS ARE NEEDED
String[] dirName2 ={
  "W", 
  "NW", 
  "N", 
  "NE", 
  "E", 
  "SE", 
  "S", 
  "SW", 
  "W"
};

// IF 4 DIRECTIONS ARE NEEDED
String[] dirName3 ={
  "W", 
  "N", 
  "E", 
  "S", 
  "W"
};

PVector origin;
void setup() {
  stroke(255);
  fullScreen();
  fill(255);
  origin = new PVector(width/2, height/2);
  textSize(30);
}

void draw() {
  background(51);
  fill(255);
  text(DIRS + " DIRECTIONS", 100, 60);
  int dirId = pointToward(mouseX, mouseY);
  text(dirName[dirId], 100, 100); // 16 directions
}

float r = 300; // FIELD RADIUS
int pointToward(float x, float y) {
  PVector point = new PVector(x, y);
  PVector dir = PVector.sub(point, origin);
  dir.normalize();
  float theta = dir.heading();
  // Get the nearest direction
  int angleMultiplier = round((DIRS) * theta/TWO_PI);
  theta = angleMultiplier * TWO_PI/(DIRS);

  // Get direction ID
  int dirId = angleMultiplier + (DIRS)/2;

  //drawing
  float triggerR = 3*r/DIRS; //TRIGGER RADIUS
  noFill();
  for (int i = -DIRS/2; i < DIRS/2; i++) {
    float cx = cos(i * TWO_PI/DIRS) * r + origin.x;
    float cy = sin(i * TWO_PI/DIRS) * r + origin.y;
    line(origin.x, origin.y, cx, cy);
    ellipse(cx, cy, triggerR*2, triggerR*2);
  }

  // check if it triggers the checking radius
  float tx = cos(theta) * r + origin.x;
  float ty = sin(theta) * r + origin.y;
  float d = dist(tx, ty, point.x, point.y);
  if (d < triggerR) {
    fill(255);
    ellipse(tx, ty, triggerR*2, triggerR*2);
    fill(0, 255, 100);
    text("TRIGGERED", 100, 150);
    //-----------------------------------DO SOMETHING HERE-----------------------------
    //text(dirName2[dirId], 100, 100); // 8 directions
    //text(dirName3[dirId], 100, 100); // 4 directions
  }
  return dirId;
}

```

---

<div class="post-metadata">

### Author: ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)
#### Post date: [July 24, 2019, 7:20am UTC](https://discourse.processing.org/t/check-direction-of-the-mouse-compared-to-a-point-in-space/12883/20 "2019-07-24T07:20:52Z")

</div>

Thanks for all the versions, i use the first 8 direction option, its perfect.

Thank you all
