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 };
}