updating interpolation nodes to sliders
in
Contributed Library Questions
•
4 months ago
guys, i m trying to create a controller to drag points in processing, the issue is that i m trying to connect the width of an edge to a slider to give a parameter to the main rectangle, but the nodes doesn t update with the rectangle. Any ideas to solve it ?????
Thanks, here you have the code
//Interpolation controller ==========================================
import controlP5.*;
ControlP5 cp5;
Node dragNode = null;
ArrayList nodes1;
int nodeCount = 1;
int ptsA=120;
int ptsB=120;
int ptsC=120;
int ptsD=120;
float mx,my;
float x = 20;
float y = 200;
float w = 300;
float h = 600;
float d = 400;
float offset = 20;
float pointsA=8;
float pointsB=8;
float pointsC=8;
float pointsD=8;
int posx1=90;
int posy1=90;
int posx2=530;
int posy2=530;
void setup() {
size(1200, 800);
smooth();
nodes1 = new ArrayList();
cp5 = new ControlP5(this);
// add a horizontal sliders, the value of this slider will be linked
// to variable 'sliderValue'
cp5.addSlider("w")
.setPosition(100,50)
.setRange(3,500)
;
//points and parameter for subdivision line A
float stepA=dist(x+w-offset, y+offset, x+w-offset, y+d-offset)/pointsA;
for (float ptsparA = 0; ptsparA < dist(x+offset, y+offset, x+w-offset, y+offset); ptsparA+=stepA) {
Node n = new Node(x+offset+ptsparA,y+offset, 5);
nodes1.add(n);
}
//points and parameter for subdivision line B
float stepB=dist(x+w-offset, y+offset, x+w-offset, y+d-offset)/pointsB;
for (float ptsparB = 0; ptsparB < dist(x+w-offset, y+offset, x+w-offset, y+d-offset); ptsparB+=stepB) {
Node b = new Node(x+w-offset, ptsparB+y+offset, 5);
nodes1.add(b);
}
//points and parameter for subdivision line C
float distC=dist(x+w-offset, y+d-offset, x+offset, y+d-offset);
float stepC=dist(x+w-offset, y+d-offset, x+offset, y+d-offset)/pointsC;
for (float ptsparC =0; ptsparC < dist(x+w-offset, y+d-offset, x+offset, y+d-offset); ptsparC+= stepC) {
Node c = new Node(-ptsparC+(x+w-offset),y+d-offset, 5);
nodes1.add(c);
}
//points and parameter for subdivision line D
float stepD=dist(x+offset, y+d-offset,x+offset,y+offset)/pointsD;
for (float ptsparD =0; ptsparD <dist(x+offset, y+d-offset,x+offset,y+offset) ; ptsparD+= stepD) {
Node e = new Node(x+offset,-ptsparD+(y+d-offset), 5);
nodes1.add(e);
}
}
void draw() {
background(255);
//Lines costruction_____________________________________________________________________________________________________________________________________________________________
line (x, y, x+w, y);
line (x+w, y, x+w, y+d);
line ( x+w, y+d, x, y+d);
line (x, y+d,x,y);
line (x+offset, y+offset, x+w-offset, y+offset);
line (x+w-offset, y+offset, x+w-offset, y+d-offset);
line (x+w-offset, y+d-offset, x+offset, y+d-offset);
line (x+offset, y+d-offset,x+offset,y+offset);
stroke(255);
//Nodes lines____________________________________________________________________________________________________________________________________________________________________________
for (int i = 0; i < nodes1.size(); i++) {
Node n = (Node) nodes1.get(i);
n.draw();
stroke(1);
noFill();
beginShape();
for (int a = 0; a < nodes1.size(); a++) {
Node p1 = (Node)nodes1.get(a);
vertex(p1.x, p1.y);
}
endShape(CLOSE);
}
//interaction___________________________________________________________________________________________________________________________________________________________________________
}
void mousePressed() {
dragNode = null;
Node n;
for (int i = 0; i < nodes1.size(); i++) {
n = (Node) nodes1.get(i);
if (n.contains(pmouseX, pmouseY)) {
dragNode = n;
}
}
}
void mouseDragged() {
if (dragNode != null) {
//dragNode.x = mouseX;
//dragNode.y = mouseY;
mx = constrain(pmouseX, x+offset, x+w-offset);
my = constrain(pmouseY,y+offset,y+d-offset);
dragNode.x = mx;
dragNode.y = my;
}
}
void mouseReleased() {
dragNode = null;
}
//Class node ==========================================
class Node {
float x, y, r;
Node(float x, float y, float r) {
this.x = x;
this.y = y;
this.r = r;
}
void draw() {
fill(255, 59, 59);
stroke(60);
strokeWeight(1);
ellipse(x, y, r * 2f, r * 2f);
}
boolean contains(float x, float y) {
float dx = this.x - x;
float dy = this.y - y;
return sqrt(dx * dx + dy * dy) <= r;
}
}
Thanks, here you have the code
//Interpolation controller ==========================================
import controlP5.*;
ControlP5 cp5;
Node dragNode = null;
ArrayList nodes1;
int nodeCount = 1;
int ptsA=120;
int ptsB=120;
int ptsC=120;
int ptsD=120;
float mx,my;
float x = 20;
float y = 200;
float w = 300;
float h = 600;
float d = 400;
float offset = 20;
float pointsA=8;
float pointsB=8;
float pointsC=8;
float pointsD=8;
int posx1=90;
int posy1=90;
int posx2=530;
int posy2=530;
void setup() {
size(1200, 800);
smooth();
nodes1 = new ArrayList();
cp5 = new ControlP5(this);
// add a horizontal sliders, the value of this slider will be linked
// to variable 'sliderValue'
cp5.addSlider("w")
.setPosition(100,50)
.setRange(3,500)
;
//points and parameter for subdivision line A
float stepA=dist(x+w-offset, y+offset, x+w-offset, y+d-offset)/pointsA;
for (float ptsparA = 0; ptsparA < dist(x+offset, y+offset, x+w-offset, y+offset); ptsparA+=stepA) {
Node n = new Node(x+offset+ptsparA,y+offset, 5);
nodes1.add(n);
}
//points and parameter for subdivision line B
float stepB=dist(x+w-offset, y+offset, x+w-offset, y+d-offset)/pointsB;
for (float ptsparB = 0; ptsparB < dist(x+w-offset, y+offset, x+w-offset, y+d-offset); ptsparB+=stepB) {
Node b = new Node(x+w-offset, ptsparB+y+offset, 5);
nodes1.add(b);
}
//points and parameter for subdivision line C
float distC=dist(x+w-offset, y+d-offset, x+offset, y+d-offset);
float stepC=dist(x+w-offset, y+d-offset, x+offset, y+d-offset)/pointsC;
for (float ptsparC =0; ptsparC < dist(x+w-offset, y+d-offset, x+offset, y+d-offset); ptsparC+= stepC) {
Node c = new Node(-ptsparC+(x+w-offset),y+d-offset, 5);
nodes1.add(c);
}
//points and parameter for subdivision line D
float stepD=dist(x+offset, y+d-offset,x+offset,y+offset)/pointsD;
for (float ptsparD =0; ptsparD <dist(x+offset, y+d-offset,x+offset,y+offset) ; ptsparD+= stepD) {
Node e = new Node(x+offset,-ptsparD+(y+d-offset), 5);
nodes1.add(e);
}
}
void draw() {
background(255);
//Lines costruction_____________________________________________________________________________________________________________________________________________________________
line (x, y, x+w, y);
line (x+w, y, x+w, y+d);
line ( x+w, y+d, x, y+d);
line (x, y+d,x,y);
line (x+offset, y+offset, x+w-offset, y+offset);
line (x+w-offset, y+offset, x+w-offset, y+d-offset);
line (x+w-offset, y+d-offset, x+offset, y+d-offset);
line (x+offset, y+d-offset,x+offset,y+offset);
stroke(255);
//Nodes lines____________________________________________________________________________________________________________________________________________________________________________
for (int i = 0; i < nodes1.size(); i++) {
Node n = (Node) nodes1.get(i);
n.draw();
stroke(1);
noFill();
beginShape();
for (int a = 0; a < nodes1.size(); a++) {
Node p1 = (Node)nodes1.get(a);
vertex(p1.x, p1.y);
}
endShape(CLOSE);
}
//interaction___________________________________________________________________________________________________________________________________________________________________________
}
void mousePressed() {
dragNode = null;
Node n;
for (int i = 0; i < nodes1.size(); i++) {
n = (Node) nodes1.get(i);
if (n.contains(pmouseX, pmouseY)) {
dragNode = n;
}
}
}
void mouseDragged() {
if (dragNode != null) {
//dragNode.x = mouseX;
//dragNode.y = mouseY;
mx = constrain(pmouseX, x+offset, x+w-offset);
my = constrain(pmouseY,y+offset,y+d-offset);
dragNode.x = mx;
dragNode.y = my;
}
}
void mouseReleased() {
dragNode = null;
}
//Class node ==========================================
class Node {
float x, y, r;
Node(float x, float y, float r) {
this.x = x;
this.y = y;
this.r = r;
}
void draw() {
fill(255, 59, 59);
stroke(60);
strokeWeight(1);
ellipse(x, y, r * 2f, r * 2f);
}
boolean contains(float x, float y) {
float dx = this.x - x;
float dy = this.y - y;
return sqrt(dx * dx + dy * dy) <= r;
}
}
1