How can I improve the displacement of vertices with mousedrag ?
in
Programming Questions
•
2 years ago
Hello,
This is my first Processing project.
When the mouse is dragged close to a vertex, it is moved with the pointer. However there are two problems:
1. If I move the mouse too fast, the vertex is "lost" by the mouse and doesn't move anymore.
2. If a vertex is too close to another, they both move with the mouse.
Any idea ?
Thank you in advance.
Nesnduma
This is my first Processing project.
When the mouse is dragged close to a vertex, it is moved with the pointer. However there are two problems:
1. If I move the mouse too fast, the vertex is "lost" by the mouse and doesn't move anymore.
2. If a vertex is too close to another, they both move with the mouse.
Any idea ?
Thank you in advance.
Nesnduma
- //
// Displacement of vertices by mousedrag
//
int points = 4;
int coords = 2;
int bgColor = color(255, 255, 255);
int[][] mesPoints = new int[points][coords];
//********** SETUP
void setup()
{
smooth();
ellipseMode(CENTER);
rectMode(CENTER);
size(400, 400);
background(bgColor);
// Creates random positions for points
// Two embedded loops
for (int i = 0; i < points; i++) {
for (int j = 0; j < coords; j++) {
mesPoints[i][j] = int(random(0,400));
}
print("Point ");
println(i+" :");
println(mesPoints[i]);
}
}
//********** DRAW
void draw()
{
// Erases everything - redraws lines
background(bgColor);
for (int i = 0; i < points ; i++) {
// Draws lines
strokeWeight(2);
stroke(0,192,255);
line(mesPoints[i][0], mesPoints[i][1], mesPoints[(i+1)%points][0], mesPoints[(i+1)%points][1]);
}
// Draws points
for (int i = 0; i < points ; i++) {
strokeWeight(1);
// Points
stroke(0,192, 255);
fill(255,255,0);
ellipse(float(mesPoints[i][0]), float(mesPoints[i][1]), 10., 10.);
}
//Test whether mouse rolls over points and draws red squares
strokeWeight(1);
stroke(255,0,0);
fill(255,0,0);
for (int i = 0; i < points; i++) {
if((abs(mouseX - mesPoints[i][0]) <= 5) && (abs(mouseY - mesPoints[i][1]) <= 5)) {
rect(mesPoints[i][0], mesPoints[i][1], 10, 10);
}
}
}
//********** MOUSEDRAG + UPDATE POSITIONS
void mouseDragged() {
strokeWeight(1);
stroke(255,0,0);
fill(255,0,0);
for (int i = 0; i < points; i++) {
if((abs(mouseX - mesPoints[i][0]) <= 5) && (abs(mouseY - mesPoints[i][1]) <= 5)) {
mesPoints[i][0] = mouseX;
mesPoints[i][1] = mouseY;
rect(mesPoints[i][0], mesPoints[i][1], 10, 10);
}
}
}
1