VarvaraToulkeridou-13-9-65

by varvara @ 8:34 am 24 January 2012
import java.util.ArrayList;
import java.util.Random;
 
import processing.core.*;
 
public class FNakeRemake extends PApplet {
	int xSubDiv = 7; //# of subdivisions horizontally
	int ySubDiv = 10; //# of subdivisions vertically
	int offset = 15; //range for offset to vary the regular subdivisions
	float circleStroke = 2; float gridLineStroke = (float) 1.50; float lineStroke = (float) 1.3;
 
	//arrays to store randomly generated breakpoints on the x,y axis
	ArrayList xBreakPoints;
	ArrayList yBreakPoints;
	//two-dimensional array to store the y coordinates of the grid
	float[][] grid = new float[ySubDiv+1][xSubDiv+1];
 
	public void setup() {
		size(700, 700);
		frameRate((float) 0.5);
	}
 
	public void draw() {
		background(255);
		//get randomly generated breakpoints for x and y axis of canvas
		xBreakPoints = subDivide(width, xSubDiv);
		yBreakPoints = subDivide(height, ySubDiv);
 
		//compute random points for the y coordinates for the interior points
		//of the grid; the x coordinates remain the same for every column of points
		for (int j = 0; j < yBreakPoints.size(); j++) {
			int y = yBreakPoints.get(j);
			int py;
			for (int i = 0; i < xBreakPoints.size(); i++) {
				strokeWeight(gridLineStroke);
				if (i == 0) {
					grid[j][i] = y;
				} else if (i != 0 && (j == 0 || j == yBreakPoints.size()-1)) {
					grid[j][i] = y;
					line(xBreakPoints.get(i-1),y,xBreakPoints.get(i),y);
				} else {
					py = y + (int)random(-offset,offset);
					grid[j][i] = py;
					line(xBreakPoints.get(i-1),y,xBreakPoints.get(i),py);
					y = py;
				}
			}
		}
 
		for (int j = 1; j < yBreakPoints.size(); j++) {
			for (int i = 1; i < xBreakPoints.size(); i++) {
				Random randomGenerator = new Random();
				//randomly pick if the grid cell will be empty or not
				boolean isEmpty = randomGenerator.nextBoolean();
				if (!isEmpty) {
					strokeWeight(lineStroke);
					//randomly pick if the grid cell will contain straight lines or not
					boolean areStraight = randomGenerator.nextBoolean();
					if (areStraight) {
						for (float k = 0; k < 1; k += 0.03){
							boolean drawLine = randomGenerator.nextBoolean();
							if (drawLine) {
								//for the upper limit line
								float x1 = lerp((float)xBreakPoints.get(i-1), (float)xBreakPoints.get(i), k);
								float y1 = lerp((float)grid[j-1][i-1], (float)grid[j-1][i], k);
								//for the lower limit line
								float x2 = x1;
								float y2 = lerp((float)grid[j][i-1], (float)grid[j][i], k);
								line(x1,y1,x2,y2);
							}
						}
					} else {
						for (float k = 0; k 9) {
					float randX = random(xBreakPoints.get(i-1), xBreakPoints.get(i));
					float randY = random(grid[j-1][i], grid[j][i]);
					float randDiam = random(30,100);
					noFill();
					strokeWeight(circleStroke);
					ellipse(randX, randY, randDiam, randDiam);
				}
			}
		}
	}
 
	/**
	 * Randomly subdivides a distance into parts; the subdivision occurs
	 * by varying (adding or subtracting from) the regular subdivision
	 * given an allowed range equal to the 1/3 of the regular subdivision.
	 *
	 * @param size the size of the distance to be subdivided in pixels
	 * @param subDiv the number of subdivisions
	 * @return an ArrayList containing the start point, the breakpoints
	 *         and the end point of the line
	 */
	public ArrayList subDivide(int size, int subDiv) {
		//an array to store the randomly generated breakpoints
		ArrayList breakPoints = new ArrayList();
 
		int defaultSubDiv = (int) (size / (float)subDiv);
		int tempThreshold = defaultSubDiv/3;
 
		//add the start point to the array
		breakPoints.add(0);
		//get randomly generated subdivisions given the defined threshold
		for (int i = 1; i < subDiv; i++) {
			int breakPoint = breakPoints.get(i-1) + defaultSubDiv + (int)random(-tempThreshold, tempThreshold);
			breakPoints.add(breakPoint);
		}
		//add the end point to the array
		breakPoints.add(size);
		return breakPoints;
	}
}

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2024 Interactive Art and Computational Design, Spring 2012 | powered by WordPress with Barecity