Calculating if a dot is on a drawn line

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:

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);
    }
  }
}
1 Like

Hey maybe this can help.

1 Like

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


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;
}
//

1 Like

That’s a great idea. Thanks.