Created
January 3, 2012 08:02
-
-
Save bmander/1554003 to your computer and use it in GitHub Desktop.
draw the universe
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
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