Skip to content

Instantly share code, notes, and snippets.

@fiskurgit
Created June 2, 2018 19:49
Show Gist options
  • Save fiskurgit/9adb7cc2905d6c2d0b316d552103f20e to your computer and use it in GitHub Desktop.
Save fiskurgit/9adb7cc2905d6c2d0b316d552103f20e to your computer and use it in GitHub Desktop.
PShape box;
int vertexCount;
float xRotation;
float yRotation;
float zRotation;
ArrayList<PVector> coords = new ArrayList();
boolean mouseRotate = true;
boolean showNearest = false;
boolean showIndexLabels = false;
boolean showNodes = true;
void setup() {
size(500, 500, P3D);
makeBox();
}
void draw() {
background(255);
pushMatrix();
translate(width/2, height/2, 0);
if(mouseRotate){
xRotation = (mouseY/360.0)*-(TWO_PI) + PI;
yRotation = (mouseX/420.0)*(TWO_PI) - PI;
zRotation = PI/36;
}
rotateX(xRotation);
rotateY(yRotation);
rotateZ(zRotation);
scale(150);
//3D shape:
//sphere.setStroke(color(0, 40));
//shape(shape);
coords.clear();
//For each vertex find where they'd be rendered on a 2D screen
for (int i = 0; i < vertexCount; i++) {
PVector vertex = box.getVertex(i);
float x = screenX(vertex.x, vertex.y, vertex.z);
float y = screenY(vertex.x, vertex.y, vertex.z);
coords.add(new PVector(x, y));
}
popMatrix();
draw2DBox();
}
void draw2DBox() {
float prevX = -1;
float prevY = -1;
float textPad = 0;
stroke(0, 0, 0, 60);
for (int v = 0; v < vertexCount-2; v = v+4) {
float x = coords.get(v).x;
float y = coords.get(v).y;
if(showNodes){
fill(0);
ellipse(x, y, 5, 5);
//We're stepping by 4 so have to get the next 3 here:
ellipse(coords.get(v+1).x, coords.get(v+1).y, 5, 5);
ellipse(coords.get(v+2).x, coords.get(v+2).y, 5, 5);
ellipse(coords.get(v+3).x, coords.get(v+3).y, 5, 5);
noFill();
}
if(showIndexLabels){
if(prevX == x && prevY == y){
textPad = textPad + textWidth("" + (v-1)) + 3;
}else{
textPad = 0;
}
text("" + v, x + textPad, y);
prevX = x;
prevY = y;
}
//Draw the face:
line(coords.get(v).x, coords.get(v).y, coords.get(v+1).x, coords.get(v+1).y);
line(coords.get(v+1).x, coords.get(v+1).y, coords.get(v+2).x, coords.get(v+2).y);
line(coords.get(v+2).x, coords.get(v+2).y, coords.get(v+3).x, coords.get(v+3).y);
line(coords.get(v+3).x, coords.get(v+3).y, coords.get(v).x, coords.get(v).y);
}
if(showNearest){
int nearestIndex = -1;
float nearestDistance = Integer.MAX_VALUE;
int c = coords.size();
for (int i = 0; i < c; i++) {
float x = coords.get(i).x;
float y = coords.get(i).y;
float distance = dist(x, y, mouseX, mouseY);
if(distance < nearestDistance){
nearestDistance = distance;
nearestIndex = i;
}
}
stroke(0, 255, 255);
fill(0,255,255);
ellipse(coords.get(nearestIndex).x, coords.get(nearestIndex).y, 15, 15);
noFill();
}
}
void keyPressed() {
switch(key){
case 'a':
mouseRotate = !mouseRotate;
break;
case's':
showNearest = !showNearest;
break;
case 'd':
showIndexLabels = !showIndexLabels;
break;
case 'f':
showNodes = !showNodes;
break;
}
}
void makeBox() {
stroke(0, 30);
box = createShape(BOX, 1.2);
vertexCount = box.getVertexCount();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment