voronoi blur and export

Hi, i'm still learning Processing and what i'm trying to achieve is a tool for generating posters similar to Andrea Gysin's Puddle Builder (http://ertdfgcvb.com/puddle-builder-13/). First of all i start with a manhattan distance voronoi code:

  import processing.pdf.*;  
  class Cell
  {
    PVector loc;
    color c;

    Cell(int _x, int _y, color _c )
    {
      loc = new PVector(_x, _y);
      c = _c;
    }
  }

  ArrayList<Cell> cells = new ArrayList<Cell>();
  boolean use_manhattan = false;


  void setup()
  {
    noLoop();

    size(600, 800);
    beginRecord(PDF, "data/test.pdf");     

    for (int i = 0; i < 10; ++i) {
      if (i<10)
        cells.add(new Cell((int)random(0, 500), (int)random(1, 100), color(random(0, 255))));
      if (i<5)
        cells.add(new Cell((int)random(0, 700), (int)random(600, 800), color(random(0, 255))));
    }
    // else
    // cells.add(new Cell((int)random(0, 250), (int)random(250, 400), color(255, 0, 0)));
  } 

  void draw() {



    cells.get(0).loc.x = mouseX;
    cells.get(0).loc.y = mouseY;
    //background(0);

    // Draw the voronoi cells
    drawVoronoi();

    // Draw the points

    for (int i = 0; i < cells.size(); ++i) {
      ellipse(cells.get(i).loc.x, cells.get(i).loc.y, 5, 5);

      fill(0);
      stroke(0);



      if (key == '3')  

        cells.add(new Cell(mouseX, mouseY, color(random(0, 255))));
    }
  }



  void drawVoronoi()
  {

    loadPixels();
    int offset = 0;

    // Iterate over every pixel
    for (int y = 0; y < height; y++)
    {
      for (int x = 0; x < width; x++)
      {
        float shortest = 1E12;
        int index = -1;

        // Find the closest cell
        for (int i = 0; i < cells.size(); ++i)
        {
          Cell cc = cells.get(i);
          float d;


          if (use_manhattan)
          {
            // eucl. Distance
            d = sq(x-cc.loc.x) + sq(y-cc.loc.y);
          } else
          {
            // man. distance
            d = abs(x - cc.loc.x) + abs(y - cc.loc.y);
          }

          if (d < shortest)
          {
            shortest = d;
            index = i;
          }
        }
        // pixel to the cells color
        pixels[offset++] = cells.get(index).c;
      }
    }
    updatePixels();

      endRecord();
      print("did it work?");
      exit();    
  }

  void keyPressed()
  {
    if (key == '1') {
      cells.add(new Cell(mouseX, mouseY, color(random(0, 255))));
    }
    if (key == ' ') {
      cells.subList(5, cells.size()).clear();
    }
    if (key == '2') {
      use_manhattan = !use_manhattan;
    }
    if (key =='q') {
      pushMatrix();

      endRecord();
      print("did it work?");
      exit();    
      popMatrix();
    }
  }

I would like to obtain a pdf similar to: manhattan-distance

but i don't understand how to blur the shapes and neither why pdf export just saves the ellipses i created and not the voronoi's shapes. Could you help me with the code? i will then later play with the controlP5 to change the stroke and blur manually.

Thanks! Giacomo

Answers

  • any help?

  • So this is my attempt using @GoToLoop's previous post: https://forum.processing.org/two/discussion/comment/71143/#Comment_71143

    Kf

    import processing.pdf.PGraphicsPDF;
    
    static final String PDF_NAME = "frame-";
    static final short ZEROS = 6;
    static final float BOLD = 1.5, FPS = 100;
    static final color STROKE = 0;
    
    
    PGraphics pdf;
    PImage canvas;
    
    ArrayList<Cell> cells = new ArrayList<Cell>();
    boolean use_manhattan = false;
    
    
    void setup() {
      //noLoop();
      size(600, 800);
      beginRecord(PDF, "data/test.pdf");     
    
    
      for (int i = 0; i < 10; ++i) {
        if (i<10)
          cells.add(new Cell((int)random(0, 500), (int)random(1, 100), color(random(0, 255))));
        if (i<5)
          cells.add(new Cell((int)random(0, 700), (int)random(600, 800), color(random(0, 255))));
      }
    
    
      smooth();
      frameRate(FPS);
    
      stroke(STROKE);
      strokeWeight(BOLD);
    
      pdf = createGraphics(width, height, PDF);
      canvas = createImage(width, height, RGB);
    }
    
    void draw() {
      line(pmouseX, pmouseY, mouseX, mouseY);
      frame.setTitle("Frame: " + frameCount);
      drawNow();
    }
    
    void mousePressed() {
      if (mouseButton == CENTER)  pdfScreenshot();
      else                        background((color) random(#000000));
    }
    
    void keyPressed() {
      int k = keyCode;
      if (k == ' ' | k == ENTER | k == RETURN)  pdfScreenshot();
    }
    
    File getPdfPath() {
      return dataFile(PDF_NAME + nf(frameCount, ZEROS) + ".pdf");
    }
    
    void pdfScreenshot() {
      File pdfFile = getPdfPath();
      if (!pdfFile.canWrite())  pdfFile.getParentFile().mkdir();
    
      pdf.setPath(pdfFile.getPath());
      println(pdfFile);
    
      loadPixels();
      arrayCopy(pixels, canvas.pixels);
      canvas.updatePixels();
    
      pdf.beginDraw();
      pdf.image(canvas, 0, 0);
      pdf.dispose();
      pdf.endDraw();
    }
    
    
    
    void drawNow() {
      //background(0);
      cells.get(0).loc.x = mouseX;
      cells.get(0).loc.y = mouseY;
    
    
      // Draw the voronoi cells
      drawVoronoi();
    
      // Draw the points
    
      for (int i = 0; i < cells.size(); ++i) {
        ellipse(cells.get(i).loc.x, cells.get(i).loc.y, 5, 5);
    
        fill(0);
        stroke(0);
        if (key == '3' && mousePressed)  
          cells.add(new Cell(mouseX, mouseY, color(random(0, 255))));
      }
    }
    
    //void keyPressed()
    //{
    //  if (key == '1') {
    //    cells.add(new Cell(mouseX, mouseY, color(random(0, 255))));
    //  }
    //  if (key == ' ') {
    //    cells.subList(5, cells.size()).clear();
    //  }
    //  if (key == '2') {
    //    use_manhattan = !use_manhattan;
    //  }
    //  if (key =='q') {
    //    //pushMatrix();
    
    //    endRecord();
    //    print("did it work?");
    //    exit();    
    //    //popMatrix();
    //  }
    //}
    
    
    
    
    void drawVoronoi()
    {
    
      loadPixels();
      int offset = 0;
    
      // Iterate over every pixel
      for (int y = 0; y < height; y++)
      {
        for (int x = 0; x < width; x++)
        {
          float shortest = 1E12;
          int index = -1;
    
          // Find the closest cell
          for (int i = 0; i < cells.size(); ++i)
          {
            Cell cc = cells.get(i);
            float d;
    
    
            if (use_manhattan)
            {
              // eucl. Distance
              d = sq(x-cc.loc.x) + sq(y-cc.loc.y);
            } else
            {
              // man. distance
              d = abs(x - cc.loc.x) + abs(y - cc.loc.y);
            }
    
            if (d < shortest)
            {
              shortest = d;
              index = i;
            }
          }
          // pixel to the cells color
          pixels[offset++] = cells.get(index).c;
        }
      }
      updatePixels();
    
      //endRecord();
      //print("did it work?");
      //exit();
    }
    
    
    
    
    
    class Cell
    {
      PVector loc;
      color c;
    
      Cell(int _x, int _y, color _c )
      {
        loc = new PVector(_x, _y);
        c = _c;
      }
    }
    
  • edited July 2017

    Thanks for the help, but is there a way to have a vector .pdf with this method?

  • is there a way to have a vector .pdf with this method?

    pixel operations and vector pdfs are pretty much incompatible. stick to lines and shapes and you'll be ok.

  • okay so, i've come to this point https://github.com/sinanatra/voronoigrid but i would like not to use the beginRaw() method but instead the beginRecord() so the circles will not be made of triangles, how could i do it? if i use it now i lose all the voronoi's polygons. Thanks for your help

Sign In or Register to comment.