Skip to content

Instantly share code, notes, and snippets.

@suzukimilanpaak
Created January 4, 2014 17:14
Show Gist options
  • Save suzukimilanpaak/8257538 to your computer and use it in GitHub Desktop.
Save suzukimilanpaak/8257538 to your computer and use it in GitHub Desktop.
mistake to draw strange ghoast
import java.util.*;
// Size of display
int DISPLAY_WIDTH = 500;
int DISPLAY_HEIGHT = 500;
// Colours
int BLACK = 0;
int WHITE = 255;
int CENTER_X = 0;
int CENTER_Y = 0;
SocialGraphCsv csv;
// This class saves coordinate
class Coordinate {
int x;
int y;
// The Constructor is defined with arguments.
Coordinate(float _x, float _y) {
x = int(_x);
y = int(_y);
}
public String toString() {
return x + ", " + y;
}
}
class EquilateralPolygon {
int numVertexes;
int radius;
float[] angles;
Coordinate[] vertexes;
EquilateralPolygon(int _numVertexes, int _radius) {
numVertexes = _numVertexes;
angles = new float[numVertexes];
radius = _radius;
}
public float xVertex(int i) {
return cos(angles[i]) * radius;
}
public float yVertex(int i) {
return sin(angles[i]) * radius;
}
public Coordinate[] vertexes() {
vertexes = new Coordinate[numVertexes];
float angleForSingleArc = 2 * PI / numVertexes;
for(int i = 0; i < numVertexes; i++) {
// Sum up angles
angles[i] = HALF_PI - angleForSingleArc * i;
float x = xVertex(i);
float y = - yVertex(i);
vertexes[i] = new Coordinate(x, y);
}
return vertexes;
}
// Put tiny circles onto each vertex
public void drawVertexes() {
smooth();
textAlign(CENTER);
int r = 20;
for (int i = 0; i < numVertexes; i++) {
// Draw black circle for the first vertex and draw gray ones for the rest
int colorValue = i == 0 ? 0 : 200;
stroke(colorValue);
strokeWeight(1);
ellipse(vertexes[i].x, vertexes[i].y, r, r);
}
}
public void drawIndexes() {
for (int i = 0; i < numVertexes; i++) {
fill(BLACK);
text(i, vertexes[i].x, vertexes[i].y);
noFill();
}
}
}
class SocialGraphCsv {
ArrayList<ArrayList<String>> stringList;
ArrayList<String> nodeNames;
ArrayList<ArrayList<String>> familiarities;
SocialGraphCsv(String filePath){
String[] rows = loadStrings(filePath);
this.stringList = convertToStrings(rows);
this.nodeNames = nodeNames(this.stringList);
this.familiarities = familiarities(this.stringList);
}
public ArrayList<String> nodeNames(ArrayList<ArrayList<String>> stringValues) {
ArrayList<String> nameOfNodes = (ArrayList<String>) stringValues.get(0).clone();
nameOfNodes.remove(0);
return nameOfNodes;
}
public ArrayList<ArrayList<String>> convertToStrings(String[] rows){
ArrayList<ArrayList<String>> csvRows = new ArrayList<ArrayList<String>>(rows.length -1);
for(int i = 0; i < rows.length - 1; i++) {
String[] cols = cols(rows[i]);
ArrayList<String> csvCols = new ArrayList<String>();
for(int j = 0; j < cols.length - 1; j++){
csvCols.add(cols[j]);
}
csvRows.add(csvCols);
}
return csvRows;
}
private String[] cols(String row) {
return split(row, ',');
}
public ArrayList<ArrayList<String>> familiarities(ArrayList<ArrayList<String>> stringValues) {
ArrayList<ArrayList<String>> vals = (ArrayList<ArrayList<String>>) stringValues.clone();
for(ArrayList<String> row: vals) row.remove(0);
vals.remove(0);
return vals;
}
}
class FamiliarityAsLine {
Coordinate vertexMe;
Coordinate vertexPeer;
Coordinate center;
FamiliarityAsLine(String familiarity, Coordinate vertexMe, Coordinate vertexPeer, Coordinate center) {
stroke(128, 128, 0, 128); // Orange
determineWeight(Integer.parseInt(familiarity));
this.vertexMe = vertexMe;
this.vertexPeer = vertexPeer;
this.center = center;
}
public void drawLine(){
int x1 = this.vertexMe.x;
int y1 = this.vertexMe.y;
int x2 = this.vertexPeer.x;
int y2 = this.vertexPeer.y;
int ctlX1 = int(x1 - (x1 - center.x) * 0.5);
int ctlY1 = int(y1 - (x1 - center.y) * 0.5);
int ctlX2 = int(x2 - (x1 - center.x) * 0.5);
int ctlY2 = int(y2 - (x1 - center.y) * 0.5);
bezier(x1, y1, ctlX1, ctlY1, ctlX2, ctlY2, x2, y2);
}
public void determineWeight(int familiarity) {
switch(familiarity) {
case 0:
strokeWeight(3);
break;
case 1:
strokeWeight(5);
break;
case 2:
strokeWeight(8);
break;
}
}
}
class SocialGraph {
EquilateralPolygon polygon;
Coordinate[] vertexes;
Coordinate center;
ArrayList<ArrayList<String>> familiarities;
SocialGraph(ArrayList<ArrayList<String>> familiarities, int radius, Coordinate center) {
this.polygon = new EquilateralPolygon(familiarities.get(0).size(), radius);
this.center = center;
this.familiarities = familiarities;
}
public void drawNodes() {
this.vertexes = polygon.vertexes();
polygon.drawVertexes();
}
public void drawNodeNames(int numVertexes, int radius) {
// polygon.drawNodeNames();
}
public void drawFamiliarities() {
for(int i = 0; i < familiarities.size() - 1; i++) {
for(int j = 0; j < familiarities.get(i).size() - 1; j++) {
FamiliarityAsLine line = new FamiliarityAsLine(
familiarities.get(i).get(j),
this.vertexes[i],
this.vertexes[j],
this.center
);
line.drawLine();
}
}
}
}
void setup() {
csv = new SocialGraphCsv("network.csv");
size(DISPLAY_WIDTH, DISPLAY_HEIGHT);
background(WHITE);
noLoop();
}
void draw() {
// Displace the drawn polygon by half width and half height of `display`, which means center.
// With `translate`, we can calculate coodinate of each vertex as distance from the center of a
// diagram, where it's coordinate is as (0, 0), and then, displace the polygon to actual coordinate
// of `display`.
translate(DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2);
int radius = 200;
SocialGraph socialGraph = new SocialGraph(csv.familiarities, radius, new Coordinate(CENTER_X, CENTER_Y));
socialGraph.drawNodes();
// socialGraph.drawNodeNames();
socialGraph.drawFamiliarities();
//
saveFrame("result.jpg");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment