Line help beginner completely new

How to make a line that is at the same position as mouse but is longer than the distance. belowimg

1 Like

Maye this helps?

float x, y;

void setup () {
  size (400, 400);
  background (255);
  x = width/2;
  y = height/2;
}

void draw () {
  background (255);
  line (x, y, mouseX, mouseY);
}

Sorry, but didn’t help. I need it to be farther than the mouse postion.

But from which starting point?

I have an ellipse(x, y, 20, 20) and from the ellipse’s position I want it to start and I want not to end the line, like on the image, where is the line longer than the distance between mouse and ellipse.

Subtracting or adding from mouseX and mouseY with some value?

And I want the line to be pointing at my cursor and not to end the line when it hits the mouse, but continue like on the image.

Hehe, for me sometimes to understand the question is harder than the code.

What do you mean by this. Normally the ponter points. Not the line.

I mean like it will be where my cursor is, look at the image.

Also, I want the line to not end but has infinite distance. Like it will never end, I don’t want it to be long from the ellipse to my Cursor but farther than my cusror

But I already replied just add a value to MouseX and MouseY.

Doesn’t work.

But if you can’t understand what I am asking for, there is a video where they have the neverending line. I’m gonna link it here > https://youtu.be/v1kqMxsvElM?t=265

1 Like

This should do what you want.

int px, py; // ellipse position
float len;
void setup(){
  size(640, 480);
  cursor(CROSS);
  len = max(width, height);
  px = 255;
  py = 199;
}

void draw(){
  background(255, 240, 200);
  drawLine(px, py, mouseX, mouseY);
  fill(0);
  noStroke();
  ellipse(px, py, 10, 10);
}

// Draw a line from [fx, fy] to pass through [tx, ty]
// to the display edge
void drawLine(float fx, float fy, float tX, float tY){
  float ang = atan2(tY - fy, tX - fx);
  float x = fx + cos(ang) * len;
  float y = fy + sin(ang) * len;
  stroke(255,0,0);
  strokeWeight(3);
  line(fx, fy, x, y);
}
3 Likes

Just to restate for future searches: OP wanted to:

  • draw a line from an origin THROUGH the mouse and PAST the edge of the display.

@quark’s solution mostly works, so long as px/py isn’t in a corner – like (10,10). Then it fails on the diagonal. For a line that is longer than the box, use:

len = sqrt(width*width + height*height)+1;

If you want to measure a line segment to exactly the edge of a bounding box that contains its endpoint – for example, if there is a rectangle in the middle of your screen, and you want to crop the line exactly – then you can use line-line collision detection to find its other endpoint.

// LINE/LINE
PVector lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {

  // calculate the direction of the lines
  float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
  float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));

  // if uA and uB are between 0-1, lines are colliding
  if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
    float ix = x1 + (uA * (x2-x1));
    float iy = y1 + (uA * (y2-y1));
    return new PVector(ix, iy);
  }
  return null;
}

continued here:

1 Like

Thanks for pointing that out I had not checked the corner position, obviously len is too small but calculating the diagonal of the screen is more than necessary simply change line to

len = 1.5 * max(width, height);

this will work for any size display. :slight_smile:

(this can be proved using basic algebra)

1 Like