student_js29,
<script>
sgts = [
[{
x: 1,
y: 2
}, {
x: -3,
y: 1
}],
[{
x: -3,
y: 5
}, {
x: 1,
y: 4
}],
[{
x: 4,
y: 2
}, {
x: -1,
y: 2
}],
[{
x: -2,
y: 5
}, {
x: 4,
y: 1
}]
];
for (let i = 0; i < sgts.length - 1; i++) {
let a = arrValue(sgts[i]);
for (let j = i + 1; j < sgts.length; j++) {
let b = arrValue(sgts[j]);
let point = Intersection(...a, ...b);
if (point) document.write(`отрезок ${i} пересекается с отрезком ${j} в точке x : ${point.x.toFixed(1)}, y : ${point.y.toFixed(1)}<br>`)
}
}
function arrValue(arr) {
return [arr[0].x, arr[0].y, arr[1].x, arr[1].y]
}
function Intersection(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
let v1 = (bx2 - bx1) * (ay1 - by1) - (by2 - by1) * (ax1 - bx1),
v2 = (bx2 - bx1) * (ay2 - by1) - (by2 - by1) * (ax2 - bx1),
v3 = (ax2 - ax1) * (by1 - ay1) - (ay2 - ay1) * (bx1 - ax1),
v4 = (ax2 - ax1) * (by2 - ay1) - (ay2 - ay1) * (bx2 - ax1),
x = ((ax1 * ay2 - ax2 * ay1) * (bx2 - bx1) - (bx1 * by2 - bx2 * by1) * (ax2 - ax1)) / ((ay1 - ay2) * (bx2 - bx1) - (by1 - by2) * (ax2 - ax1)),
y = ((by1 - by2) * x - (bx1 * by2 - bx2 * by1)) / (bx2 - bx1);
return (v1 * v2 < 0) && (v3 * v4 < 0) && {
x: -x,
y: y
};
}
</script>