here you can click mouse in upper right corner
//
final int STATE_ENTER = 0;
final int SHOW_3D = 1;
int state = STATE_ENTER;
PImage hm;
int xstep = 1;
int max_height = 120;
//
final String HELP =
"Click mouse to draw vertex."
+"\nBackspace to delete last item."
+"\nHit c to delete entire list."
+"\nHit Space bar: fill on/off."
+"\nHit e to export." ;
ArrayList<PVector> list = new ArrayList();
boolean showFill = false;
void setup() {
size(888, 888, P3D);
background(111);
}
void draw() {
background(111);
//
if (state == STATE_ENTER) {
// ENTER ------------------------
// example shape / optional
showCross(300, 300);
// the list that has been entered
showList();
// show Instructions
showInstructions();
} else if (state == SHOW_3D) {
// SHOW ------------------------
background(0);
strokeWeight(1);
stroke(2, 255, 244, 244);
float b, z, px, pz;
translate(width/2, height/2, -20);
rotateY(map(mouseX, 0, width, -PI, PI));
rotateX(map(mouseY, 0, height, -PI, PI));
translate(-width/2, -height/2);
for (int y = 15; y < height; y+=8) {
px = -1;
pz = 4;
for (int x = 0; x < width; x+=xstep) {
b = brightness(hm.get(x, y));
z = map(b, 0, 800, 0, max_height);
// stroke(color(b));
line(px, y, pz, x, y, z);
px = x;
pz = z;
}
}
}//else if
}
// ----------------------------------------------
// Inputs
void mousePressed () {
//println(
// "vertex("
// +mouseX
// +", "
// +mouseY
// +");");
if (mouseX<65&&mouseY<65) {
hm = get();
state = SHOW_3D;
return;
}
PVector pv = new PVector(mouseX, mouseY) ;
list.add(pv);
}
void keyPressed() {
if (key=='c') {
// delete all
list.clear();
} else if (key==BACKSPACE) {
// delete last
if (list.size()>0) {
list.remove(list.size()-1);
}
} else if ( key==' ' ) {
// toggle
showFill = !showFill ;
} else if ( key=='e' ) {
// export
export();
} else if ( key==ENTER||key==RETURN ) {
hm = get();
state = SHOW_3D;
}// else if
}//func
//------------------------------------------------------------
// Tools
void showInstructions() {
fill(255);
text("Mouse: "
+mouseX
+","
+mouseY
+"\n\n" + HELP,
width-210, 19,
200, 900);
}
void export() {
//
println( "\n// -------------------------------");
println( "\n// " + dateTimeStamp() );
println( "void setup() {");
println( " size(888, 888);");
println( " background(111);");
println( "} //func");
println( "");
println( "void draw() {");
println( " background(111);");
println( " drawShape1();");
println( "} //func");
println( "");
println( "void drawShape1() {");
println( " beginShape();" );
println( " stroke(0); " );
println( " fill( 255, 0, 0); " );
for (PVector pv : list) {
println(
" vertex("
+pv.x
+", "
+pv.y
+");");
}//for
println( " endShape(CLOSE); " );
println( "} //func");
println( "// -------------------------------");
}
void showList() {
beginShape();
if (showFill) {
fill( 255, 0, 0);
} else {
noFill();
}
noStroke();
stroke(0);
for (PVector pv : list) {
vertex(pv.x, pv.y);
}
endShape();
//
for (PVector pv : list) {
pointPVector(pv);
}
}
void pointPVector( PVector pv) {
//
fill(0, 0, 255);
stroke(255);
point (pv.x, pv.y);
point (pv.x, pv.y+1);
point (pv.x+1, pv.y);
point (pv.x, pv.y+2);
point (pv.x+2, pv.y);
}
// ----------------------------------------------------------------------
// Tools date and time
String dateTimeStamp() {
// short version for file names
return getDate() + ", " + getTime();
}
String getDate() {
return leadingZeros(year())
+ "/"+leadingZeros(month())
+ "/"+leadingZeros(day());
}
String getTime() {
return leadingZeros(hour())
+ ":"+leadingZeros(minute())
+ ":"+leadingZeros(second());
}
String leadingZeros(int a) {
String Buffer;
Buffer=nf(a, 2);
return(Buffer);
}
//-----------------------------------------------------------------------
// optional
void showCross(float x1, float y1) {
// example shape / optional
pushMatrix();
// the \ line ----------
beginShape();
fill( 255, 0, 0);
noStroke();
vertex(35, 27);
vertex(48, 17);
vertex(85, 80);
vertex(85-13, 80+3);
endShape(CLOSE);
// the / line -------
beginShape();
fill( 255, 0, 0);
vertex(35, 80);
vertex(48, 87);
vertex(85, 20);
vertex(85-13, 20-3-2);
endShape(CLOSE);
popMatrix();
}
//