Skip to content

Instantly share code, notes, and snippets.

@toddmoy
Created January 14, 2013 21:03
Show Gist options
  • Select an option

  • Save toddmoy/4533449 to your computer and use it in GitHub Desktop.

Select an option

Save toddmoy/4533449 to your computer and use it in GitHub Desktop.
package cooccurrencematrix;
import processing.core.PApplet;
import controlP5.*;
public class CooccurrenceMatrix extends PApplet {
static final long serialVersionUID = 1;
int items = 50;
int itemSize = 10;
float matrix[][];
float threshold = 0.7f;
int margin = 10;
ControlP5 controls;
Slider slider;
public void setup() {
int w = (items*itemSize) + (margin*2) + 200;
int h = (items*itemSize) + (margin*2);
size(w, h);
controls = new ControlP5(this);
slider = controls.addSlider("threshold")
.setPosition((items*itemSize)+margin*2, margin)
.setWidth(190)
.setRange(0,1);
generateMatrix();
}
public void draw() {
colorMode(RGB);
background(255, 255, 255);
drawMatrix();
}
public void drawMatrix() {
colorMode(HSB, 1.0f);
noStroke();
pushMatrix();
translate(10, 10);
for(int y=0; y<items; y++) {
for(int x=0; x<items; x++) {
if (matrix[y][x] < threshold) {
fill(255);
} else {
fill(0, 0, 1 - matrix[y][x]);
}
rect(y*itemSize, x*itemSize, itemSize, itemSize);
}
}
popMatrix();
// Draw a border around it
noFill();
stroke(0);
rect(margin, margin, items * itemSize, items * itemSize);
}
public void generateMatrix() {
matrix = new float[items][items];
int rowIndex = 0;;
// init by blackout
for(int y=0; y<items; y++) {
for(int x=0; x<items; x++) {
matrix[y][x] = 0.0f;
}
}
for(int y=0; y<items; y++) {
for(int x=rowIndex; x<items; x++) {
if (x == y) {
matrix[x][y] = 1.0f;
} else {
float tmp = (float)random(0,1);
matrix[x][y] = tmp;
//matrix[y][x] = tmp;
}
}
rowIndex++;
}
}
public void keyPressed() {
generateMatrix();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment