# Correct practice line intersect

**URL:** <https://discourse.processing.org/t/correct-practice-line-intersect/13017>\
**Category:** Coding Questions\
**Created:** [July 27, 2019, 5:07pm UTC](https://discourse.processing.org/t/correct-practice-line-intersect/13017 "2019-07-27T17:07:33Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [July 27, 2019, 5:07pm UTC](https://discourse.processing.org/t/correct-practice-line-intersect/13017/1 "2019-07-27T17:07:33Z")

</div>

I’m trying to replicate a line line intersection algorithm whilst putting my own spin on it. Heres the code in Javascript from (Coding Math - youtube channel) ----- Amazing channel do check it out if you have time!!!

 ![07%20PM%20001](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2757e12b4c25fd79ed4b391072e2b0e30a387dc7.jpeg)

Now being new to processing i’m not sure if this is the best way of implementing this feature. I have opted for a Hashmap option which stores the x and y like this;

```auto
HashMap<String, Float> check_intersect(Line a, Line b){
    
    //for(int i=0;i<this.sorted_lines.size();i++){
      
    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 * b1 - b2 * b1;
    //float x = (b2 *c1 - b1 * c2) / denom;
    //float y = (a1 *c2 - a2 * c1) / denom;
    HashMap<String, Float> p = new HashMap<String, Float>();
    Float x = (b2 *c1 - b1 * c2) / denom;
    Float y = (a1 *c2 - a2 * c1) / denom;
    //p.put("x", x);
    
    p.put("x",x);
    p.put("y",y);
    
    
    return p;
  
  }

```

Just wanted to check if this way has any drawbacks that people know of (again very new to processing);

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 27, 2019, 5:33pm UTC](https://discourse.processing.org/t/correct-practice-line-intersect/13017/2 "2019-07-27T17:33:05Z")

</div>

You’re miles better off using a PVector to store your _x_ & _y_ rather than some clunky [HashMap](https://processing.org/reference/HashMap.html) container:  
[Processing.org/reference/PVector.html](http://Processing.org/reference/PVector.html)  
`final PVector p0 = new PVector(100, 10);`  
`return new PVector(b2*c1 - b1*c2, a1*c2 - a2*c1);`

---

<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:** [July 27, 2019, 7:07pm UTC](https://discourse.processing.org/t/correct-practice-line-intersect/13017/3 "2019-07-27T19:07:16Z")

</div>

Of course. I’d made a point about not using Pvectors until I understood them, bu clearly they are very useful. Thanks

---

<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:** [July 27, 2019, 7:10pm UTC](https://discourse.processing.org/t/correct-practice-line-intersect/13017/4 "2019-07-27T19:10:44Z")

</div>

[https://natureofcode.com/book/chapter-1-vectors/](https://natureofcode.com/book/chapter-1-vectors/)

🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 27, 2019, 9:34pm UTC](https://discourse.processing.org/t/correct-practice-line-intersect/13017/5 "2019-07-27T21:34:31Z")

</div>

> [@paulgoux](#):
>
> I’d made a point about not using PVectors until I understood them, but clearly they are very useful.

PVector is just a common way to store _x_, _y_, _z_ coordinates as 1 object in Processing.

But given you aren’t using the coordinate _z_, you could easily go w/ a Point2D.Float instead:  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/geom/Point2D.Float.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/geom/Point2D.Float.html)  
`import java.awt.geom.Point2D.Float;`

Or when the `int` primitive datatype is enough, you can go w/ just a Point:  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Point.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Point.html)  
`import java.awt.Point;`

Alternatively, you can create your own custom `class` to store the coodinate pair _x_ & _y_:

```auto
class CoordPair {
  float x, y;

  CoordPair() {
  }

  CoordPair(final float xx, final float yy) {
    x = xx;
    y = yy;
  }

  String toString() {
    return "[" + x + ", " + y + "]";
  }
}

```

Or even pick Java’s simplest container, the array:

```auto
final float[][] dots = {
  { 100, 100 }, { 400, 400 }, // line A coords
  { 500, 50 }, { 80, 500 } // line B coords
};

```

That’s exactly my pick in my conversion attempt below:

> **[Line Intersections - GoToLoop - OpenProcessing](https://openprocessing.org/sketch/740740)**
>
> https://YouTube.com/watch?v=4bIsntTiKfM

```java
/**
 * Line Intersections (v1.0)
 * Mod: GoToLoop (2019/Jul/27)
 *
 * https://www.YouTube.com/watch?v=4bIsntTiKfM
 * https://GitHub.com/bit101/CodingMath/blob/master/episode32/main.js
 *
 * https://Discourse.Processing.org/t/correct-practice-line-intersect/13017/5
 * https://www.OpenProcessing.org/sketch/740740
 */

static final color BG = 0350;
static final int DIAM = 20;

final float[][] dots = {
  { 100, 100 }, { 400, 400 }, // line A coords
  { 500, 50 }, { 80, 500 } // line B coords
};

void setup() {
  size(600, 600);
  noLoop();
  noFill();

  for (final float[] coord : dots) println(coord);
  print("\n");
}

void draw() {
  background(BG);

  line(dots[0][X], dots[0][Y], dots[1][X], dots[1][Y]); // line A
  line(dots[2][X], dots[2][Y], dots[3][X], dots[3][Y]); // line B

  final float[] inter = lineIntersect(dots[0], dots[1], dots[2], dots[3]);
  println(inter);

  ellipse(inter[X], inter[Y], DIAM, DIAM); // intersection mark
}

static final float[] lineIntersect(
  final float[] p0, final float[] p1, // line A coords
  final float[] p2, final float[] p3) // line B coords
{
  final float
    a1 = p1[Y] - p0[Y], 
    b1 = p0[X] - p1[X], 
    c1 = a1*p0[X] + b1*p0[Y], 

    a2 = p3[Y] - p2[Y], 
    b2 = p2[X] - p3[X], 
    c2 = a2*p2[X] + b2*p2[Y], 

    d = a1*b2 - a2*b1;

  return new float[] { (b2*c1 - b1*c2) / d, (a1*c2 - a2*c1) / d };
}

```
