# Two elements crossing each other

**URL:** https://discourse.processing.org/t/two-elements-crossing-each-other/18576
**Category:** Coding Questions
**Created:** [March 10, 2020, 9:01pm UTC](https://discourse.processing.org/t/two-elements-crossing-each-other/18576 "2020-03-10T21:01:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)
#### Post date: [March 10, 2020, 9:01pm UTC](https://discourse.processing.org/t/two-elements-crossing-each-other/18576/1 "2020-03-10T21:01:38Z")

</div>

Hey guys,

so I’ve faced a very simple issue but I cant get over it. I got a rect and a line crossing each other. I need the coordinate where they are crossing each other. How can I get this point?

See me example below:

 ![dsfdsf](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/dc293577ebb15dbca04a06bcac83c9f57f9dd4ff.png)

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [March 10, 2020, 11:11pm UTC](https://discourse.processing.org/t/two-elements-crossing-each-other/18576/2 "2020-03-10T23:11:20Z")

</div>

A classic example of asking a question without showing us any code. This is how much code I had to write to even START solving the problem:

```auto
float rx, ry, rw, rh, x1, y1, x2, y2;

void setup(){
  size(440,440);
  stroke(255);
  noFill();
}

void draw(){
  background(0);
  
  rx = random(width/2);
  ry = random(height/2);
  rw = random(width/2);
  rh = random(height/2);
  
  x1 = random(rx, rx+rw);
  y1 = random(ry, ry+rh);
  
  x2 = random(width);
  while( rx <= x2 && x2 <= rx+rw){
    x2 = random(width);
  }
  
  y2 = random(height);
  while( ry <= y2 && y2 <= ry + rh ){
    y2 = random(height);
  }  
  
  rect(rx,ry,rw,rh);
  line(x1,y1,x2,y2);
  
  noLoop();
}

void keyPressed(){
  loop();
}

```

Anyway, the idea is that you know the rectangle is made up of four lines itself, so you just check each of those lines to see if it crosses the single line.

Line-Line intersection is a solved problem; you can easily search the internet for a solution.

---

<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: [March 13, 2020, 3:50am UTC](https://discourse.processing.org/t/two-elements-crossing-each-other/18576/3 "2020-03-13T03:50:47Z")

</div>

Here is a demonstration of solving line-line intersection in Processing:

- [http://jeffreythompson.org/collision-detection/line-line.php](http://jeffreythompson.org/collision-detection/line-line.php)

Note that a rectangle is made up of four lines, and that your line might cross any one of them – or more than one. It is even (in theory) possible for the line and one side of the rect to be on top of one another.
