# Check if object is inbetween 2 points

**URL:** <https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288>\
**Category:** Beginners\
**Created:** [September 16, 2021, 2:02pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288 "2021-09-16T14:02:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [September 16, 2021, 2:02pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/1 "2021-09-16T14:02:38Z")

</div>

how to check if an object is anywhere inbetween the two x values, width-(width/4) and eight-(height/4)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 16, 2021, 2:12pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/2 "2021-09-16T14:12:24Z")

</div>

> how to see if a character is inbetween two points (width-(width/4), height-(height/4))

Do you mean like a text character? Like, you wrote

```auto
text("This is a test", width/2, height/2)

```

and you want to find out if the letter “a” is between two points?

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [September 16, 2021, 3:29pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/3 "2021-09-16T15:29:05Z")

</div>

no, I meant an object

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 16, 2021, 3:53pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/4 "2021-09-16T15:53:52Z")

</div>

An object can be anything and “in between 2 points” can also mean a lot of things. Could you be more precise?

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 16, 2021, 3:58pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/5 "2021-09-16T15:58:28Z")

</div>

An object – like, a picture of a cat (and the outline of the cat, not the rectangle of the picture)? Or like a PShape – e.g. a polygon? Or a rendering, like `circle`?

There are different algorithms to determine between-ness depending on how you define what you are checking for. The most common things to check are:

1. geometry (a circle, a bezier curve)
2. a polygon defined as a collection of vertex points
3. a pixel area defined as a collection of colors which are blocking (e.g. walls) or non-blocking (floor, grass, etc.)

Very different algorithms. So: what are you trying to do?

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [September 16, 2021, 4:47pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/6 "2021-09-16T16:47:19Z")

</div>

if an image is anywhere inbetween the two points, (width-width/4 , height-height/4) and (width, height-height/4)

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [September 16, 2021, 4:50pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/7 "2021-09-16T16:50:34Z")

</div>

if an image is anywhere inbetween the two x values, width-(width/4) and eight-(height/4)

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 16, 2021, 4:51pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/8 "2021-09-16T16:51:14Z")

</div>

Again, what are you calling an object? do you have any exemples? A screenshot, a figure, something to share so we ca get the idea of what you want exactly?

Also you are speaking of 2 x values but you are using the height to define one. I’m not saying it is wrong as nothing is preventing you from doing it, but height mainly refers to the y coordinate and I just want to be sure you are indeed speaking of 2 x values.

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [September 16, 2021, 4:52pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/9 "2021-09-16T16:52:02Z")

</div>

image(midas[i], midasx, midasy, 50, 80);

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 16, 2021, 4:53pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/10 "2021-09-16T16:53:35Z")

</div>

If you just want to treat your image as a rectangle – you don’t care what is inside – then you can do it with rect-line collision detection:

> **[Collision Detection](http://www.jeffreythompson.org/collision-detection/line-rect.php)**
>
> An online book about collision detection using Processing.

If you _do_ care what is inside in the image, then you need to use pixel detection or geometry or both.

---

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [September 17, 2021, 8:04pm UTC](https://discourse.processing.org/t/check-if-object-is-inbetween-2-points/32288/11 "2021-09-17T20:04:00Z")

</div>

Hi @MoonAlien822 ,

See the sketch below. Hit any key on the keyboard to change the x coordinate of the image. The `if(imgX > firstX && imgX + img.width < secondX)` is the bit that you were asking for I think. It checks both if the `imgX` is to the right of the first x coordinate, AND (&&) if it is to the left of the second X coordinate. Notice how you also need to add the width of the image to it, otherwise it would just check if the the left side of the image was inside.

```auto

float firstX, secondX;
float imgX;

PImage img;

void setup()
{
  size(600, 100);
  textAlign(CENTER, CENTER);
  
  firstX = width/4; // 25%
  secondX = width - width/4; // 75%
  
  img = createImage(100, 100, RGB); // use loadImage here instead
  imgX = random(width - img.width);
}

void draw()
{
  background(255);

  image(img, imgX, 0);
  
  if(imgX > firstX && imgX + img.width < secondX) {
    fill(0, 255, 0);
    text("Woo!", imgX, 0, img.width, img.height);
  } else {
    fill(255);
    text("Poor me :(", imgX, 0, img.width, img.height);
  }
    
  
  // draw first and second X for reference:
  
  stroke(255, 0, 0);
  line(firstX, 0, firstX, height);
  
  stroke(255, 0, 0);
  line(secondX, 0, secondX, height);
  
}

void keyReleased()
{
  imgX = random(width - img.width);
}

```
