# Running a circle pattern inside a given shape

Hi,
I am cosidering myself still a beginner, trying to learn as much as I can.
I would like to realize something but I m stuck. maybe someone has a tip or solution…

Within my sketch I create little circles row by row. they all have the same diameter and distance to each other and a given starting point.
And I have loaded an .svg shape.

What I would like to achieve is, that that circles start inside this shape (top left corner) fill the first row and when it reaches the right boarder of the shape jumps into the next row, starting there in a way that it is always inside the shape… I wouldn t mind, if the circles arent vertically aligned due to different starting points

Do you know what I mean ?

Any help welcome.
cheers, Tom!

``````
import processing.pdf.*;
PShape s;
float x;
float y;
float xsp = 20;

void setup () {
size (600, 600);
beginRecord(PDF, "test.pdf");
background(255);
smooth();
noStroke();
fill(0,250);
frameRate(5);
x = 20;
y = 20;
}

void draw () {
shape(s, 0, 0, 500, 500);
x = x + xsp;
ellipse (x, y, 8, 8);
fill (0,255);

if (x > width-40) {
x = 20;
y += 15;
}
}

void keyPressed() {
if (key == 's') {
endRecord();
exit();
}

}
``````
1 Like

Hi TomMoto,

You can use the ‘get’ function to read the color of a pixel on the screen of the shape you draw on the screen. So what you can do is find the first non-white pixel from the left on the current line and start drawing circles there. Then on the next line search again. etc…

Like this:

``````import processing.pdf.*;
PShape s;
int x = 0;
int y = 25;
int xsp = 25;

void setup () {
size (600, 600);
beginRecord(PDF, "test.pdf");
background(255);
smooth();
noStroke();
fill(0, 250);
frameRate(5);
shape(s, 0, 0, width, height);

for (x=0; x<width; x++) {
if (get(x, y)!=-1) break; //exit loop on first non-white pixel and remember x value
}
}

void draw () {
ellipse (x + 15, y, 10, 10);
fill (0, 255);

x = x + xsp;
if (x > width-28) {
y += 30;
for (x=0; x<width; x++) {
if (get(x, y)!=-1) break; //exit loop on first non-white pixel and remember x value
}
}
}

void keyPressed() {
if (key == 's') {
endRecord();
exit();
}
}
``````

Good luck!
Rick

3 Likes

Hi Rick,
wow !!! Thanks so much for that ! Absolutely makes sense.

Best
Tom

Hi, I need really help and this is my discussion : Circle Packing. Trying to implement on my project

I want to pack circles inside of that shapes one by one like this :

How can I reach just white borders or How can I work with just the shape which is white. Is svg format is work with that ? Could you guys help me ?

Thank you.