How can I modify this 2D sketch to do something similar in 3D ?
Which classes/library would you recommend ?
Also, how can I insert a different number per line within the code in my messages to the forum ?
Thank you in advance.
Nesnduma
- //
// Mesh creation m x n points with mobile vertices
//
import controlP5.*;
ControlP5 controlP5;
int rows = 15; // number of rows
int cols = 8; // number of cols
int points = rows * cols ; // number of points
int x_size = 500; // width
int y_size = 500; // height
int margin = 50; // margin
int button_height = 100; // additional height for button zone
//int coords = 2; // number of coordinates per point (2D at the moment)
int bgColor = color(255, 255, 255); // background color
int squareColor = color(128, 0, 0); // square color upon mouse over point
boolean bover = false;
boolean locked = false;
int movingPoint = -1 ; // number of selected point
PVector[] mesPoints = new PVector[points];
// this is only a note.
// we will not use variable b in the code below.
// we have to use controlP5.Button here since there
// would be a conflict if we only use Button to declare button b.
controlP5.Button b;
// a button-controller with name buttonValue will change the
// value of this variable when pressed.
//int buttonValue = 0;
//********** SETUP
void setup()
{
smooth();
ellipseMode(CENTER);
rectMode(CENTER);
size(x_size, (y_size+button_height));
background(bgColor);
//Button
controlP5 = new ControlP5(this);
controlP5.addButton("Bouton1",0,50, (y_size+50),88,21);
controlP5.setColorActive(color(255, 0, 0)); // color for mouse-over
controlP5.setColorBackground(color(128, 0, 0)); // default background color
controlP5.setColorLabel(color(255, 255, 255)); // text color
controlP5.controller("Bouton1").setLabel("Regenerate points"); // button label
//controlP5.setColorForeground(color(128, 0, 0)); // default color
// Creates positions for points
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int current_point = (cols * i)+j;
mesPoints[current_point] = new PVector( ((((x_size - (2*margin))/(cols-1)) * j)+margin), ((((y_size - (2*margin))/(rows-1)) * i)+margin));
}
}
}
//********** DRAW
void draw()
{
// Erases everything - redraws lines
background(bgColor);
// Draw lines
strokeWeight(2);
for (int i = 0; i < rows ; i++) {
for (int j = 0; j < cols ; j++) {
int current_point = (cols * i)+j;
//Draw horizontal lines
if (j < cols - 1) { // all but last column
if ((i == 0) || (i == (rows - 1))) { // first and last rows
stroke(0, 0, 0); // outer lines are black
} else {
stroke(0,192,255); // inner lines are blue
}
line(mesPoints[current_point].x, mesPoints[current_point].y, mesPoints[current_point+1].x, mesPoints[current_point+1].y);
}
//Draw vertical lines
if (i < rows - 1) { // all but last line
if ((j == 0) || (j == (cols - 1))) { // first and last column
stroke(0, 0, 0); // outer lines are black
} else {
stroke(0,192,255); // inner lines are blue
}
line(mesPoints[current_point].x, mesPoints[current_point].y, mesPoints[current_point+cols].x, mesPoints[current_point+cols].y);
}
}
}
// Draws points
strokeWeight(1);
for (int i = 0; i < (rows * cols) ; i++) {
if ( (i % cols == 0) || (i % cols == (cols - 1)) || (((i+1) / cols) == 0) || (((i+1) / cols) == rows - 1 ) ) { //outer points
stroke(0,0, 0);
fill(0,0,0);
} else { // inner points
stroke(0,192, 255);
fill(255,255,0);
}
ellipse(mesPoints[i].x, mesPoints[i].y, 10, 10);
}
//Tests whether mouse rolls over points and draws red squares
strokeWeight(1);
if (locked) {
squareColor = color(255, 0, 0);
} else {
squareColor = color(128, 0, 0);
}
stroke(squareColor);
fill(squareColor);
for (int i = 0; i < points; i++) {
if(dist(mouseX, mouseY, mesPoints[i].x, mesPoints[i].y) <= 5) {
movingPoint = i;
rect(mesPoints[i].x, mesPoints[i].y, 10, 10);
i = points ; //not necessary to go on with loop
} else {
movingPoint = -1;
}
}
if (movingPoint != -1) {
bover = true ;
} else {
bover = false;
}
//
}
//********** Detects whether mouse button is pressed
void mousePressed() {
if(bover) {
locked = true;
} else {
locked = false;
}
}
//********** Moves while button held
void mouseDragged() {
if(locked) {
strokeWeight(1);
stroke(255,0,0);
fill(255,0,0);
mesPoints[movingPoint].x = mouseX;
mesPoints[movingPoint].y = mouseY;
rect(mesPoints[movingPoint].x, mesPoints[movingPoint].y, 10, 10);
}
}
//********** Unlocks upon button release
void mouseReleased() {
locked = false;
bover = false;
}
// function Bouton1 will receive changes from
// controller with name controlP5
public void Bouton1(int theValue) {
for (int i = 0; i < rows; i++) {
int randomX;
int randomY;
for (int j = 0; j < cols; j++) {
int current_point = (cols * i)+j;
if ( (current_point % cols == 0) || (current_point % cols == (cols - 1)) || (((current_point+1) / cols) == 0) || (((current_point+1) / cols) == rows - 1 ) ) { //outer points
randomX = 0;
randomY = 0;
} else { // inner points
randomX = int(random(-10, 10));
randomY = int(random(-10, 10));
}
mesPoints[current_point] = new PVector( ((((x_size - (2*margin))/(cols-1)) * j)+margin+randomX), ((((y_size - (2*margin))/(rows-1)) * i)+margin+randomY));
}
}
}
//*********
// END
//*********