Skip to content

Instantly share code, notes, and snippets.

@bmander
Created January 3, 2012 08:02
Show Gist options
  • Save bmander/1554003 to your computer and use it in GitHub Desktop.
Save bmander/1554003 to your computer and use it in GitHub Desktop.
draw the universe
import java.awt.event.*;
class Path {
ArrayList points;
Path() {
points = new ArrayList();
}
void add(float x, float y) {
this.points.add( new PVector( x, y ) );
}
void draw() {
beginShape();
for(int i=0; i<this.points.size(); i++) {
PVector pt = (PVector)this.points.get(i);
vertex( pt.x, pt.y );
}
endShape();
}
}
float xtrans=0;
float ytrans=0;
float xscale=1;
float yscale=1;
ArrayList paths = new ArrayList();
Path path = null;
class Person {
float x;
float y;
float width=0.5; //meters
}
class Button {
int x;
int y;
int width;
int height;
}
class MouseWheelInput implements MouseWheelListener{
void mouseWheelMoved(MouseWheelEvent e) {
int step=e.getWheelRotation();
xscale = xscale*pow(1.1,step);
yscale = yscale*pow(1.1,step);
println( xscale );
}
}
float screenX() {
return (mouseX-(width/2))/xscale - xtrans;
}
float screenY() {
return (mouseY-(height/2))/yscale - ytrans;
}
void setup() {
size(1000,1000);
background(255);
smooth();
strokeWeight(0.5);
frame.addMouseWheelListener(new MouseWheelInput());
}
void draw() {
translate(width/2,height/2);
scale(xscale, yscale);
pushMatrix();
translate(xtrans,ytrans);
background(255);
noFill();
stroke(255,0,0);
if( path != null )
path.draw();
stroke(0);
for(int i=0; i<paths.size(); i++) {
((Path)paths.get(i)).draw();
}
line(-10,0,10,0);
line(0,-10,0,10);
popMatrix();
}
void mousePressed() {
if( mouseButton==37 ) {
path = new Path();
path.add( screenX(), screenY() );
}
}
void mouseReleased() {
if( mouseButton==37 ){
paths.add( path );
path=null;
println( screenX() );
}
}
void mouseDragged() {
println( mouseButton );
if( mouseButton==37 ) {
path.add( screenX(), screenY() );
} else if( mouseButton==39 ) {
xtrans += (mouseX-pmouseX)/xscale;
ytrans += (mouseY-pmouseY)/yscale;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment