# Line-Line intersection for y=mx+c

**URL:** <https://discourse.processing.org/t/line-line-intersection-for-y-mx-c/26484>\
**Category:** Coding Questions\
**Created:** [December 23, 2020, 10:27pm UTC](https://discourse.processing.org/t/line-line-intersection-for-y-mx-c/26484 "2020-12-23T22:27:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave307](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dave307](https://discourse.processing.org/u/dave307)\
**Post date:** [December 23, 2020, 10:27pm UTC](https://discourse.processing.org/t/line-line-intersection-for-y-mx-c/26484/1 "2020-12-23T22:27:35Z")

</div>

I was wondering how to make a function that you give it two line equations in the form y=mx+c

you pass it the gradient and y-intercept of both.

then it returns a vector which is the point in which the two given lines would intersect

---

<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:** [December 23, 2020, 10:30pm UTC](https://discourse.processing.org/t/line-line-intersection-for-y-mx-c/26484/2 "2020-12-23T22:30:43Z")

</div>

> [@Trying to understand the "Substrate" algorithm](https://discourse.processing.org/t/trying-to-understand-the-substrate-algorithm/3031/10):
>
> Hi @solub. Here is an example of line-line collision detection – it returns true-false, but you can also return a PVector with the intersection point, which is very useful for truncating a growing line at exactly the right place after it crosses. /\*\* \* Line-Line collision detection \* http://www.jeffreythompson.org/collision-detection/line-line.php \*/ boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { // calculate the distance to intersection…

---

<div class="post-metadata">

**Author:** ![markcosmic](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@markcosmic](https://discourse.processing.org/u/markcosmic)\
**Post date:** [December 24, 2020, 10:12pm UTC](https://discourse.processing.org/t/line-line-intersection-for-y-mx-c/26484/3 "2020-12-24T22:12:05Z")

</div>

Check out Daniel Shiffman’s “Ray Tracing” on youtube. He explains it quite well

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 25, 2020, 10:46am UTC](https://discourse.processing.org/t/line-line-intersection-for-y-mx-c/26484/4 "2020-12-25T10:46:43Z")

</div>

```auto
class Line {

  //PVector a,b;
  float x1, x2, y1, y2;
  PVector m;
  cell a, b;
  
  Line(float a, Float b,float c, Float d) {
    x1 = a;
    y1 = b;

    x2 = c;
    y2 = d;
    
  };
  Line(int a, int b,int c, int d) {
    x1 = a;
    y1 = b;

    x2 = c;
    y2 = d;
    
  };
  
  Line(PVector cell1, PVector cell2) {
    x1 = cell1.x;
    y1 = cell1.y;

    x2 = cell2.x;
    y2 = cell2.y;
    
  };
  
  Line(PVector cell1, PVector cell2,PVector c) {
    
    x1 = cell1.x;
    y1 = cell1.y;

    x2 = cell2.x;
    y2 = cell2.y;
    m = new PVector(c.x,c.y);
  };
  Line(cell cell1, cell cell2) {
    x1 = cell1.x;
    y1 = cell1.y;

    x2 = cell2.x;
    y2 = cell2.y;
  };

  void checkMouse() {
  };
  
  void draw(){
    stroke(0);
    strokeWeight(2);
    line(x1,y1,x2,y2);
  };
  
  void drawM(){
    stroke(0);
    strokeWeight(20);
    point(m.x,m.y);
  };
};

PVector checkIntersect(Line a, Line b) {

  float a1 = a.y2 - a.y1;
  float b1 = a.x1 - a.x2;
  float c1 = a1 * a.x1 + b1 * a.y1;
  float a2 = b.y2 - b.y1;
  float b2 = b.x1 - b.x2;
  float c2 = a2 * b.x1 + b2 * b.y1;
  float denom = a1 * b2 - a2 * b1;
  //stroke(0,0,255);
  //strokeWeight(2);
  //line(a.x1,a.y1,a.x2,a.y2);
  //stroke(0,255,255);
  //strokeWeight(2);
  //line(b.x1,b.y1,b.x2,b.y2);
  if ((a.x1==b.x1||a.x2==b.x2)&&(a.y1==b.y1||a.y2==b.y2)) {
    
    return null;
  } else {

    Float X = (b2 *c1 - b1 * c2) / denom;
    Float Y = (a1 *c2 - a2 * c1) / denom;
    
    PVector p = new PVector(X, Y);   
    boolean Linea = ((p.x<a.x1&&p.x>a.x2)||(p.x>a.x1&&p.x<a.x2))&&((p.y<a.y1&&p.y>a.y2)||(p.y>a.y1&&p.y<a.y2));
    boolean Lineb = ((p.x<b.x1&&p.x>b.x2)||(p.x>b.x1&&p.x<b.x2))&&((p.y<b.y1&&p.y>b.y2)||(p.y>b.y1&&p.y<b.y2));
    float n = 0.001;
    if (Linea&&Lineb) {
      //strokeWeight(10);
      //stroke(255,255,0);
      //point(p.x,p.y);
      //strokeWeight(1);
      return p;
    } else {
      return null;
    }
  }
};

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 25, 2020, 2:51pm UTC](https://discourse.processing.org/t/line-line-intersection-for-y-mx-c/26484/5 "2020-12-25T14:51:48Z")

</div>

Hello,

> [@dave307](#):
>
> I was wondering how to make a function that you give it two line equations in the form y=mx+c

“Wonder is the beginning of wisdom.” - Socrates

You can adapt this easily into a function:

> **[Line–line intersection | Given two line equations](https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_line_equations)**
>
> The x and y coordinates of the point of intersection of two non-vertical lines can easily be found using the following substitutions and rearrangements.
> Suppose that two lines have the equations y = ax + c and y = bx + d where a and b are the slopes (gradients) of the lines and where c and d are the y-intercepts of the lines. At the point where the two lines intersect (if they do), both y coordinates will be the same, hence the following equality:

A simple example of returning a PVector:

```auto
PVector v1;

void setup() 
  {
  size(500, 500);
  background(0);
  }

void draw()
  { 
  v1 = dot(mouseX, mouseY);
  println(v1);
  
  strokeWeight(2);
  stroke(255, 255, 0);
  point(v1.x, v1.y);  
  }

PVector dot(float x, float y) 
  {
  PVector v = new PVector(x, y);
  return v;
  }

```

`:)`
