xxxxxxxxxx
58
// initial coordinates for point of refraction
var x = 180;
var y = 148;
// coordinates for rainbow exit
var xr, yr;
// coordinates of the verteces of the triangle
var xb1 = 150;
var yb1 = 200;
var xb2 = 200;
var yb2 = 113;
var xb3 = 250;
var yb3 = 200;
// color array for rainbow colors
var c = [];
var d;
function setup() {
createCanvas(400, 400);
c = ['red','red','orange','orange','yellow','yellow','yellow','green','green','blue','blue','blue','purple','purple'];
d = color(random(50,255),random(50,255),random(50,255),random(100,255));
}
function draw() {
background(0);
// draw triangle
stroke(d);
strokeWeight(5);
fill(0,0,0,100);
triangle(xb1,yb1,xb3,yb3,xb2,yb2);
// draw light ray
stroke(255,255,255,230);
strokeWeight(2);
if (mouseX > xb1 && mouseX < xb2 && mouseY < yb1 && mouseY > yb2) {
// change point of refraction
y = mouseY;
x = (y-yb1)*(xb2-xb1)/(yb2-yb1)+xb1;
};
line(0,200,x,y);
// draw light refracting inside prism
for(var i = 0; i < 14; i++) {
if (mouseX == 0 && mouseY == 0) {
yr = y-6;
print("I'm off the screen");
}
if (mouseX > xb1 && mouseX < xb2 && mouseY < yb1-5 && mouseY > yb2+5) {
// change point of refraction
yr = mouseY-6;
}
else {
};
xr = (yr+i-yb2)*(xb2-xb3)/(yb2-yb3)+xb2;
strokeWeight(.3);
line(x,y,xr,yr+i);
strokeWeight(2);
stroke(c[(i+mouseY)%14]);
line(xr,yr+i,width,yr+20+i*1.5);
}
}