Skip to content

Instantly share code, notes, and snippets.

@coreygo
Created February 7, 2016 07:08
Show Gist options
  • Save coreygo/f05904d5e068adb4de18 to your computer and use it in GitHub Desktop.
Save coreygo/f05904d5e068adb4de18 to your computer and use it in GitHub Desktop.
// Zoom and pan a cube of 3D points
// Scale
float zoom;
// offset from center
PVector offset;
// previous offset
PVector poffset;
// mouse position
PVector mouse;
void setup() {
size(1200, 1200, P3D);
//smooth(4);
frameRate(60);
//hint(DISABLE_OPTIMIZED_STROKE);
//colorMode(HSB, 360, 360, 360);
offset = new PVector(0, 0);
poffset = new PVector(0, 0);
zoom = 10.0;
}
void draw() {
background(0);
pushMatrix();
translate(width/2, height/2);
scale(zoom);
translate(offset.x/zoom, offset.y/zoom);
// Draw everything relative to center and scale for 2D "zoom"
// Offset and scale to zoom
// Now for the circles!
//drawCircles(width/2, height/2, 2400);
drawPoints();
popMatrix();
//saveFrame("frames/cir-######.png");
surface.setTitle(zoom + " zoom");
}
// Zoom in and out when the key is pressed
void keyPressed() {
if (key == 'e') {
zoom += 1.0;
} else if (key == 'q') {
zoom -= 1.0;
}
zoom = constrain(zoom, -100, 100);
}
// Store the mouse and the previous offset
void mousePressed() {
mouse = new PVector(mouseX, mouseY);
poffset.set(offset);
}
void mouseWheel(MouseEvent event) {
float e = event.getCount();
zoom = constrain(zoom+(e/10), -1000, 1000);
}
// Calculate the new offset based on change in mouse vs. previous offsey
void mouseDragged() {
offset.x = mouseX - mouse.x + poffset.x;
offset.y = mouseY - mouse.y + poffset.y;
}
void drawCircles(float x, float y, float radius) {
stroke(255);
strokeWeight(1);
noFill();
ellipse(x, y, radius, radius);
// try adjusting radius 2...12
if (radius > 24) {
drawCircles(x + radius/2, y, radius/2);
drawCircles(x - radius/2, y, radius/2);
drawCircles(x, y + radius/2, radius/2);
// uncomment for added fun
drawCircles(x, y - radius/2, radius/2);
}
}
void drawPoints() {
for (int x=-20; x<20; x++) {
for (int y=-20; y<20; y++) {
for (int z=-20; z<20; z++) {
stroke(255);
strokeWeight(random(1, 5));
point(x, y, z);
}
}
}
}
void sphere() {
lights();
translate(offset.x, offset.y);
fill(255/2);
stroke(255/2);
sphereDetail(10);
sphere(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment