#Rasterizing an Image with rotating maze tiles

5 messages · Page 1 of 1 (latest)

still fjord
#

Hi Coding Train community!

I am trying to figure out how to rasterize a PImage(); by connecting the rotation of each tile to a value each pixel. So far I have figured out the rotation of the rectangle and the maze tile pattern, but I can’t figure out why my maze tiles are not rotating as well?

This is the current output:

#

,,,

/*
Maze Tile
*/

// Load the image to be rasterized
PImage img;

// declare variables
float x, y;
float x2, y2;
float h, w;
float xShift, yShift;

// these values can be changed
// Set the size of the rectangles
int xSclFactor = 30;
int ySclFactor = 30;
//set size of steps in the square
float wStep = 2;
float hStep = 2;

void setup(){
//these values can be changed
size(1000,1000);
pixelDensity(2);
img = loadImage("11.jpg");
img.resize(950,950);

// Set the image mode to center the image within the canvas
imageMode(CENTER);

background(0);
strokeWeight(1);
stroke(255);
noFill();
smooth();

// end of changeable details
x = width / xSclFactor;
y = height / ySclFactor;
y2 = x2 = 0;
for(int i = 0; i < width; i+=x){
for(int j = 0; j < height; j+=y){
h = w = 0;
yShift = j;
xShift = i;
createMaze();
}
}
}
,,,

#

,,,

void draw(){
// Iterate over the pixels of the image
for (int x = 0; x < width; x += xSclFactor) {
for (int y = 0; y < height; y += ySclFactor) {
// Calculate the average color of the current pixel
int c = getAverageColor(x, y, xSclFactor, ySclFactor);
noFill();
stroke(1);

  // Calculate the rotation of the rectangle based on the pixel color
  float rotation = map(brightness(c), 0, 255, 0, TWO_PI);

  // Draw a rotated rectangle at the current position
  pushMatrix();
  translate(x + xSclFactor/2, y + ySclFactor/2);
  rotate(rotation);
  rect(-xSclFactor/2, -ySclFactor/2, xSclFactor, ySclFactor);
  popMatrix();
}

}
}

// Function to calculate the average color of a region within the image
int getAverageColor(int x, int y, int w, int h) {
int r = 0;
int g = 0;
int b = 0;
int count = 0;

// Iterate over the region and sum the color values
for (int i = x; i < x+w; i++) {
for (int j = y; j < y+h; j++) {
color c = img.get(i, j);
r += red(c);
g += green(c);
b += blue(c);
count++;
}
}

// Calculate the average color and return it
r /= count;
g /= count;
b /= count;
return color(r, g, b);
}

void createMaze(){
beginShape();
vertex((x-w)+ xShift, (y2+h) + yShift);
for(float i = min(width/xSclFactor, height/ySclFactor);
i > min (width/ xSclFactor, height/ySclFactor/2);
i-= max(wStep, hStep)){
vertex((x-w)+xShift, (y-h)+yShift);
vertex((x2+w)+xShift, (y-h)+yShift);
vertex((x2+w)+xShift, (y2+h)+yShift);
w += wStep;
vertex((x-w)+xShift, (y2+h)+yShift);
h += hStep;
}
endShape();
}
,,,

#

@calm wyvern

calm wyvern
#

sorry for not responding! i was asleep at the time you posted this! ill try to solve the problem when i have time.