xxxxxxxxxx
32
let x1 = 50;
let y1 = 50;
let x2 = 350;
let y2 = 350;
let x3 = 300;
let y3 = 101;
let x4 = 350;
let y4 = 350;
let t, u, px, py;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(0);
let x4 = mouseX;
let y4 = mouseY;
t = ((x1-x3)*(y3-y4)-(y1-y3)*(x3-x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
u = -((x1-x2)*(y1-y3)-(y1-y2)*(x1-x3))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
px = x1+t*(x2-x1);
py = y1+t*(y2-y1);
stroke(255);
strokeWeight(3);
line(x1, y1, x2, y2);
line(x3, y3, x4, y4);
stroke(255, 0, 0);
strokeWeight(10);
if ((0 <= t && t <= 1) && (0 <= u && u <= 1)) {
point(px, py);
}
}