Created
January 27, 2015 13:22
-
-
Save CSchoel/85ef0b52f481abdcba67 to your computer and use it in GitHub Desktop.
Create pixel art by flipping values in a two-dimensional Array with mouse clicks.
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
//Autor: Christopher Schölzel | |
class Field { | |
int[][] content; | |
float fh,fw; | |
float px,py; | |
Field(float posX, float posY, int fieldsX, int fieldsY, float fieldWidth, float fieldHeight) { | |
content = new int[fieldsY][fieldsX]; | |
this.fw = fieldWidth; | |
this.fh = fieldHeight; | |
this.px = posY; | |
this.py = posY; | |
} | |
int xpos(float x) { | |
return (int)((x-px)/fw); | |
} | |
int ypos(float y) { | |
return (int)((y-py)/fh); | |
} | |
void swap(int x, int y) { | |
assert y >= 0 && y < content.length; | |
assert x >= 0 && x < content[y].length; | |
content[y][x] = 1 - content[y][x]; | |
} | |
boolean isValidIndex(int x, int y) { | |
return y >= 0 && y < content.length && x >= 0 && x < content[y].length; | |
} | |
void display() { | |
for(int y = 0; y < content.length; y++) { | |
for(int x = 0; x < content[y].length; x++) { | |
fill(content[y][x] == 0 ? 255 : 0); | |
strokeWeight(2); | |
stroke(0); | |
rect(px + x * fw, py + y * fh, fw, fh); | |
} | |
} | |
//Markierung zeichnen | |
int mx = xpos(mouseX); | |
int my = ypos(mouseY); | |
if(isValidIndex(mx,my)) { | |
noStroke(); | |
fill(0,0,255,100); | |
rect(px+mx*fw+1,py+my*fh+1,fw-2,fh-2); | |
} | |
} | |
} | |
Field field; | |
void setup() { | |
size(400,400); | |
field = new Field(5,5,10,10,30,30); | |
} | |
void draw() { | |
background(255); | |
field.display(); | |
} | |
void mousePressed() { | |
int x = field.xpos(mouseX); | |
int y = field.ypos(mouseY); | |
if(field.isValidIndex(x,y)) field.swap(x,y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment