Check if object is inbetween 2 points

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

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

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

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

no, I meant an object

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

2 Likes

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?

2 Likes

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

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

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.

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

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:

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

1 Like

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.


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);
}


1 Like