import processing.core.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; import java.util.regex.*; public class Reflection1 extends PApplet { /** * Non-orthogonal Reflection * by Ira Greenberg. * * Based on the equation (R = 2N(N*L)-L) where R is the * reflection vector, N is the normal, and L is the incident * vector. */ float baseX1, baseY1, baseX2, baseY2; float baseLength; float[] xCoords, yCoords; float ellipseX, ellipseY, ellipseRadius = 6; float directionX, directionY; float ellipseSpeed = 3.5f; float velocityX, velocityY; public void setup(){ size(640, 240); frameRate(30); fill(128); smooth(); baseX1 = 0; baseY1 = height-150; baseX2 = width; baseY2 = height; // start ellipse at middle top of screen ellipseX = width/2; // calculate initial random direction directionX = random(0.1f, 0.99f); directionY = random(0.1f, 0.99f); // normalize direction vector float directionVectLength = sqrt(directionX*directionX + directionY*directionY); directionX /= directionVectLength; directionY /= directionVectLength; } public void draw(){ // draw background fill(0, 12); noStroke(); rect(0, 0, width, height); // calculate length of base top baseLength = dist(baseX1, baseY1, baseX2, baseY2); xCoords = new float[ceil(baseLength)]; yCoords = new float[ceil(baseLength)]; // fill base top coordinate array for (int i=0; i width-ellipseRadius){ ellipseX = width-ellipseRadius; directionX *= -1; } // left if (ellipseX < ellipseRadius){ ellipseX = ellipseRadius; directionX *= -1; } // top if (ellipseY < ellipseRadius){ ellipseY = ellipseRadius; directionY *= -1; // randomize base top baseY1 = random(height-100, height); baseY2 = random(height-100, height); } } static public void main(String args[]) { PApplet.main(new String[] { "Reflection1" }); } }