Last active
June 2, 2018 18:02
-
-
Save fiskurgit/8e23a33507042dfa7b990083ff73f448 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PShape sphere; | |
int sphereDetail = 8; | |
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); | |
makeSphere(); | |
} | |
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 = sphere.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(); | |
stroke(0); | |
text("Sphere detail: " + sphereDetail, 10, 15); | |
draw2DSphere(); | |
} | |
void draw2DSphere() { | |
float prevX = -1; | |
float prevY = -1; | |
float textPad = 0; | |
stroke(0, 0, 0, 60); | |
for (int v = 0; v < vertexCount; v++) { | |
float x = coords.get(v).x; | |
float y = coords.get(v).y; | |
if(showNodes){ | |
fill(0); | |
ellipse(x, 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 triangle: | |
if(v < vertexCount - sphereDetail - 1){ | |
line(coords.get(v).x, coords.get(v).y, coords.get(v+sphereDetail).x, coords.get(v+sphereDetail).y); | |
line(coords.get(v+sphereDetail).x, coords.get(v+sphereDetail).y, coords.get(v+ sphereDetail+1).x, coords.get(v+sphereDetail+1).y); | |
line(coords.get(v+sphereDetail +1).x, coords.get(v+sphereDetail + 1).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 'w': | |
sphereDetail++; | |
makeSphere(); | |
break; | |
case 'q': | |
sphereDetail--; | |
makeSphere(); | |
break; | |
case 'a': | |
mouseRotate = !mouseRotate; | |
break; | |
case's': | |
showNearest = !showNearest; | |
break; | |
case 'd': | |
showIndexLabels = !showIndexLabels; | |
break; | |
case 'f': | |
showNodes = !showNodes; | |
break; | |
} | |
} | |
void makeSphere() { | |
stroke(0, 30); | |
sphereDetail(sphereDetail); | |
sphere = createShape(SPHERE, 1.2); | |
vertexCount = sphere.getVertexCount(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment