Line with constant length following mouse (or even better line tracking curve)
in
Programming Questions
•
2 years ago
Please how to get line with constant length following mouse ?
(I was looking on Google but I don't even/already know what words to look for)
I have made this, but there must be smarter way to make it - without loop just with calculation.
I want to make line to follow mouse (for start), like if I pick solid stick at first point, second point will follow my hand at the same distance. Even better line with fixed length moving on a curve with both points. (Like wagon on a curved railway - and that's what's going on about here - more lines moving on a curve like train). Btw sorry for my weak english.
Thank you very much
(I was looking on Google but I don't even/already know what words to look for)
I have made this, but there must be smarter way to make it - without loop just with calculation.
I want to make line to follow mouse (for start), like if I pick solid stick at first point, second point will follow my hand at the same distance. Even better line with fixed length moving on a curve with both points. (Like wagon on a curved railway - and that's what's going on about here - more lines moving on a curve like train). Btw sorry for my weak english.
Thank you very much
- ConstLenLine lin;
- void setup() {
- size(640, 480, P2D);
- background(250);
- stroke(0);
- smooth();
- lin = new ConstLenLine();
- }
- void draw() {
- background(250);
- lin.update();
- lin.draw();
- }
- class ConstLenLine {
- float len = 50; // length of the line
- PVector A, B, eq;
- ConstLenLine() {
- A = new PVector(mouseX, mouseY);
- B = new PVector(A.x, A.y+len);
- eq = new PVector(0, 0);
- }
- void update() {
- A = new PVector(mouseX, mouseY);
- if (round(A.dist(B))>len)
- eq.add(PVector.sub(A, B));
- if (round(A.dist(B))<len)
- eq.sub(PVector.sub(A, B));
- eq.normalize();
- int count=0;
- while (round (A.dist (B)) != len) {
- //how to do it without this lame loop, just with computing
- B.add(eq);
- if (count>1000)
- break; // just for sure, I don't like infinite loops
- count++;
- }
- }
- void draw() {
- pushStyle();
- strokeWeight(len/20);
- line(A.x, A.y, B.x, B.y);
- popStyle();
- }
- }
1