SOLVED Draw_Nodes Export to PDF has too many duplicated paths

Hi everyone,

I am pretty new to processing and have haphazardly made my way through some code to get a graphic output for a poster I am making. The code comes from multiple sources so I am sure there is a lot of unnecessary stuff in there based on what I actually need.

The code allows you to draw and then prints to a PDF which I was hoping would allow me to use the art as a vector for these posters. Unfortunately the vectors produced have about half a million duplicate paths that slow down illustrator to the point of crashing. I have tried multiple ways to simplify the file once produced but to no avail. Hoping someone here can help me to adapt the code to draw single paths instead of (what I assume it is doing) drawing multiple on top of each other?

This a sample of the sort of image Iā€™m producing:


import processing.pdf.*;

ArrayList<Node> nodes;
float maxDistance = 90;
float dx = 60;
float dy = 60;
float maxNeighbors = 9;

Boolean drawMode = true;

void setup()
{
  size(900,500);
  beginRecord(PDF, "everything.pdf");
  background(255);
  nodes = new ArrayList<Node>();
}

void draw()
{
  //background(255);
  
  if(drawMode)
  {
    if(mousePressed){
      addNewNode(mouseX,mouseY,random(-dx,dx),random(-dx,dx));
    }
  } else
  {
    addNewNode(random(width),random(height),0,0);
  }
  

  
  for(int i = 0; i < nodes.size(); i++){
    Node currentNode = nodes.get(i);
    for(int j=0; j<currentNode.neighbors.size(); j++)
    {
      Node neighborNode = currentNode.neighbors.get(j);
      float lineColor = currentNode.calculateLineColor(neighborNode,maxDistance);
      stroke(lineColor, lineColor, lineColor);
      strokeWeight(.1);
      line(currentNode.x,currentNode.y,neighborNode.x,neighborNode.y);
    }
    currentNode.display();
  }
}

void addNewNode(float xPos, float yPos, float dx, float dy)
{
  //println("add new node");
  //generates a random location within a 50x50px box around the mouse
  //float xPos = mouseX + random(-50,50);
  //float yPos = mouseY + random(-50,50);
  //adds a node at this location
  Node node = new Node(xPos+dx,yPos+dy);
  
  node.setNumNeighbors( countNumNeighbors(node,maxDistance) );
  
  //println("newly added node has " + node.numNeighbors + " neighbors");
  //println("and neighbors.size() = " + node.neighbors.size());
  
  if(node.numNeighbors < maxNeighbors){
    nodes.add(node);
    for(int i=0; i<nodes.size(); i++)
    {
      Node currentNode = nodes.get(i);
      currentNode.setNumNeighbors( countNumNeighbors(currentNode,maxDistance) );
    }
  }
  
}

int countNumNeighbors(Node nodeA, float maxNeighborDistance)
{
  int numNeighbors = 0;
  nodeA.clearNeighbors();
  
  for(int i = 0; i < nodes.size(); i++)
  {
    Node nodeB = nodes.get(i);
    float distance = sqrt((nodeA.x-nodeB.x)*(nodeA.x-nodeB.x) + (nodeA.y-nodeB.y)*(nodeA.y-nodeB.y));
    if(distance < maxNeighborDistance)
    {
      numNeighbors++;
      nodeA.addNeighbor(nodeB);
    }
  }
  return numNeighbors;
}

void keyPressed()
{ if (key=='Q'){
    endRecord();
    exit();}
else {
  
  drawMode = !drawMode;
  nodes = new ArrayList<Node>();
}
}
class Node
{
  float x;
  float y;
  int numNeighbors;
  ArrayList<Node> neighbors;
  float lineColor;
  float nodeWidth = 1;
  float nodeHeight = 1;
  float fillColor = 0;
  float lineColorRange = 0;
  
  

  Node(float xPos, float yPos)
  {
    x = xPos;
    y = yPos;
    numNeighbors = 0;
    neighbors = new ArrayList<Node>();
  }
  
  void display()
  {
    noStroke();
    fill(fillColor);
    ellipse(x,y,nodeWidth,nodeHeight);
  }
  
  void addNeighbor(Node node)
  {
    neighbors.add(node);
  }
  
  void setNumNeighbors(int num)
  {
    numNeighbors = num;
  }
  
  void clearNeighbors()
  {
    neighbors = new ArrayList<Node>();
  }
  
  float calculateLineColor(Node neighborNode, float maxDistance)
  {
    float distance = sqrt((x-neighborNode.x)*(x-neighborNode.x) + (y-neighborNode.y)*(y-neighborNode.y));
    lineColor = (distance/maxDistance)*lineColorRange;
    return lineColor;
  }
    
}

Thanks!

1 Like