void setup() {
size(720, 480);
noLoop();
}
void draw() {
//Push original matrix
pushMatrix();
//Translate to center
translate(width/2, height/2);
//Outer Grey Squares
scale(2);
for(float k = 0; k < 2; k += .5) {
rotate(k * PI);
fill(102);
rect(20, 20, 150, 150);
}
//Diagonal Grey Squares
scale(.55);
rotate(.25 * PI);
for(float k = 0; k < 2; k += .5) {
rotate(k * PI);
rect(20, 20, 150, 150);
}
//Black Center
for(float k = 0; k < 2; k += .10) {
rotate(k*PI);
fill(0);
rect(20, 20, 90, 90);
}
//Pop to orinal origin
popMatrix();
//Pop original origin
pushMatrix();

//Left diagonal of Triangles
for(int i = 0; i < 3; i++) {
translate(width/4, height/4);
for(float k = 1; k < 3; k += .10) {
rotate(k * PI);
triangle(0, 0, 15, 0, 0, 20);
}
}
popMatrix();

pushMatrix();
translate(width, 0);
//Right Diagonal of Rotated Triangles
for(int i = 0; i < 3; i++) {
translate(-width/4, height/4);
for(float k = 1; k < 3; k += .10) {
rotate(k * PI);
triangle(0, 0, 15, 0, 0, 20);
}
}
}















//Program will draw a single random point triangle and rotate it
//multiple times in increments of .25*PI radians.
int screenwid = 720;
int screenhei = 480;
float[][] rotation = new float[2][2];

void setup() {
size(screenwid, screenhei);
noLoop();
}

void draw() {
//Create a random triangle
int x0, y0, x1, y1, x2, y2;
x0 = int(random(screenwid));
y0 = int(random(screenhei));
x1 = int(random(screenwid));
y1 = int(random(screenhei));
x2 = int(random(screenwid));
y2 = int(random(screenhei));
//Rotate it in increments of .25*PI radians
for(float i = 0; i < 2; i += .25) {
rotation(x0, y0, x1, y1, x2, y2, (i*PI));
}
}

void rotation(int x0, int y0, int x1, int y1, int x2, int y2, float rotat) {
//Set rotation angle
setRotation(rotat);
//Calculate origin of desired triangle
int origx = int((x0+x1+x2)/3);
int origy = int((y0+y1+y2)/3);
//Create point matrix based on origin
float[][] points = {{x0-origx, y0-origy}, {x1-origx, y1-origy}, {x2-origx, y2-origy}};
//Multiply by rotation matrix
points = multiply(points, rotation);
//Add back origin
for(int i = 0; i < points.length; i++) {
for(int j = 0; j < points[0].length; j++) {
if(j == 0)
points[i][j] += origx;
else
points[i][j] += origy;
}
}
//Show rotated triangle
triangle(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1]);
}

//Sets the values of the rotation matrix
void setRotation(float rotat) {
rotation[0][0] = cos(rotat);
rotation[0][1] = sin(rotat);
rotation[1][0] = -sin(rotat);
rotation[1][1] = cos(rotat);
}

//Used to multiply one matrix to another
float[][] multiply(float[][] a, float[][] b) {
float[][] c = new float[a.length][b[0].length];
for(int i = 0; i < c.length; i++) {
for(int j = 0; j < c[0].length; j++) {
for(int k = 0; k < b.length; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}














//Set screen size
final int screenwid = 720;
final int screenhei = 480;
int bxmin, bxmax, bymin, bymax;

void setup()
{
size(screenwid, screenhei);
noLoop();
}

void draw()
{
//Declare clipping bounds and create rectangle
bxmin = screenwid/4;
bymin = screenhei/4;
bxmax = screenwid/4 + screenwid/2;
bymax = screenhei/4 + screenhei/2;
//rect(bxmin, bymin, screenwid/2, screenhei/2);
rect(bxmin, bymin, (bxmax - bxmin), (bymax - bymin));

//Produce 5 triangles with random points
int x0, x1, x2, y0, y1, y2;
for(int i = 0; i < 5; i++)
{
//Initiate points of triangle
x0 = int(random(screenwid));
x1 = int(random(screenwid));
x2 = int(random(screenwid));
y0 = int(random(screenhei));
y1 = int(random(screenhei));
y2 = int(random(screenhei));
//Draw edges
drawline(x0, y0, x1, y1);
drawline(x1, y1, x2, y2);
drawline(x2, y2, x0, y0);
}
}

//Calculate Sutherland-Cohen clipping code
int bP(int x, int y) {
int b = 0;
//X too low
if(x < bxmin)
b += 8;
//X too high
if(x > bxmax)
b += 4;
//Y too low
if(y > bymax)
b += 2;
//Y too high
if(y < bymin)
b += 1;
return b;
}

boolean rejectCheck(int b0, int b1) {
if((b0 & b1) != 0)
return true;
return false;
}

boolean acceptCheck(int b0, int b1) {
if((b0 == 0) && (b1 == 0))
return true;
return false;
}

boolean drawline(int x0, int y0, int x1, int y1) {
int b0, b1;
while(true) {
b0 = bP(x0, y0);
b1 = bP(x1, y1);
if(rejectCheck(b0, b1))
return false;
if(acceptCheck(b0, b1)) {
line(x0, y0, x1, y1);
return true;
}
if(b0 == 0) {
int tempCoord, tempCode;
tempCoord = x0; x0 = x1; x1 = tempCoord;
tempCoord = y0; y0 = y1; y1 = tempCoord;
tempCode = b0; b0 = b1; b1 = tempCode;
}
if((b0 & 1)!=0) {
x0 += (x1 - x0)*(bymax-y0)/(y1-y0);
y0 = bymax;
}
else if((b0 & 2)!=0) {
x0 += (x1 - x0)*(bymin-y0)/(y1-y0);
y0 = bymin;
}
else if((b0 & 4)!=0) {
y0 += (y1-y0)*(bxmax-x0)/(x1-x0);
x0 = bxmax;
}
else if((b0 & 8)!=0) {
y0 += (y1-y0)*(bymin-x0)/(x1-x0);
x0 = bxmin;
}
}
}