Created
June 1, 2018 18:21
-
-
Save fiskurgit/ca4cd52af66291d50c95013760ee476e to your computer and use it in GitHub Desktop.
This file contains 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; | |
float xRotation; | |
float yRotation; | |
float zRotation; | |
ArrayList<PVector> coords = new ArrayList(); | |
boolean rotateActive = true; | |
void setup(){ | |
size(500, 500, P3D); | |
makeSphere(); | |
} | |
void makeSphere(){ | |
noFill(); | |
sphereDetail(sphereDetail); | |
sphere = createShape(SPHERE, 1); | |
sphere.setStroke(color(0, 40)); | |
} | |
void draw(){ | |
background(255); | |
//3D drawing: | |
pushMatrix(); | |
translate(width/2, height/2, 0); | |
if(rotateActive){ | |
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); | |
shape(sphere); | |
coords.clear(); | |
int numVertices = sphere.getVertexCount(); | |
for (int i = 0; i < numVertices; 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(); | |
//2D drawing | |
int nearestIndex = -1; | |
float nearestDistance = Integer.MAX_VALUE; | |
for (int j = 0; j < numVertices; j++) { | |
PVector v = coords.get(j); | |
fill(0); | |
ellipse(v.x, v.y, 10, 10); | |
noFill(); | |
//Check distance to mouse | |
float distance = dist(v.x, v.y, mouseX, mouseY); | |
if(distance < nearestDistance){ | |
nearestDistance = distance; | |
nearestIndex = j; | |
} | |
} | |
if(!rotateActive){ | |
fill(255, 0, 0); | |
noStroke(); | |
PVector closest = coords.get(nearestIndex); | |
ellipse(closest.x, closest.y, 20, 20); | |
} | |
} | |
void keyPressed(){ | |
switch(key){ | |
case 'q': | |
sphereDetail--; | |
makeSphere(); | |
break; | |
case 'w': | |
sphereDetail++; | |
makeSphere(); | |
break; | |
case 'a': | |
rotateActive = false; | |
break; | |
} | |
} | |
void keyReleased(){ | |
switch(key){ | |
case 'a': | |
rotateActive = true; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment