# How to judge and prevent the intersection of lines

**URL:** <https://discourse.threejs.org/t/how-to-judge-and-prevent-the-intersection-of-lines/43493>\
**Category:** Questions\
**Tags:** geometry\
**Created:** [October 11, 2022, 12:42pm UTC](https://discourse.threejs.org/t/how-to-judge-and-prevent-the-intersection-of-lines/43493 "2022-10-11T12:42:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![n1ka](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@n1ka](https://discourse.threejs.org/u/n1ka)\
**Post date:** [October 11, 2022, 12:42pm UTC](https://discourse.threejs.org/t/how-to-judge-and-prevent-the-intersection-of-lines/43493/1 "2022-10-11T12:42:40Z")

</div>

Hello  
can you give me some help to judge and prevent the intersection of lines.

 ![企业微信截图_20221011203456](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/b/6/b6cb84f31b5f8c67fe47558204e1ee4d8871d596.png)

---

<div class="post-metadata">

**Author:** ![hofk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/hofk/32/44944_2.png) [@hofk](https://discourse.threejs.org/u/hofk)\
**Post date:** [October 11, 2022, 4:23pm UTC](https://discourse.threejs.org/t/how-to-judge-and-prevent-the-intersection-of-lines/43493/2 "2022-10-11T16:23:14Z")

</div>

What does your code look like?  
It seems you have wrong coordinates for the vertices.

Take a look at these simple examples from the [Collection of examples from discourse.threejs.org](https://discourse.threejs.org/t/collection-of-examples-from-discourse-threejs-org/4315) .

[BufferGeometryNonIndexed](https://hofk.de/main/discourse.threejs/2022/BufferGeometry/BufferGeometryNonIndexed.html)  
[BufferGeometryIndexed](https://hofk.de/main/discourse.threejs/2022/BufferGeometry/BufferGeometryIndexed.html)

[NumberingHelperExamples](https://hofk.de/main/discourse.threejs/2022/THREEn/NumberingHelperExamples.html)

---

<div class="post-metadata">

**Author:** ![n1ka](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@n1ka](https://discourse.threejs.org/u/n1ka)\
**Post date:** [October 12, 2022, 1:42am UTC](https://discourse.threejs.org/t/how-to-judge-and-prevent-the-intersection-of-lines/43493/3 "2022-10-12T01:42:44Z")

</div>

Yeah, my coordinates for the vertices is wrong([https://discourse.threejs.org/t/non-simple-self-intersecting-polygon-troubles/8951](https://discourse.threejs.org/t/non-simple-self-intersecting-polygon-troubles/8951)), but i did it on purpose, because my idea is when draw lines and happen intersection, how to prevent mouse click this wrong coordinate of vertices(picture 1). Can you give me some Help? Thanks！

This is my code:  
{  
Plane2Entity.prototype.setGeometry = function (vertices) {

```
this.entity.clear();
let points = vertices.map(obj => new THREE.Vector2(obj.x, obj.z));
let shape = new THREE.Shape(points);
this._geometry = new THREE.ExtrudeGeometry(shape, { 'depth': 0.1, bevelEnabled: false});
//console.log(this._geometry);
this._geometry.rotateX(Math.PI / 2);
// edge
this._edgeGeo = new THREE.EdgesGeometry(this._geometry);
this._edge = new THREE.LineSegments(this._edgeGeo, this._lineMaterial);
// mesh
this._mesh = new THREE.Mesh(this._geometry, this._meshMaterial);
// points
this._points = new THREE.Points(this._geometry, this._pointMaterial);
this.entity.add(this._edge);
this.entity.add(this._mesh);
this.entity.add(this._points);

```

}  
}

 ![企业微信截图_16655389381129](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/0/5/0594b9825a17dd7d0afeb7afed3c5648b22fa448.png)

---

<div class="post-metadata">

**Author:** ![n1ka](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@n1ka](https://discourse.threejs.org/u/n1ka)\
**Post date:** [October 12, 2022, 1:50am UTC](https://discourse.threejs.org/t/how-to-judge-and-prevent-the-intersection-of-lines/43493/4 "2022-10-12T01:50:17Z")

</div>

Yeah, my coodinate is wrong, but i did it on purpose, how can i prevent mouse click on this situasion happen (purpose make wrong coodinate), this picture effect is what i want, it can penvent line segment intersection.

My code shows that:

```
let points = vertices.map(obj => new THREE.Vector2(obj.x, obj.z));
let shape = new THREE.Shape(points);
this._geometry = new THREE.ExtrudeGeometry(shape, { 'depth': 0.1, bevelEnabled: false});
//console.log(this._geometry);
this._geometry.rotateX(Math.PI / 2);

// edge
this._edgeGeo = new THREE.EdgesGeometry(this._geometry);
this._edge = new THREE.LineSegments(this._edgeGeo, this._lineMaterial);
// mesh
this._mesh = new THREE.Mesh(this._geometry, this._meshMaterial);
// points
this._points = new THREE.Points(this._geometry, this._pointMaterial);
this.entity.add(this._edge);
this.entity.add(this._mesh);
this.entity.add(this._points);

```

 ![企业微信截图_16655389381129](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/0/5/0594b9825a17dd7d0afeb7afed3c5648b22fa448.png)

---

<div class="post-metadata">

**Author:** ![n1ka](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@n1ka](https://discourse.threejs.org/u/n1ka)\
**Post date:** [October 12, 2022, 2:04am UTC](https://discourse.threejs.org/t/how-to-judge-and-prevent-the-intersection-of-lines/43493/5 "2022-10-12T02:04:11Z")

</div>

I think my question can be translated into how to judge a line segment and a line segment intersection.

---

<div class="post-metadata">

**Author:** ![n1ka](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@n1ka](https://discourse.threejs.org/u/n1ka)\
**Post date:** [October 13, 2022, 6:01am UTC](https://discourse.threejs.org/t/how-to-judge-and-prevent-the-intersection-of-lines/43493/6 "2022-10-13T06:01:21Z")

</div>

My question is solved.

code:  
{  
export function LineIntersectLine(lineA, lineB) {

```
const [[a0x, a0y], [a1x, a1y]] = lineA
const [[b0x, b0y], [b1x, b1y]] = lineB;

// Test for shared points
if (a0x === b0x && a0y === b0y) return true;
if (a1x === b1x && a1y === b1y) return true;

const denom = ((b1y - b0y) * (a1x - a0x)) - ((b1x - b0x) * (a1y - a0y));

if (denom === 0) return false;

const deltaY = a0y - b0y;
const deltaX = a0x - b0x;
const numer0 = ((b1x - b0x) * deltaY) - ((b1y - b0y) * deltaX);
const numer1 = ((a1x - a0x) * deltaY) - ((a1y - a0y) * deltaX);
const quotA = numer0 / denom;
const quotB = numer1 / denom;

return quotA > 0 && quotA < 1 && quotB > 0 && quotB < 1;

```

}  
}
