How to detect if a line intersects any of the rectangles

edited January 2014 in How To...
import processing.core.*;

public class MyProcessingSketch extends PApplet {
  //    An array of stripes
  Stripe[] stripes = new Stripe[50];

  float[][] distances;
  float maxDistance;
  int spacer;

  public float a;

  public void setup() {
    size(640, 360);
    stroke(255);
    a = 0;
  }

  public void draw() {
    background(51);
    rect(200, 210, 50, 50);
    rect(120, 145, 20, 25);
    rect(330, 301, 50, 10);
    rect(140, 100, 10, 60);
    rect(10, 10, 100, 80);
    rect(10, 150, 100, 80);
    line(0, a, width, a);  
    a = (float) (a + 1);
    if (a > height) { 
      return; 
    }
  }
}************************

Hello all!

Above, there is a code. I have drawn multiple rectangles on the screen, and there is a line sweeping the screen from top to bottom. What I'm trying to do is, change the color of the rectangle when the line touches it.

When the line touches ANY of the rectangles, is there a way to change its color WITHOUT comparing the coordinates in a loop, or sorting the coordinates etc.?

Just using the pixel values, can I detect if two objects intersect?

Is there a function such that if (background of the line is not fully black) or something else?

It would be the same thing, when a part of the line is encountered with an obstacle, that part stays there, but without knowing the coordinates.

I think that requires a little bit of image processing, what do you think?

Answers

  • Look at the get() function.

  • Answer ✓

    When the line touches ANY of the rectangles, is there a way to change its color WITHOUT comparing the coordinates in a loop, or sorting the coordinates etc.?

    There is no easy way to do this just using the pixel array.

    Just using the pixel values, can I detect if two objects intersect?

    No really.

    Is there a function such that if (background of the line is not fully black) or something else?

    By background do you mean the line color. Yes but you would have to understand how color values are calculated from Red/Green/Blue/Alpha values

    It would be the same thing, when a part of the line is encountered with an obstacle, that part stays there, but without knowing the coordinates.

    Not sure what this means but can't see how you could do this just using the pixel array.

    I think that requires a little bit of image processing, what do you think?

    To do what you what just using the pixel array would be very difficult if not impossible. Even if you managed it the result would very, very, very slow because the amount of computation would be huge.

    There is no realistic way to do what you want without recording line/rectangle/shape coordinates.

  • Answer ✓

    Well, given the line is horizontal and across the whole sketch area, it should be simple to scan the corresponding line of pixels to see if there are white pixels.

    Of course, a simple algorithmic approach with rectangle coordinates would be simpler / better...

  • Thank you very much for your time.

Sign In or Register to comment.