Skip to content

Instantly share code, notes, and snippets.

@jkwok91

jkwok91/Star.pde Secret

Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/bc421099ddfe7ed20415 to your computer and use it in GitHub Desktop.
Save jkwok91/bc421099ddfe7ed20415 to your computer and use it in GitHub Desktop.
requested by misha. quick sketch
class Point {
int xcor, ycor;
Point(int x, int y) {
xcor = x;
ycor = y;
}
String toString() {
return "("+xcor+","+ycor+")";
}
}
class Star {
Point center;
int xrad;
int yrad;
Point top;
Point right;
Point bottom;
Point left;
Star(Point c, int radius) {
center = c;
xrad = radius;
yrad = radius;
top = new Point(center.xcor, center.ycor-yrad);
bottom = new Point(center.xcor, center.ycor+yrad);
left = new Point(center.xcor-xrad, center.ycor);
right = new Point(center.xcor+xrad, center.ycor);
}
Star(Point c, int xr, int yr) {
center = c;
xrad = xr;
yrad = yr;
top = new Point(center.xcor, center.ycor-yrad);
bottom = new Point(center.xcor, center.ycor+yrad);
left = new Point(center.xcor-xrad, center.ycor);
right = new Point(center.xcor+xrad, center.ycor);
}
void drawS() {
drawArc(center, top, right);
drawArc(center, bottom, right);
drawArc(center, left, bottom);
drawArc(center, left, top);
}
String toString() {
return "xrad:"+xrad+", yrad:"+yrad+"centered at:"+center;
}
void drawArc(Point center, Point p1, Point p2) {
int c1x, c1y, c2x, c2y;
int h = (p1.xcor == center.xcor) ? (p2.xcor) : (p1.xcor);
int k = (p1.ycor == center.ycor) ? (p2.ycor) : (p1.ycor);
int DY = (k < center.ycor) ? -3*xrad : 3*xrad;
if (h > center.xcor) {
c1x = center.xcor;
c1y = k+DY;
c2x = h+(3*yrad);
c2y = center.ycor;
} else {
c1x = h+(-3*yrad);
c1y = center.ycor;
c2x = center.xcor;
c2y = k+DY;
}
stroke(255);
curve(c1x, c1y, p1.xcor, p1.ycor, p2.xcor, p2.ycor, c2x, c2y);
}
}
/*
a night sky of twinkling stars
*/
int w = 200;
int h = 200;
color c = color(255);
ArrayList<Star> galaxy = new ArrayList<Star>();
int numStars = (int)(Math.random()*w+(w/2));
void setup() {
size(w,h);
background(0);
for (int i = 0; i < numStars; i++) {
//gneerate a random point to make a star and push into galaxy
/*
http://stackoverflow.com/questions/7922978/random-numbers-with-math-random-in-java
always need a resource when dealing with random nums because i'm stupid
*/
//random yrad
int yrad = (int)(Math.random()*5+5);
//random xrad. because i don't want stupid looking stars, im restricting this to be smaller than the y
int xrad = (int)(Math.random()*yrad+1);
//random xcor and ycor of central pt
int x = (int)(Math.random()*(w-2*xrad)+xrad);
int y = (int)(Math.random()*(w-2*yrad)+yrad);
Star s = new Star(new Point(x,y), xrad, yrad);
galaxy.add(s);
}
}
void draw() {
background(0);
//now we make them twinkle
int mod = (int)(Math.random()*(galaxy.size()/7)+1);
for (int i = 0; i < galaxy.size(); i++) {
if (i%mod == 0) {
Star current = galaxy.get(i);
current.drawS();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment