# How to detect if two lines are intersecting?

**URL:** <https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636>\
**Category:** Coding Questions\
**Created:** [April 10, 2020, 6:27pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636 "2020-04-10T18:27:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [April 10, 2020, 6:27pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636/1 "2020-04-10T18:27:11Z")

</div>

So if you have two lines, and they both can be curvy or straight, how do you tell if they are intersecting? Is there a way to do it with colours?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 10, 2020, 11:53pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636/2 "2020-04-10T23:53:56Z")

</div>

Start by search collision here in the forum please

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [April 12, 2020, 2:16pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636/3 "2020-04-12T14:16:45Z")

</div>

> **[Collision Detection](http://www.jeffreythompson.org/collision-detection/)**
>
> An online book about collision detection using Processing.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 14, 2020, 10:16pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636/4 "2020-04-14T22:16:30Z")

</div>

> [@Cybernet](#):
>
> they both can be curvy or straight

However: nothing in Thompson’s book will talk about “curvy” lines. If they are segments of a circle, you can use circle collision, but otherwise you are in for some more serious math that isn’t covered there. What kinds of “curvy lines” do you mean? Splines / Bezier curves? Are they defined by a mathematical formula, or are they being drawn using Processing functions like curveVertex()…?

---

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [April 14, 2020, 10:18pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636/5 "2020-04-14T22:18:22Z")

</div>

I meant circle. Sorry for confusion

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 14, 2020, 10:21pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636/6 "2020-04-14T22:21:14Z")

</div>

> [@Cybernet](#):
>
> Is there a way to do it with colours?

Yes, sort of. This is called “picking.” You use a buffer, and you check a point or points to see if the value in the buffer matches. It doesn’t work as well or easily with lines as it does with closed shapes (circles, ellipses, rects, polygons, etc.) – it is better for testing the location of a mouse pointer than for line-line intersection.

---

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [April 14, 2020, 10:25pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636/7 "2020-04-14T22:25:58Z")

</div>

Oh okay. What’s a buffer?

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 14, 2020, 10:38pm UTC](https://discourse.processing.org/t/how-to-detect-if-two-lines-are-intersecting/19636/8 "2020-04-14T22:38:16Z")

</div>

In this case, either an off-screen PImage / PGraphics object or a 2D array of numbers, one per pixel / point.

See for example the Picking Library [http://n.clavaud.free.fr/processing/library/picking/](http://n.clavaud.free.fr/processing/library/picking/)
