• fullscreen
  • Mondrian.pde
  • color black = color(0); 
    color white = color(255); 
    color Red = color(255,0,0); 
    color Yellow = color(255,255,0); 
    color Blue = color(0,0,255); 
    int linesLimit = 20; 
    int rectangleCount = 2; 
    float horizontalLines; 
    float verticalLines; 
    float removals = 10; 
     
    void setup() 
    { 
      size(768,512); 
      drawPainting(); 
    } 
     
    void drawPainting() 
    { 
      background(255); 
      stroke(black); 
      drawHorizontalLines(); 
      drawVerticalLines(); 
      drawFrame();
      deleteLines(); 
      fillRectangles(); 
      drawFrame();
    } 
     
    void draw() { 
    } 
     
    void mousePressed() 
    { 
      drawPainting(); 
    } 
     
    void drawHorizontalLines() 
    { 
        stroke(black); 
      horizontalLines = random(3,linesLimit); 
      for(int i=0; i<horizontalLines; i++) 
      { 
        float yPos=random(height); 
        strokeWeight(4); 
        line(0,yPos,width,yPos); 
      }  
    } 
     
    void drawVerticalLines() 
    { 
        stroke(black); 
      verticalLines = random(linesLimit); 
      for(int i=0; i<verticalLines; i++) 
      { 
        float xPos=random(width); 
        strokeWeight(4); 
        line(xPos,0,xPos,height); 
      }  
    } 
     
    void drawFrame() 
    { 
      stroke(black); 
      strokeWeight(10); 
      line(0,0,width,0); 
      line(0,0,0,height); 
      line(0,height,width,height); 
      line(width,0,width,height); 
    } 
     
    void deleteLines() 
    { 
      int counter = 0; 
      while(counter<removals) 
      { 
        int xPosition = (int)random(width); 
        int yPosition = (int)random(height); 
        if(get(xPosition,yPosition)==white) 
        { 
          counter++; 
          removeLineLeftOfRectangle(xPosition,yPosition); 
        } 
      } 
    } 
    void fillRectangles() 
    { 
      float rectangles = random(rectangleCount-1,rectangleCount); 
     
      int counter = 0; 
      while(counter<rectangles) 
      { 
        int xPosition = (int)random(width); 
        int yPosition = (int)random(height); 
        if(get(xPosition,yPosition)==white) 
        { 
          counter++; 
          fillSingleRectangle(xPosition,yPosition,Blue); 
        } 
      } 
      counter = 0; 
      while(counter<rectangles) 
      { 
        int xPosition = (int)random(width); 
        int yPosition = (int)random(height); 
        if(get(xPosition,yPosition)==white) 
        { 
          counter++; 
          fillSingleRectangle(xPosition,yPosition,Red); 
        } 
      }   
      counter = 0; 
      while(counter<rectangles) 
      { 
        int xPosition = (int)random(width); 
        int yPosition = (int)random(height); 
        if(get(xPosition,yPosition)==white) 
        { 
          counter++; 
          fillSingleRectangle(xPosition,yPosition,Yellow); 
        } 
      }   
    } 
     
    void fillSingleRectangle(int x, int y,color c) 
    { 
      int p=0; 
      while(get(x,y-p)==white) p++; 
      int q=0; 
      while(get(x-q,y)==white) q++; 
      int i=0; 
      while(get(x,y+i)==white) i++; 
      int j=0; 
      while(get(x+j,y)==white) j++; 
      noStroke(); 
      fill(c); 
      rectMode(CORNERS); 
      rect(x-q,y-p,x+j,y+i); 
    } 
     
    void removeLineLeftOfRectangle(int x, int y) 
    { 
      int p=0; 
      while(get(x,y-p)==white) p++; 
      int i=0; 
      while(get(x,y+i)==white) i++; 
      int j=0; 
      while(get(x+j,y)==white) j++;   
      stroke(white); 
      strokeCap(SQUARE); 
      line(x+j+3,y-p+1,x+j+3,y+i); 
    } 
     
     
     
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Bernhardt D. Engerer

    MondrianGenerator

    Add to Faves Me Likey@! 6
    You must login/register to add this sketch to your favorites.

    A simple and rather crude Piet Mondrian-style painting generator.

    Click to generate a new painting



    Note: going through the preview images before I uploaded, I'm not sure if I saw some Mondrian generators already. I apologize to any Processing artists who already posted something similar to this as I did not mean to copy the idea, even though I believe my code to be wholly original and without using any reference to other code.

    Actually this is the first time I saw a 'generator', and it looks great! Currently, a classroom from Turkey are working on those Mondrian sketches. I am sure they will notice your sketch, and I will also let them know about this!
    ps: (should there be some kind of maximum width limitation? sometimes boxes are too wide)
    Thanks for the comment!
    Yeah I realised that sometimes the border around the fpicture was being removed in places too, I'll upload an update as soon as possible with it fixed.
    updated, still some minor bugs though :(

    I'll try to iron them out later ;)
    Michael
    25 Apr 2009
    Funny, it makes me think about David Morgan-Mars "Piet programming language in which the programs look like abstract paintings.
    http://www.dangermouse.net/esoteric/piet.html
    Ribas
    21 Mar 2012
    great work! :-)
    You need to login/register to comment.