you need to store the coordinates of the line, indeed. that means, 2 points. or 4 numbers (int). you can do that with a class too. example (click & drag) :
without any class :
Code:int x1, y1, x2, y2;
void draw() {
background(255);
line(x1, y1, x2, y2);
}
void mouseDragged() {
x2 = mouseX; y2 = mouseY;
}
void mousePressed() {
x1 = mouseX; y1 = mouseY;
x2 = mouseX; y2 = mouseY;
}
example with a
Point class :
Code:Point p1, p2;
class Point {
int x, y;
void setXY(int x, int y) {
this.x = x; this.y = y;
}
}
void setup() {
p1 = new Point();
p2 = new Point();
}
void draw() {
background(255);
line(p1.x, p1.y, p2.x, p2.y);
}
void mouseDragged() {
p2.setXY(mouseX, mouseY);
}
void mousePressed() {
p1.setXY(mouseX, mouseY);
p1.setXY(mouseX, mouseY);
}
minimalist example with a
Line class :
Code:Line li;
class Line {
Point p1, p2;
Line() {
p1 = new Point();
p2 = new Point();
}
void display() {
line(p1.x, p1.y, p2.x, p2.y);
}
}
class Point {
int x, y;
void setXY(int x, int y) {
this.x = x; this.y = y;
}
}
void setup() {
li = new Line();
}
void draw() {
background(255);
li.display();
}
void mouseDragged() {
li.p2.setXY(mouseX, mouseY);
}
void mousePressed() {
li.p1.setXY(mouseX, mouseY);
li.p1.setXY(mouseX, mouseY);
}