my problem is that i need to update the value in void setup or even foe global variable, but i don't know how to code it!
here is my code and i need to update blue line according to slider output, im new to processing and programming . . .
PShape s; // The PShape object
closeRect ham;
//Declare for sliders
Slider sliderCol;
Slider sliderRow;
Slider sliderCellx;
Slider sliderCelly;
int start=1;
int startY=1;
int end=8;
float minCellSize=5;
float maxCellSize=100;
//Declaration fpr grid
float w=400;
float h=400;
int vNum=10;
int hNum=10;
float wSh=(w / vNum);
float hSh=(h/hNum);
Point grid[][]=new Point[vNum+1][hNum+1];
void setup() {
size(700, 700, P2D);
for (int i=0;i<vNum+1;i++) {
for (int j=0;j<hNum+1;j++) {
grid[i][j]=new Point((i*wSh)+200, (j*hSh+200));
}
}
//Make sliders
sliderCol=new Slider("colume", 40, 20, 100, 13, start, end);
sliderRow=new Slider("Row", 40, 40, 100, 13, startY, end);
sliderCellx=new Slider("CellX", 40, 60, 100, 13, minCellSize, maxCellSize);
sliderCelly=new Slider("CellY", 40, 80, 100, 13, minCellSize, maxCellSize);
}
void draw() {
background(0);
//run Sliders
sliderCol.run();
sliderRow.run();
sliderCellx.run();
sliderCelly.run();
//Get Slider's Value and check the minimum values
float newwSh=sliderCellx.getValue();
float newhSh=sliderCelly.getValue();
//chek for 0 values for colume and row
int newcol=round(sliderCol.getValue());
if (newcol==0) {
newcol=1;
}
int newrow=round(sliderRow.getValue());
if (newrow==0) {
newrow=1;
}
//Check for grid Update
if (newcol!=vNum||newrow!=hNum||newwSh!=wSh||newhSh!=hSh) {
vNum=newcol;
hNum=newrow;
hSh=sliderCelly.getValue();
wSh=sliderCellx.getValue();
print("yes");
Point grid[][]=new Point[vNum+1][hNum+1];
for (int i=0;i<vNum+1;i++) {
for (int j=0;j<hNum+1;j++) {
grid[i][j]=new Point((i*wSh)+200, (j*hSh+200));
}
}
}
for (int i=0;i<vNum+1;i++) {
for (int j=0;j<hNum+1;j++) {
//UPDATE COORDINATES
float taPtx=(grid[i][j].xvalue());
float taPty=(grid[i][j].yvalue());
//Chech the border
float newx=mouseX;
float newy=mouseY;
if (mouseX>(vNum*wSh)+200) {
newx=(vNum*wSh)+200;
}
if (mouseX<10) {
newx=10;
}
if (mouseY>(hNum*hSh)+200) {
newy=(hNum*hSh)+200;
}
if (mouseY<200) {
newy=200;
}
//ACTUAL UPDATING
float d=(sqrt(sq(taPtx-newx)+sq(taPty-newy)));
if (mousePressed) {
if (d<10) {
grid[i][j]=new Point(newx,newy);
}
}
}
}
//DRAW POINTS
for (int i=0;i<vNum+1;i++) {
for (int j=0;j<hNum+1;j++) {
grid[i][j].run();
}
}
for (int i=0;i<vNum;i++) {
for (int j=0;j<hNum;j++) {
float v0x=(grid[i][j].xvalue());
float v0y=(grid[i][j].yvalue());
float v1x=(grid[i+1][j].xvalue());
float v1y=(grid[i+1][j].yvalue());
float v3x=(grid[i][j+1].xvalue());
float v3y=(grid[i][j+1].yvalue());
float v2x=(grid[i+1][j+1].xvalue());
float v2y=(grid[i+1][j+1].yvalue());
ham=new closeRect(v0x, v0y, v1x, v1y,v2x, v2y, v3x, v3y);
ham.run();
}
}
}
class Point {
float x, y;
Point(float _x, float _y) {
x=_x;
y=_y;
}
void run() {
display();
}
void display() {
stroke(255, 0, 0);
strokeWeight(4);
point(x, y);
}
float xvalue() {
return x ;
}
float yvalue() {
return y ;
}
}
class Slider {
float topCornerX;
float topCornerY;
float downCornerX;
float downCornerY;
float startDomain;
float endDomain;
float sliderPos;
float newsliderPos;
boolean over; // is the mouse over the slider?
boolean locked;
String sliderName;
PFont f;
Slider(String _sliderName, float _topCornerX, float _topCornerY, float _downCornerX, float _downCornerY, float _startDomain, float _endDomain) {
topCornerX=_topCornerX;
topCornerY=_topCornerY;
downCornerX=_downCornerX;
downCornerY=_downCornerY;
startDomain=_startDomain;
endDomain=_endDomain;
sliderPos=topCornerX+(downCornerX/2);
newsliderPos=sliderPos;
sliderName=_sliderName;
f = createFont("Arial", 16, true);
}
void run() {
update();
display();
sliderTag();
}
void update() {
if (overEvent()) {
over = true;
}
else {
over = false;
}
if (mousePressed && over) {
locked = true;
}
if (!mousePressed) {
locked = false;
}
if (locked) {
newsliderPos = constrain(mouseX-topCornerX, 0, downCornerX);
}
if (abs(newsliderPos - sliderPos) > 1) {
sliderPos = newsliderPos;
}
}
void display() {
stroke(100, 149, 237);
fill(100, 149, 237);
rect(topCornerX, topCornerY, downCornerX, downCornerY);
stroke(0, 0, 255);
fill(0, 0, 255);
rect(topCornerX, topCornerY, sliderPos, downCornerY);
}
void sliderTag() {
textFont(f, 10); // STEP 4 Specify font to be used
fill(30, 144, 255); // STEP 5 Specify font color
text(sliderName, topCornerX+downCornerX+5, topCornerY+downCornerY-(downCornerY/2));
textFont(f, 8); // STEP 4 Specify font to be used
fill(0, 206, 209); // STEP 5 Specify font color
text(round(startDomain+(sliderPos/downCornerX*(endDomain-startDomain))), topCornerX+downCornerX+7, topCornerY+downCornerY+(downCornerY/8));
}
float constrain(float val, float minv, float maxv) {
return min(max(val, minv), maxv);
}
boolean overEvent() {
if (mouseX > topCornerX && mouseX < topCornerX+downCornerX &&
mouseY > topCornerY && mouseY < topCornerY+downCornerY) {
return true;
}
else {
return false;
}
}
float getValue() {
return startDomain+(sliderPos/downCornerX*(endDomain-startDomain));
}
}
class closeRect {
float v0x,v0y,v1x,v1y,v2x,v2y,v3x,v3y;
closeRect(float _v0x,float _v0y, float _v1x,float _v1y, float _v2x,float _v2y, float _v3x,float _v3y) {
v0x=_v0x;
v0y=_v0y;
v1x=_v1x;
v1y=_v1y;
v2x=_v2x;
v2y=_v2y;
v3x=_v3x;
v3y=_v3y;
}
void run() {
display();
}
void display() {
s = createShape();
s.beginShape();
s.noFill();
s.stroke(255, 200, 100);
s.strokeWeight(1);
s.vertex(v0x-25, v0y-25);
s.vertex(v1x-25, v1y-25);
s.vertex(v2x-25, v2y-25);
s.vertex(v3x-25, v3y-25);
s.endShape();
s.endShape(CLOSE);
shape(s, 25, 25);
}
}