Last active
December 17, 2015 00:18
-
-
Save dreger/5519425 to your computer and use it in GitHub Desktop.
Generate random graphs with Processing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GraphMaker | |
// By Kyle Dreger @dreger | |
// Date: 4 May 2013 | |
void setup() { | |
// Color of graph | |
boolean lightGraph = true; | |
boolean crazyColors = false; | |
// Set up | |
int windowSize = 700; | |
int windowWidth = 800; | |
int windowHeight = 600; | |
int padding = 20; | |
int nodeSize = 10; | |
int numNodes = 250; | |
int maxLinesPerNode = 7; | |
float maxLineDistance = .1*windowSize; | |
color background, nodeColor, lineColor; | |
if(lightGraph) { | |
nodeColor = color(#aaaaaa); | |
nodeColor = color(#aaaaaa,100); | |
lineColor = color(#dedede); | |
background = color(255,255,255); | |
} else { | |
nodeColor = color(#aaaaaa); | |
nodeColor = color(#00aced,100); | |
lineColor = color(#333333); | |
background = color(#111111); | |
} | |
size(windowWidth, windowHeight); | |
background(background); | |
ArrayList nodes = new ArrayList(); | |
for(int x=0; x<numNodes; x++) { | |
float randOver; | |
float randDown; | |
Node n; | |
randOver = random(padding, windowWidth-padding); | |
randDown = random(padding, windowHeight-padding); | |
System.out.println(); | |
if(crazyColors) { | |
color tempColor = color(random(0,255),random(0,255),random(0,255),100); | |
n = new Node(randOver, randDown, nodeSize, tempColor); | |
} else { | |
n = new Node(randOver, randDown, nodeSize, nodeColor); | |
} | |
nodes.add(n); | |
} | |
// Make sure nodes prefer close nodes to connect with | |
for(int x=0; x<nodes.size(); x++) { | |
Node start = (Node) nodes.get(x); | |
ArrayList closeNodes = new ArrayList(); | |
for(int k=0; k<nodes.size(); k++) { | |
// Only draw lines between close nodes | |
Node end = (Node) nodes.get(k); | |
float xDifference = abs(start.getX() - end.getX()); | |
float yDifference = abs(start.getY() - end.getY()); | |
if(xDifference < maxLineDistance && yDifference < maxLineDistance) { | |
closeNodes.add(end); | |
//System.out.println(xDifference+", "+yDifference); | |
} | |
} | |
for(int k=0; k<closeNodes.size(); k++) { | |
Node end = (Node) closeNodes.get(k); | |
if(end.getConnectedCount() < maxLinesPerNode | |
&& start.getConnectedCount() < maxLinesPerNode) { | |
stroke(lineColor); | |
line(start.getX(), start.getY(), end.getX(), end.getY()); | |
end.incrementConnectedNodes(); | |
start.incrementConnectedNodes(); | |
} | |
} | |
} | |
// Make sure nodes are on top of lines | |
for(int x=0; x<nodes.size(); x++) { | |
Node n = (Node) nodes.get(x); | |
n.display(); | |
} | |
save(random(5000)+"graph.png"); | |
} | |
class Node { | |
int nodeSize; | |
float xpos, ypos; | |
int otherNodesConnected = 0; | |
color nodeColor; | |
Node (float tempOver, float tempDown, int tempSize, color tempColor) { | |
xpos = tempOver; | |
ypos = tempDown; | |
nodeColor = tempColor; | |
nodeSize = tempSize; | |
} | |
void display() { | |
stroke(nodeColor); | |
fill(nodeColor); | |
ellipse(xpos, ypos, nodeSize, nodeSize); | |
} | |
void incrementConnectedNodes() { | |
otherNodesConnected++; | |
} | |
int getConnectedCount() { | |
return otherNodesConnected; | |
} | |
float getX() { | |
return xpos; | |
} | |
float getY() { | |
return ypos; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment