//// Based on the Starter Code for "Embedded Iteration + Randomness" var boolDoRefresh; var lines = []; var lineNumber = 12; //<<-----change this number to add/remove lines! function setup() { createCanvas(720, 480); background(20,20,50) noStroke(); boolDoRefresh = true; } // Math for the intersection of two lines from Paul Bourke http://paulbourke.net/geometry/pointlineplane/ //takes two lines and returns the x,y value for the inersection of those lines function intersec(a,b){ var x1 = a[0]; var y1 = a[1]; var x2 = a[2]; var y2 = a[3]; var x3 = b[0]; var y3 = b[1]; var x4 = b[2]; var y4 = b[3]; var ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4-y3) * (x2-x1) - (x4-x3) * (y2-y1)); var ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4-y3) * (x2-x1) - (x4-x3) * (y2-y1)); var x = (x1 + ua * (x2 - x1)); var y = (y1 + ua * (y2 - y1)); if (ua < 0 || ua > 1 || ub < 0 || ub > 1) { return [-20,-20]; } return[x,y]; } //moves the lines according to each points velocity. function updateLines(lines){ for(var i = 0; i < lines.length; i++){ var l = lines[i]; l[0] += l[4]; l[1] += l[5]; l[2] += l[6]; l[3] += l[7]; } } function draw() { updateLines(lines); //Draws all of the lines: background(20,20,50) stroke(255, 50); fill(255); for(var i = 0; i < lines.length; i++){ line(lines[i][0],lines[i][1],lines[i][2],lines[i][3]); } //Draws a red dot at all of the intersections: for(var i = 0; i < lines.length; i++){ for(var j = 0; j < lines.length; j++){ if(i != j){ la = lines[i]; lb = lines[j]; noStroke() fill(255,0,0); ellipse(intersec(la,lb)[0],intersec(la,lb)[1],3); } } } if (boolDoRefresh) { boolDoRefresh = false; //Creates new lines: lines = []; for(var i = 0; i < lineNumber; i++){ lines.push([random(width),random(height),random(width),random(height),random(-1,1), random(-1,1),random(-1,1),random(-1,1)]) } } } function mousePressed() { boolDoRefresh = true; } |
Category: 01-Intersections
airsun-Intersections
//Clair(sijing) Sun //sijings@andrew.cmu.edu //Assignment-5 var lineP=[]; var boolDoRefresh; var numberofL=10; function setup() { createCanvas(480, 480); boolDoRefresh = false; for (var i=0; i<numberofL; i++){ var x1 = random(0,width); var x2 = random(0,width); var y1 = random(0,height); var y2 = random(9,height); lineP[i] = [x1,y1,x2,y2]; } } function draw() { background(200); //regenerate if mousePressed if (boolDoRefresh) { for (var i=0; i<numberofL; i++){ var x1 = random(0,width); var x2 = random(0,width); var y1 = random(0,height); var y2 = random(9,height); lineP[i] = [x1,y1,x2,y2]; } boolDoRefresh=false } //for drawing the lines for (var i = 0; i < numberofL; i += 1){ line(lineP[i][0],lineP[i][1],lineP[i][2],lineP[i][3]); } //for drawing the intersections, spliting to two lines each time for (var j = 0; j < numberofL; j += 1){ for (var i = 0; i < numberofL; i += 1){ var x1 = lineP[j][0]; var y1 = lineP[j][1]; var x2 = lineP[j][2]; var y2 = lineP[j][3]; var x3 = lineP[i][0]; var y3 = lineP[i][1]; var x4 = lineP[i][2]; var y4 = lineP[i][3]; intersect(x1, y1, x2, y2, x3, y3, x4, y4); } } } // Modified from line intercept math by Paul Bourke http://paulbourke.net/geometry/pointlineplane/ // Determine the intersection point of two line segments // Modified from http://paulbourke.net/geometry/pointlineplane/javascript.txt function intersect(x1, y1, x2, y2, x3, y3, x4, y4) { if ((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) { return false } var denom = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)) if (denom === 0) { return false } var ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom var ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom // is the intersection along the segments if (ua < 0 || ua > 1 || ub < 0 || ub > 1) { return false } fill(0); ellipse (x1 + ua * (x2 - x1),y1 + ua * (y2 - y1),15); } function mousePressed() { boolDoRefresh = true; }
chaine-Intersections
var boolDoRefresh; var size = 40; var lineLength = 300; var numLines = 12; var lineList = []; var x = []; //var circleCoords = []; //var circleList = []; //var foo = 0; function setup(){ createCanvas(728, 480); background(255,255,255); boolDoRefresh = true; for (var i = 0; i < numLines; i ++){ append(x, int(random(150, width - 150))); //starting x append(x, int(random(150, height - 150))); //starting y append(x, int(random(0, 361))); //angle of line append(lineList, x); x = []; } } function draw(){ if (boolDoRefresh == true){ for (var j = 0; j < lineList.length; j++){ //drawing where the circles should be var a1 = lineList[j][0]; var b1 = lineList[j][1]; var angle2 = lineList[j][2]; var a2 = a1+lineLength*cos(angle2); var b2 = b1+lineLength*sin(angle2); for (var k = 0; k <lineList.length; k++){ var a3 = lineList[k][0]; var b3 = lineList[k][1]; var angle3 = lineList[k][2]; var a4 = a3+lineLength*cos(angle3); var b4 = b3+lineLength*sin(angle3); //This long formula was taken from http://www-cs.ccny.cuny.edu/~wolberg/capstone/intersection/Intersection%20point%20of%20two%20lines.html //Paul Bourke's paper if (((b4-b3)*(a2-a1)-(a4-a3)*(b2-b1))!=0){ var ta = ((a4-a3)*(b1-b3)-(b4-b3)*(a1-a3))/((b4-b3)*(a2-a1)-(a4-a3)*(b2-b1)); var tb = ((a2-a1)*(b1-b3)-(b2-b1)*(a1-a3))/((b4-b3)*(a2-a1)-(a4-a3)*(b2-b1)); } else { ta = -1; tb = -1; } if (ta >= 0 && ta <= 1 && tb >= 0 && tb <= 1){ fill(204,229, 255); strokeWeight(0); ellipse((a1+ta*(a2-a1)),(b1+ta*(b2-b1)), 20); } else{ continue; } } fill(255,255,255); strokeWeight(0.1); for (var i = 0; i < numLines; i++){ var x1 = lineList[i][0]; var y1 = lineList[i][1]; var angle = lineList[i][2]; //below formula from https://stackoverflow.com/questions/48525583/get-a-points-position-from-an-angle-and-the-length-of-the-line line(x1, y1, x1+lineLength*cos(angle), y1+lineLength*sin(angle)); //append(lineList[i], x1+lineLength*cos(angle)); //append(lineList[i], y1+lineLength*sin(angle)); } //print(a2,b2); //print(lineList); //print(ta, tb); } } boolDoRefresh = false; } function mousePressed(){ boolDoRefresh = true; x = []; lineList = []; setup(); } |
Spoon-Intersections
Intersections with 12 lines.
Intersections with 100 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | var boolDoRefresh; const NUM_LINES = 12; function setup() { createCanvas(720, 480); background('#FFFFFF'); strokeWeight(5); boolDoRefresh = true; } function draw() { if(boolDoRefresh) { background('#FFFFFF'); var lines = constructLines(); findIntersections(lines); boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true; } function constructLines() { const LINE_LENGTH = 250; var lines = []; for(var i = 0; i < NUM_LINES; i++) { var x1 = random(720); var y1 = random(480); var x2 = random(100) > 50 ? x1 - random(LINE_LENGTH) : x1 + random(LINE_LENGTH); var y2 = ((random(100) > 50 ? 1 : -1) * sqrt(sq(LINE_LENGTH) - sq(x1 - x2))) + y1; line(x1, y1, x2, y2); lines[i] = [x1, y1, x2, y2]; } return lines; } function findIntersections(lines) { //uses calculations found at http://paulbourke.net/geometry/pointlineplane/ //draws circles around any points where lines intersect for(var i = 0; i < NUM_LINES; i++) { for(var j = i + 1; j < NUM_LINES; j++) { var x1 = lines[i][0]; var x2 = lines[i][2]; var y1 = lines[i][1]; var y2 = lines[i][3]; var x3 = lines[j][0]; var x4 = lines[j][2]; var y3 = lines[j][1]; var y4 = lines[j][3]; var u1 = (((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3))) / (((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1))); var u2 = (((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3))) / (((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1))); if(u1 > 0 && u1 < 1 && u2 > 0 && u2 < 1) { var x = x1 + (u1 * (x2 - x1)); var y = y1 + (u1 * (y2 - y1)); fill('#FFC600'); ellipse(x, y, 15); } } } } |
nerual-Intersections
var boolDoRefresh;
var lineArr;
var bkgrd = 255;
var canvasW = 600;
var canvasH = 600;
var numLines = 12;
function setup() {
createCanvas(canvasW, canvasH);
boolDoRefresh = true;
background(bkgrd);
lineArr = [];
//noFill();
}
function draw() {
var x1,x2,x3,x4,y1,y2,y3,y4;
if (boolDoRefresh) {
for (var i = 0; i < numLines; i++) {
createLine();
}
for (i = 0; i+1 < numLines; i++){
for (j = i+1; j < numLines; j++){
var a = lineArr[i];
var b = lineArr[j];
drawIntersect(a,b);
}
}
boolDoRefresh = false;
}
}
function createLine() {
x1 = random(canvasW);
y1 = random(canvasH);
x2 = random(canvasW);
y2 = random(canvasH);
stroke(0);
line(x1,y1,x2,y2);
lineArr.push([x1,y1,x2,y2]);
}
function drawIntersect(a, b){
x1 = a[0]; y1 = a[1]; x2 = a[2]; y2 = a[3];
x3 = b[0]; y3 = b[1]; x4 = b[2]; y4 = b[3];
var denom = (y4-y3)*(x2-x1) - (x4-x3)*(y2-y1);
if (denom == 0) {
return;
}
var ua = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3))/denom;
var ub = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3))/denom;
var ix = x1 + ua*(x2-x1);
var iy = y1 + ua*(y2-y1);
if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
return;
}
noStroke();
fill(random(256),random(256),random(256),150);
ellipse(ix, iy, 20, 20);
fill(255,255,255,200);
ellipse(ix,iy,5,5);
resetProps();
}
function resetProps(){
stroke(0);
}
function mousePressed() {
lineArr = [];
background(bkgrd);
boolDoRefresh = true;
}
sapeck-Intersections
/* Sapeck 9/6/2017 "sapeck-Intersections" 60-212 Carnegie Mellon University Copyright (C) 2018-present Sapeck This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ var NUM_LINES = 12 function setup() { createCanvas(720, 480) boolDoRefresh = true } function draw() { if (boolDoRefresh) { background(255); let lines = [] let intersects = [] for (let i=0;i<NUM_LINES;i++) { let thisLine = { x1: floor(random(0, width)), y1: floor(random(0, height)), y2: floor(random(0, height)) } thisLine.x2 = floor(random(thisLine.x1+1, width)) thisLine.m = (thisLine.y2 - thisLine.y1) / (thisLine.x2 - thisLine.x1) lines.push(thisLine) } for (let i=0;i<lines.length;i++) { for (let j=i+1;j<lines.length;j++) { if (i !== j) { let thisLine = lines[i] let testLine = lines[j] // My brute-force-test-x method that didn't entirely work // for (let thisX=thisLine.x1;thisX<=thisLine.x2;thisX+=0.1) { // let thisY = floor((thisLine.m * (thisX-thisLine.x1)) + thisLine.y1) // let testY = floor((testLine.m * (thisX-testLine.x1)) + testLine.y1) // if (thisY==testY) intersects.push({x: thisX, y: thisY}) // } // Paul Bourke's method (see function defintion below for full citation) let x1 = thisLine.x1, x2 = thisLine.x2, x3 = testLine.x1, x4 = testLine.x2 let y1 = thisLine.y1, y2 = thisLine.y2, y3 = testLine.y1, y4 = testLine.y2 let intersection = intersect(x1, y1, x2, y2, x3, y3, x4, y4) if (intersection !== false) intersects.push({x: intersection.x, y: intersection.y}) } } } for (let i=0;i<lines.length;i++) { let thisLine = lines[i] stroke(color(0,0,255)) line(thisLine.x1, thisLine.y1, thisLine.x2, thisLine.y2) } for (let i=0;i<intersects.length;i++) { let intersection = intersects[i] noStroke(); fill(color(255,0,0,50)) ellipse(intersection.x,intersection.y,20,20) } boolDoRefresh = false } } function mousePressed() { boolDoRefresh = true } // line intercept math by Paul Bourke http://paulbourke.net/geometry/pointlineplane/ // Determine the intersection point of two line segments // Return FALSE if the lines don't intersect function intersect(x1, y1, x2, y2, x3, y3, x4, y4) { // Check if none of the lines are of length 0 if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) { return false } denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)) // Lines are parallel if (denominator === 0) { return false } let ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator let ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator // is the intersection along the segments if (ua < 0 || ua > 1 || ub < 0 || ub > 1) { return false } // Return a object with the x and y coordinates of the intersection let x = x1 + ua * (x2 - x1) let y = y1 + ua * (y2 - y1) return {x, y} } |
nannon-Intersections
var boolDoRefresh; var linelist=[] var numSlider function setup() { createCanvas(720,480); boolDoRefresh = true; } function rando(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive } // function findintersection(line1, line2) { // return math.intersect(line1[0], line1[1], line2[0], line2[1]) // } function intersect(x1, y1, x2, y2, x3, y3, x4, y4) { if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) { return false } denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)) if (denominator === 0) { return false } let ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator let ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator if (ua < 0 || ua > 1 || ub < 0 || ub > 1) { return false } let x = x1 + ua * (x2 - x1) let y = y1 + ua * (y2 - y1) return [x, y] } function draw() { if (boolDoRefresh) { var numLines= 15 background("#FFE6E6"); linelist=[] for (i=0;i<numLines;i++){ var startx= rando(0, width) var starty= rando(0, height) var endx = rando(0, width) var endy = rando(0, height) stroke("#FF0000"); strokeWeight(2); line(startx, starty, endx,endy) linelist.push([[startx,starty],[endx,endy]]) if (linelist.length >1) { for (j=0;j<i; j++) { var inter = intersect(linelist[i][0][0], linelist[i][0][1], linelist[i][1][0], linelist[i][1][1], linelist[j][0][0], linelist[j][0][1], linelist[j][1][0], linelist[j][1][1],) if (inter) { strokeWeight(2) fill('rgba(255,0,0, 0.25)') ellipse(inter[0], inter[1], 20,20) } } } } boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true; } //random function from: //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random //intersect function from: //http://paulbourke.net/geometry/pointlineplane/javascript.txt |
yuvian-Intersections
intersections
twelve intersections
seventy intersections
var lines = [];
var numOfLines = 12;
var intersectXArray =[];
var intersectYArray = [];
var yAxis = 1;
var c1, c2, c3;
function setup() {
createCanvas(720, 480);
background(0);
c1 = color(0);
c2 = color(16, 45, 117);
c3 = color(244, 89, 66);
// gradient background 1
setGradient(0, 0, windowWidth, windowHeight / 2, c1, c2, yAxis);
// gradient background 2
setGradient(0, windowHeight/2, windowWidth, windowHeight, c2, c3, yAxis);
for (var i = 0; i < numOfLines; i++) {
lines.push(new Line());
}
}
function draw() {
for (var i = 0; i < lines.length; i++) {
lines[i].display();
}
for (var i = 0; i < lines.length; i++) {
for (var j = 0; j < lines.length; j++) {
intersectXArray.push(intersect(lines[i].x1, lines[i].y1, lines[i].x2, lines[i].y2, lines[j].x1, lines[j].y1, lines[j].x2, lines[j].y2).x);
intersectYArray.push(intersect(lines[i].x1, lines[i].y1, lines[i].x2, lines[i].y2, lines[j].x1, lines[j].y1, lines[j].x2, lines[j].y2).y);
}
}
// display twinkling stars as intersection points
for (var i = 0; i < intersectXArray.length; i++) {
star = new Star(intersectXArray[i], intersectYArray[i]);
star.display();
star.twinkle();
}
}
function mousePressed() {
lines = [];
intersectXArray = [];
intersectYArray = [];
setup();
}
function Line() {
this.x1 = random(0, width);
this.y1 = random(0, height);
this.x2 = random(0, width);
this.y2 = random(0, height);
this.display = function() {
stroke(249, 252, 232, 20);
strokeWeight(1);
line(this.x1, this.y1, this.x2, this.y2);
}
}
function Star(x,y) {
this.x = x;
this.y = y
this.r = random(8);
this.display = function() {
stroke(22, 71, 119);
fill(255);
this.rc = constrain(this.r, 0, 9);
ellipse(this.x, this.y, this.rc, this.rc);
};
this.twinkle = function() {
if (this.r < 3) {
this.r += random(-.5,1.5);
} else if (this.r >= 3 && this.r < 6) {
this.r += random(-1,1);
} else if (this.r >=6 && this.r <=9) {
this.r += random(-1.5,0.5);
}
}
}
// creates gradients
function setGradient(x, y, w, h, c1, c2, axis) {
noFill();
if (axis == yAxis) { // Top to bottom gradient
for (var i = y; i <= y+h; i++) {
var inter = map(i, y, y+h, 0, 1);
var c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x+w, i);
}
}
}
//from Paul Bourke http://paulbourke.net/geometry/pointlineplane/javascript.txt
function intersect(x1, y1, x2, y2, x3, y3, x4, y4) {
// Check if none of the lines are of length 0
if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) {
return false
}
denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))
// Lines are parallel
if (denominator === 0) {
return false;
}
let ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator;
let ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator;
// is the intersection along the segments
if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
return false
}
// Return a object with the x and y coordinates of the intersection
let x = x1 + ua * (x2 - x1)
let y = y1 + ua * (y2 - y1)
return {x, y}
}
casher-intersections
var boolDoRefresh = true; var lines = []; function setup() { createCanvas(720, 480); } function draw() { if (boolDoRefresh) { // set design values background(201, 239, 255); strokeWeight(1.5); stroke(140, 81, 200); lines = []; // assign random values to points of each line for (var k = 0; k < 12; k++) { x1 = random(720); y1 = random(480); x2 = random(720); y2 = random(480); var newLine = [x1, y1, x2, y2]; lines.push(newLine); } // double loop takes points from line[] to make a line // and forms an ellipse at the points from findIntersection() for (var i = 0; i < 12; i++) { for (var j = 0; j < 12; j++) { line(lines[i][0], lines[i][1], lines[i][2], lines[i][3]) var steve = findIntersection(lines[i][0], lines[i][1], lines[i][2], lines[i][3], lines[j][0], lines[j][1], lines[j][2], lines[j][3]) ellipse(steve[0],steve[1],20,20) } } } boolDoRefresh = false; } //adapted from Paul Bourkes/Leo Bottaro http://paulbourke.net/geometry/pointlineplane/javascript.txt function findIntersection(x1, y1, x2, y2, x3, y3, x4, y4) { // Check if none of the lines are of length 0 if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) { return false } denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)) // Lines are parallel if (denominator === 0) { return false } let ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator let ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator // is the intersection along the segments if (ua < 0 || ua > 1 || ub < 0 || ub > 1) { return false } // Return a object with the x and y coordinates of the intersection let x = x1 + ua * (x2 - x1) let y = y1 + ua * (y2 - y1) return[x, y] } function mousePressed() { boolDoRefresh = true; } |
breep-Intersections
// Generating random lines and highlighting intersections var boolDoRefresh; var slider; var totalLines; function setup() { createCanvas(720, 480); background(255, 117, 102); boolDoRefresh = true; slider = createSlider(1, 100, 12) slider.position(10, 10) } function draw() { if (boolDoRefresh) { lines = []; background(255, 179, 102); for ( i = 0; i < slider.value(); i ++){ var x1 = random(width); var x2 = random(width); var y1 = random(height); var y2 = random(height); lines[i] = [x1, y1, x2, y2] } // Line intersections for ( i = 0; i < slider.value(); i++){ for ( j = 0; j < slider.value(); j++){ var intersection = findingIntersection(lines[i][0], lines[i][1], lines[i][2], lines[i][3], lines[j][0], lines[j][1], lines[j][2], lines[j][3]); if ( intersection != false) { stroke(0); fill(173, 216, 230); ellipse(intersection.x, intersection.y, 20, 20); } } } // Line drawing for (i = 0; i < slider.value(); i ++){ strokeWeight(1); line( lines[i][0], lines[i][1], lines[i][2], lines[i][3]) } fill(0); text(slider.value(), 150, 27) boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true } // Function implementation inspired by Paul Bourke's algorithm // http://paulbourke.net/geometry/pointlineplane/function // And implemented by Leo Bottaro // http://paulbourke.net/geometry/pointlineplane/javascript.txt function findingIntersection(x1, y1, x2, y2, x3, y3, x4, y4) { // Check if none of the lines are of length 0 if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) { return false } denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)) // Lines are parallel if (denominator === 0) { return false } let ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator let ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator // is the intersection along the segments if (ua < 0 || ua > 1 || ub < 0 || ub > 1) { return false } // Return a object with the x and y coordinates of the intersection let x = x1 + ua * (x2 - x1) let y = y1 + ua * (y2 - y1) return {x, y} } |