-
-
Save jkwok91/248b404f498e766f6aeb to your computer and use it in GitHub Desktop.
practice with the map() fn
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
/* | |
inspired by this: | |
http://dvdp.tumblr.com/post/35345895496/121109 | |
*/ | |
DotOnLine[] dots; | |
int NUM_DOTS, RAD; | |
float angle; | |
void setup() { | |
size(300, 300); | |
RAD = 150; | |
NUM_DOTS = width*2; | |
dots = new DotOnLine[NUM_DOTS]; | |
for (int i = 0; i < NUM_DOTS; i++) { | |
dots[i] = new DotOnLine(random(0, TWO_PI)); | |
} | |
} | |
void draw() { | |
background(0); | |
for (DotOnLine d : dots) { | |
d.oscillate(); | |
} | |
angle = (angle+0.1)%TWO_PI; | |
} | |
class DotOnLine { | |
PVector start, end; | |
float slope, ycoeff; | |
float x, y; | |
float offset; | |
color c; | |
DotOnLine(float theta) { | |
float cosine = cos(theta); | |
float sine = sin(theta); | |
float p0 = random(0, RAD/2); | |
float p1 = random(p0+20, RAD); | |
start = new PVector(p0*cosine, p0*sine); | |
end = new PVector(p1*cosine, p1*sine); | |
float dx = end.x-start.x; | |
float dy = end.y-start.y; | |
slope = dy/dx; | |
ycoeff = start.y - slope*start.x; | |
offset = map(p0, 0, RAD, 0, PI); | |
c = getColor(random(RAD)); | |
} | |
void oscillate() { | |
x = sin(angle+offset); | |
x = map(x, -1, 1, start.x, end.x); | |
y = x*slope+ycoeff; | |
display(); | |
} | |
void display() { | |
pushMatrix(); | |
translate(width/2, height/2); | |
stroke(c); | |
noFill(); | |
ellipse(x, y, map(x,start.x,end.x,0.1,2), map(y,start.y,end.y,0.1,2)); | |
popMatrix(); | |
} | |
} | |
/* | |
resources: | |
http://krazydad.com/tutorials/makecolors.php | |
*/ | |
color getColor(float i) { | |
int center = 200; | |
int widthh = 100; | |
double frequency = .3; | |
int red = (int)Math.ceil(Math.sin(frequency*i+0) * widthh + center); | |
int green = (int)Math.ceil(Math.sin(frequency*i+2) * widthh + center); | |
int blue = (int)Math.ceil(Math.sin(frequency*i+4) * widthh + center); | |
return color(red,green,blue); | |
} |
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
/* | |
things in a ring expanding in and out | |
*/ | |
Dot[] dots; | |
int NUM_DOTS, RAD; | |
void setup() { | |
size(300, 300); | |
background(0); | |
stroke(255); | |
noFill(); | |
RAD = width/3; | |
NUM_DOTS = width*2; | |
dots = new Dot[NUM_DOTS]; | |
for (int i = 0; i < NUM_DOTS; i++) { | |
dots[i] = new Dot(random(0, TWO_PI)); | |
} | |
} | |
void draw() { | |
background(0); | |
for (Dot d : dots) { | |
d.move(); | |
d.display(); | |
} | |
} | |
class Dot { | |
PVector original, location, velocity; | |
Dot(float theta) { | |
float cosine = cos(theta); | |
float sine = sin(theta); | |
float p0 = random(0, RAD/2); | |
original = new PVector(p0*cosine, p0*sine); | |
location = original.get(); | |
velocity = original.normalize(null); //this syntax is a little odd but w/e the docs tell it like it is | |
} | |
void move() { | |
location.add(velocity); | |
float distance = dist(location.x, location.y, 0, 0); | |
if (distance > RAD || distance < original.mag()) { | |
velocity.mult(-1); | |
} | |
} | |
void display() { | |
pushMatrix(); | |
translate(width/2, height/2); | |
ellipse(location.x, location.y, map(location.x, original.x, RAD, 0.1, 2), map(location.y, original.y, RAD, 0.1, 2)); | |
popMatrix(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment