Skip to content

Instantly share code, notes, and snippets.

@gasin gasin/mabling.pde
Last active Dec 3, 2017

Embed
What would you like to do?
code of processing to draw mabling images
import java.util.Random;
final int SIZE = 500;
int TIMER = 0;
int[][][] board = new int[SIZE][SIZE][3];
int[][][] buffer = new int[SIZE][SIZE][3];
boolean inside_board(int x, int y)
{
return (0 <= x && x < SIZE && 0 <= y && y < SIZE);
}
void paint_white(int[] cell)
{
for (int i = 0; i < 3; i++) {
cell[i] = 255;
}
}
void board_copy(int[][][] from, int[][][] to)
{
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
to[i][j] = from[i][j].clone();
}
}
}
void setup()
{
size(500, 500);
background(255, 255, 255);
frameRate(1);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
paint_white(board[i][j]);
}
}
}
void drop_circle()
{
Random rnd = new Random();
int x = rnd.nextInt(SIZE);
int y = rnd.nextInt(SIZE);
int r = rnd.nextInt(80);
r += 20;
int red = rnd.nextInt(256);
int green = rnd.nextInt(256);
int blue = rnd.nextInt(256);
board_copy(board, buffer);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if ((i-x)*(i-x)+(j-y)*(j-y) <= r*r) {
board[i][j][0] = red;
board[i][j][1] = green;
board[i][j][2] = blue;
} else {
double tmp = Math.sqrt(1-r*r/(double)((i-x)*(i-x)+(j-y)*(j-y)));
int fromx = (int)(x + (i-x) * tmp);
int fromy = (int)(y + (j-y) * tmp);
if (inside_board(fromx, fromy)) {
board[i][j] = buffer[fromx][fromy].clone();
} else {
paint_white(board[i][j]);
}
}
}
}
}
void draw_line()
{
Random rnd = new Random();
double theta = rnd.nextDouble()*Math.PI;
double ex = Math.cos(theta), ey = Math.sin(theta);
double rx = Math.cos(theta+Math.PI), ry = Math.sin(theta+Math.PI);
int ax = rnd.nextInt(SIZE), ay = rnd.nextInt(SIZE);
double alpha = 100.0, beta = 10.0;
board_copy(board, buffer);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
double tmp = alpha * beta / (beta + Math.abs((i-ax)*rx+(j-ay)*ry));
int fromx = (int)(i - tmp * ex);
int fromy = (int)(j - tmp * ey);
if (inside_board(fromx, fromy)) {
board[i][j] = buffer[fromx][fromy].clone();
} else {
paint_white(board[i][j]);
}
}
}
}
void draw_spiral()
{
Random rnd = new Random();
int cx = rnd.nextInt(SIZE);
int cy = rnd.nextInt(SIZE);
int r = rnd.nextInt(80);
r += 20;
double alpha = 100.0, beta = 10.0;
board_copy(board, buffer);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
double d = Math.abs(Math.sqrt((i-cx)*(i-cx)+(j-cy)*(j-cy))-r);
double theta = alpha * beta / ((d+beta) * (Math.sqrt((i-cx)*(i-cx)+(j-cy)*(j-cy))));
int fromx = (int)(cx + (i-cx) * Math.cos(theta) + (j-cy) * Math.sin(theta));
int fromy = (int)(cy + (i-cx) * -Math.sin(theta) + (j-cy) * Math.cos(theta));
if (inside_board(fromx, fromy)) {
board[i][j] = buffer[fromx][fromy].clone();
} else {
paint_white(board[i][j]);
}
}
}
}
void draw_wave()
{
Random rnd = new Random();
int A = rnd.nextInt(5);
A += 5;
double t = rnd.nextDouble() * Math.PI;
double fai = rnd.nextDouble() * Math.PI;
double omega = rnd.nextDouble()/5;
board_copy(board, buffer);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
double theta = omega * (i*Math.sin(t)-j*Math.cos(t)) + fai;
int fromx = (int)(i - A * Math.sin(theta) * Math.cos(t));
int fromy = (int)(j - A * Math.sin(theta) * Math.sin(t));
if (inside_board(fromx, fromy)) {
board[i][j] = buffer[fromx][fromy].clone();
} else {
paint_white(board[i][j]);
}
}
}
}
void draw()
{
background(0);
TIMER++;
if (TIMER%5 <= 1) {
drop_circle();
} else if (TIMER%5 == 2) {
draw_line();
} else if (TIMER%5 == 3) {
draw_spiral();
} else {
draw_wave();
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
stroke(board[i][j][0], board[i][j][1], board[i][j][2]);
point(i, j);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.