Grail
YaBB Newbies
Offline
Posts: 3
Mondrian painting program- Problems with pixel[]
Mar 20th , 2009, 1:06am
I am working on a few processing programs for a art show. Im very new to processing and have been researching code generated art for about 2 months now. The project i am currently working on is called 'Future Artist'. It will be a 'Piet Mondrian' random painting simulator. The paintings will resemble his compositions in red, yellow, and blue. for visual reference: http://en.wikipedia.org/wiki/File:Mondrian_CompRYB.jpg At the moment i have constructed a program that will create the horizontal and vertical black lines. these lines will continue to draw until they run off the canvas size. I have attempted to add some simple collision detection for the canvas sides; now I would like to add collision detection to the lines themselves. Ex: If a line being drawn intercepts another line, then that line will stop. I have attempted to check for this using the pixel[] array. if the increasing coordinate value's color is black then it will not draw, if this color is white, that means the space is empty and it may continue drawling. My first problem is that i run into a Index error, using pixel[y*width+x] this is commented out in the drawLine() method of the Line class. the error code is telling me i am trying to access a index of 160001, well my canvas is 400x400 which = 160000, so my guess is if the y variable generated 400 and was plugged in to the equation, what ever x is will throw us out of index. I obtained this math from: http://processing.org/reference/pixels.html y*width+x My second problem: When pixel[y*width+x] actually returns a value to me the numbers make no since, I thought they were RGB color values, so i assumed they would be in a range of 0-255, i and receiving values of -1 to -16777216. for the colors white and black respectively. My third problem is that on some occasions, lines that have not intersected will stop drawing. i have only attempted horizontal collisions, these if statements are commented out in the drawLine() method of the Line class here is the code, it will parse: int x, y; int xo ,yo; char dir; ArrayList lines = new ArrayList(); int lineCount; void setup(){ size(400, 400); colorMode(RGB); smooth(); frameRate(30); background(255); strokeCap(PROJECT); stroke(0); lineCount = int(random(1,15)); for (int i = 0; i < lineCount; i++) { y = int(random(1, height)); x = int(random(1, width)); switch(int(random(0,2))){ case 0: dir = 'H'; break; case 1: dir = 'V'; break; } // Line (int x1, int y1, int x2, int y2, int wweight, char HV) lines.add(new Line(x,y,int(random(2,15)),dir)); //Line L = (Line) lines.get(i);// to cast the object coming out } } void draw(){ background(255); for (int i = 0; i < lines.size(); i++) { Line L = (Line) lines.get(i);// to cast the object coming out L.drawLine(L.direction); } } class Line { int weight; int x; int y; int xo; int yo; int xx; int yy; int speedX; int speedY; int speedXX; int speedYY; char direction; Line (int x1, int y1, int wweight, char HV) { x = x1; xo = x1; xx = x1; y = y1; yo = y1; yy = y1; weight = wweight; direction = HV; speedX = int(random(1,4)); speedY = int(random(1,4)); speedXX = int(random(1,4)); speedYY = int(random(1,4)); } void drawLine(char dire) { stroke(0); strokeWeight(weight); x+=speedX; y+=speedY; xx-=speedXX; yy-=speedYY; loadPixels(); switch(dire) { case 'H': line(x, yo, xo, yo); //horizontal right line(xo, yo, xx, yo); // horizontal left if (x > width){ speedX*=0; } else if (x < 0){ speedX*=0; } else if (xx > width){ speedXX*=0; } else if (xx == 0){ speedXX*=0; } if (pixels[yo*width+x] == -16777216){ speedX*=0; } int index = int(yo*width+xx); if(index > 160000) { index = 160000; } if (pixels[index] == -16777216){ speedXX*=0; } break; case 'V': line(xo, yy, xo, yo); //vertical up line(xo, yo, xo, y); //vertical down if (y > height){ speedY*=0; } else if (y < 0){ speedY *=0; } else if (yy > height){ speedYY*=0; } else if (yy < 0){ speedYY*=0; } break; } println("x = " +x+ " yo = " +yo+ " xo = "+xo); println(yo*width+x); /* else if(pixels[yo*width+xx] == -16777216){ speedXX*=0; } if (pixels[yo*width+x] == -16777216){ speedX *=0; }*/ } } // box structure for later. class Box { Box(){ } void drawBox(){ } } //Thank you for you time reading this and any possible help you can give!