# How to find segments intersection?

**URL:** <https://discourse.threejs.org/t/how-to-find-segments-intersection/5965>\
**Category:** Questions\
**Tags:** raycaster, geometry\
**Created:** [February 1, 2019, 8:07am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965 "2019-02-01T08:07:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fancyrainbow](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@fancyrainbow](https://discourse.threejs.org/u/fancyrainbow)\
**Post date:** [February 1, 2019, 8:07am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/1 "2019-02-01T08:07:30Z")

</div>

Hi !

Is there any way to find an intersection between two segments ? Or rays ?  
I searched the Ray doc and I found that there is methods for finding an intersecting vector with triangles, boxes, and spheres, so I am surprised to find none with segments.

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [February 1, 2019, 8:11am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/2 "2019-02-01T08:11:15Z")

</div>

Hi!  
Could you explain it more detailed? What segments? Segments of what?

---

<div class="post-metadata">

**Author:** ![fancyrainbow](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@fancyrainbow](https://discourse.threejs.org/u/fancyrainbow)\
**Post date:** [February 1, 2019, 8:14am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/3 "2019-02-01T08:14:20Z")

</div>

As “segments” I mean arrays of two vector3.

the intersecting vetor3 between [[0,5,0], [0,-5,0] ] and [[5,0,0], [-5,0,0] ] would be [0,0,0].

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [February 1, 2019, 8:27am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/4 "2019-02-01T08:27:45Z")

</div>

AFAIK, there is no such functionality “out-of-the-box” in Three.js.

Are points of those two lines (segments) always co-planar? Do those lines (segments) always intersect?

---

<div class="post-metadata">

**Author:** ![fancyrainbow](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@fancyrainbow](https://discourse.threejs.org/u/fancyrainbow)\
**Post date:** [February 1, 2019, 8:41am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/5 "2019-02-01T08:41:56Z")

</div>

Thanks anyway,

But it is surprising, as Ray.intersectBox() can find an intersecting vector between a ray and a three-dimensional object, I’d expected that the functionality was available as well for an (a priori) simpler task (intersection between two rays). I will try to find a workaround and keep you posted.

For my use, the lines can be not-coplanar, and it should be possible to return null if there is no intersection.

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [February 1, 2019, 8:48am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/6 "2019-02-01T08:48:53Z")

</div>

Just out of curiousily, why do you want to use finding intesection between lines? What’s the task? 🙂

---

<div class="post-metadata">

**Author:** ![fancyrainbow](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@fancyrainbow](https://discourse.threejs.org/u/fancyrainbow)\
**Post date:** [February 1, 2019, 9:09am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/7 "2019-02-01T09:09:01Z")

</div>

I want to make a function to offset polylines (array of vector3) according to their bisectors.  
In order to code a parametric geometry. (so the user can change the shape of the polyline, and the offset will adapt).

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [February 1, 2019, 9:10am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/8 "2019-02-01T09:10:44Z")

</div>

Could you provide an explanatory picture?

---

<div class="post-metadata">

**Author:** ![fancyrainbow](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@fancyrainbow](https://discourse.threejs.org/u/fancyrainbow)\
**Post date:** [February 1, 2019, 9:15am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/9 "2019-02-01T09:15:51Z")

</div>

![2019-02-01_171343](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/threejs/original/2X/9/98af3d57e788b97ae682be519093bfc1b84cc601.jpeg)

In blue the base polyline, in black the polyline offset from the blue according to bisectors.

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [February 1, 2019, 9:18am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/10 "2019-02-01T09:18:45Z")

</div>

Maybe this topic will be useful for you:

> [@OffsetContour function](https://discourse.threejs.org/t/offsetcontour-function/3185):
>
> There’s a question on SO: And a forum topic: With some changes of the ProfiledContourGeometry, there is a function which offsets points of a contour - OffsetContour : [http://jsfiddle.net/prisoner849/a2ef1v5g/](http://jsfiddle.net/prisoner849/a2ef1v5g/) [OffsetContour] function OffsetContour(offset, contour) { let result = []; offset = new THREE.BufferAttribute(new Float32Array([offset, 0, 0]), 3); for (let i = 0; i \< contour.length; i++) { let v1 = new THREE.Vector2().subVectors(contour[i - 1 \< 0 ?…

---

<div class="post-metadata">

**Author:** ![fancyrainbow](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@fancyrainbow](https://discourse.threejs.org/u/fancyrainbow)\
**Post date:** [February 1, 2019, 9:28am UTC](https://discourse.threejs.org/t/how-to-find-segments-intersection/5965/11 "2019-02-01T09:28:39Z")

</div>

Thank you, I will use that as a starting point !
