Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int xSpeed = 5, ySpeed = 3;
- int amountOfLines = 100;
- int []speedXArr = new int[2];
- int []speedYArr = new int [2];
- color []lineColorArr = new color [amountOfLines];
- int []startLineXArr = new int [amountOfLines];
- int []startLineYArr = new int [amountOfLines];
- int []endLineXArr = new int [amountOfLines];
- int []endLineYArr = new int [amountOfLines];
- void setup()
- {
- size(842, 480);
- speedXArr[0]=xSpeed;
- speedYArr[0]=ySpeed;
- speedXArr[1]=xSpeed;
- speedYArr[1]=ySpeed;
- startLineXArr[0]=(int)random(width+1);
- startLineYArr[0]=(int)random(height+1);
- endLineXArr[0]=(int)random(width+1);
- endLineYArr[0]=(int)random(height+1);
- float angle = (float)millis()/1000.0;
- lineColorArr[0] = color(sin(angle) * 255,
- sin(angle + 6.28 / 3) * 255,
- sin(angle + 6.28 / 3 * 2) * 255 );
- }
- void draw()
- {
- float alpha;
- clear();
- background(0);
- addPosition();
- // Draw all lines
- for (int i = 0; i < amountOfLines; i++) {
- // alpha will define the transparancy.
- // The number of steps between 255 and 0 will be inline with number of elements in the array.
- alpha = map(i, 0, amountOfLines-1, 255, 0);
- stroke(lineColorArr[i], alpha);
- line(startLineXArr[i], startLineYArr[i], endLineXArr[i], endLineYArr[i]);
- }
- }
- void addPosition() {
- // Shift the array backwards, so the next line
- // can be added to the array on index position 0
- for (int i = amountOfLines-1; i > 0; i--) {
- startLineXArr[i] = startLineXArr[i-1];
- startLineYArr[i] = startLineYArr[i-1];
- endLineXArr[i] = endLineXArr[i-1];
- endLineYArr[i] = endLineYArr[i-1];
- lineColorArr[i] = lineColorArr[i-1];
- }
- // Create new line color
- float angle = (float)millis()/1000.0;
- lineColorArr[0] = color(sin(angle) * 255,
- sin(angle + 6.28 / 3) * 255,
- sin(angle + 6.28 / 3 * 2) * 255 );
- // Create new line points
- // Check if you will go over the borders and change direction if so
- // New start point
- if (startLineXArr[1]+speedXArr[0] > width || startLineXArr[1]+speedXArr[0] < 0) {
- speedXArr[0] *= -1;
- }
- startLineXArr[0] = startLineXArr[1]+speedXArr[0];
- if (startLineYArr[1]+speedYArr[0] > height || startLineYArr[1]+speedYArr[0] < 0) {
- speedYArr[0] *= -1;
- }
- startLineYArr[0] = startLineYArr[1]+speedYArr[0];
- // New end point
- if (endLineXArr[1]+speedXArr[1] > width || endLineXArr[1]+speedXArr[1] < 0) {
- speedXArr[1] *= -1;
- }
- endLineXArr[0] = endLineXArr[1]+speedXArr[1];
- if (endLineYArr[1]+speedYArr[1] > height || endLineYArr[1]+speedYArr[1] < 0) {
- speedYArr[1] *= -1;
- }
- endLineYArr[0] = endLineYArr[1]+speedYArr[1];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement