Correct practice line intersect

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!!!

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;

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.

1 Like

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

2 Likes

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

https://natureofcode.com/book/chapter-1-vectors/

:slight_smile:

1 Like

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
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
import java.awt.Point;

Alternatively, you can create your own custom class to store the coodinate pair x & y:

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:

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 (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 };
}
1 Like