# Calculating if a dot is on a drawn line

**URL:** <https://discourse.processing.org/t/calculating-if-a-dot-is-on-a-drawn-line/41221>\
**Category:** Coding Questions\
**Created:** [March 10, 2023, 6:27am UTC](https://discourse.processing.org/t/calculating-if-a-dot-is-on-a-drawn-line/41221 "2023-03-10T06:27:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ingo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ingo/32/2363_2.png) [@Ingo](https://discourse.processing.org/u/Ingo)\
**Post date:** [March 10, 2023, 6:27am UTC](https://discourse.processing.org/t/calculating-if-a-dot-is-on-a-drawn-line/41221/1 "2023-03-10T06:27:27Z")

</div>

Hi everyone,

I’d like to program a game in processing (Java) in which the player draws a possibly curved line and next makes a dot on this line. I think, I would draw the line only with pixels, not lines oder bezier-curves and maybe store the drawn pixels in an ArrayList. But there are 2 problems I’m not sure how to solve **fast** :

a) The line must not cross itself or any other existing line. This would mean, I have to check **during** the drawing if the currently drawn pixel is already stored in the ArrayList.

b) The player must place a dot or small circle somewhere on its last drawn line and only on this one. So may be I should use additionaly an ArrayList for every drawn line? And every Line consist of an ArrayList of pixels?

Are there any classic approaches to doing it somehow efficiently? Are there any mathematical approaches?

Thanks in advance, Ingo

Any tips on a suitable data structure, mathematics or source code would be great.

Addition: I tried working with only the pixels, but I get only holes where the lines intersect:

```auto
void setup() {
  size(600, 400);
  background(0);
  stroke(255);
  strokeWeight(3);
}

void draw() {}

void mouseDragged() {
  if (pmouseX!=0&&pmouseY!=0) {
    loadPixels();
    int i=(mouseY-1)*width+mouseX;
    if (brightness(pixels[i])==0) {
      line(pmouseX, pmouseY, mouseX, mouseY);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [March 12, 2023, 3:19pm UTC](https://discourse.processing.org/t/calculating-if-a-dot-is-on-a-drawn-line/41221/2 "2023-03-12T15:19:04Z")

</div>

Hey maybe [this](https://jeffreythompson.org/collision-detection/line-point.php) can help.

---

<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:** [March 12, 2023, 6:40pm UTC](https://discourse.processing.org/t/calculating-if-a-dot-is-on-a-drawn-line/41221/3 "2023-03-12T18:40:23Z")

</div>

> [@Ingo](#):
>
> I tried working with only the pixels, but I get only holes where the lines intersect:

this works when you don’t move the mouse too fast

```auto

boolean mouseIsDown=false;

void setup() {
  size(600, 400);

  background(0);
  stroke(255);
  strokeWeight(1);
}

void draw() {
  //
  if (mouseIsDown) {
    if (pmouseX!=0&&pmouseY!=0) {
      //loadPixels();
      //int i=(mouseY-1)*width+mouseX;
      if (mouseX!=pmouseX||mouseY!=pmouseY) {
        //
        // checkMouse();
        if (brightness(get(mouseX, mouseY))==0 ) {
          line(pmouseX, pmouseY, mouseX, mouseY);
        } else {
          mouseIsDown=false;
        }//else
      }//if
    }//if
  }//if
}//draw

// ----------------------------------------------------------------------------------------------------

void checkMouse() {
  // doesn't work
  if (dist (mouseX, mouseY, pmouseX, pmouseY) < 10)
    return;

  for (int i = 2; i <= 8; i++) {
    float x = lerp(mouseX, pmouseX, i/10.0);
    float y = lerp(mouseY, pmouseY, i/10.0);

    if (brightness(get(int(x), int(y)))==0 ) {
      // line(pmouseX, pmouseY, mouseX, mouseY);
    } else {
      mouseIsDown=false;
    }//else
  }
}

void mousePressed() {
  mouseIsDown=true;
}

void mouseReleased() {
  mouseIsDown=false;
}
//

```

---

<div class="post-metadata">

**Author:** ![Ingo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ingo/32/2363_2.png) [@Ingo](https://discourse.processing.org/u/Ingo)\
**Post date:** [March 20, 2023, 11:14am UTC](https://discourse.processing.org/t/calculating-if-a-dot-is-on-a-drawn-line/41221/4 "2023-03-20T11:14:32Z")

</div>

That’s a great idea. Thanks.
