# My projects library! - CodeMasterX's gallery megathread

**URL:** <https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301>\
**Category:** Gallery\
**Created:** [December 16, 2020, 1:58pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301 "2020-12-16T13:58:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 16, 2020, 1:58pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/1 "2020-12-16T13:58:38Z")

</div>

Hi! I made a git-hub page. My name on github is time-bender.

In this topic I’ll post all of my projects instead of creating new topics. It would probably be really annoying if a post “CodeMasterX posted: xyz GALERY” several times a day. If you want to copy my code feel free to!

Table of content:

- #1 - number of squres
- #2 - cesar-cypher
- #3 - chess board (empty)
- #4 - Bounce game
- #5 - Discord emoji counter
- #6 - polar rose
- #7 - Denominators
- #8 - Planetary swarm
- #9 - circles in a circle (Python)
- #10 - growing circles
- #11 - ring packer
- #12 - TicTacToe
- #13 - Meteor shower
- #14 - fractal tree with projectiles
- #14\_2 - fractal tree with projectiles V2
- #15 - lotteries!
- #16 - bouble sort
- #17 - Etch a sketch
- #18 - X in Row (flexible 4inRow)

**#1: number of squares**  
how many squares (x ^ 2) are between **a** and **b** :

```auto
int a = 10, b = 100, n = 0;
for (int i = floor(sqrt(a)); i < ceil(sqrt(b))+1; i++) {
  if (pow(i, 2) >= a && pow(i, 2) <= b) {
    n++;
  }
}
println(n);

```

Console: 7

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 16, 2020, 1:59pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/2 "2020-12-16T13:59:23Z")

</div>

If you want to ask questions use #project-id (#1, #2, #3,…) in the question.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 16, 2020, 2:31pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/3 "2020-12-16T14:31:51Z")

</div>

#1 here is an alternative algorithm that does the same thing but without the for-loop. Useful if `b` is very much bigger than `a`

```auto
int a = 10, b = 100, n = 0;
// Alternative algorithm avoiding use of a for-loop
int sqrt_a = (int) sqrt(a);
int sqrt_b = (int) sqrt(b);
int n = sqrt_b - sqrt_a + (0 != sqrt(a) - sqrt_a ? 0 : 1);
println(n);

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 16, 2020, 2:44pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/4 "2020-12-16T14:44:55Z")

</div>

#1

> [@quark](#):
>
> `int n = sqrt_b...`

You have already created n. It is a duplicate local variable.  
new code is

```auto
int a = 10, b = 1000, n = 0;
// Alternative algorithm avoiding use of a for-loop
int sqrt_a = (int) sqrt(a);
int sqrt_b = (int) sqrt(b);
n = sqrt_b - sqrt_a + (0 != sqrt(a) - sqrt_a ? 0 : 1);
println(n);

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 16, 2020, 6:34pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/5 "2020-12-16T18:34:29Z")

</div>

**#2: cesar cypher** ([https://edabit.com/challenge/a33jdGXkaQRtK9ZTs](https://edabit.com/challenge/a33jdGXkaQRtK9ZTs))  
it ciphers the input by shifting the letters by \<turn (integer)\>. There is also a decypher option which turns cyphered code back into normal text (without spaces)

> **Code**
>
> ```auto
> //Cesar cypher. It shifts the letters "turn" letter forward. With turn = 3 'a' turns info 'd'
> //Made by time-bender (CodeMasterX on processing forum)
> char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
> int turn = 3;
> String cipher = "";
> 
> void setup() {
> String input = "this is the input";
> println( decipher(cipher(input)), cipher(input));
> }
> void draw() {
> }
> String cipher(String input) {
> String output = "";
> for (int i = 0; i < input.length(); i++) for (int j = 0; j < alphabet.length; j++) if (input.charAt(i) == alphabet[j]) {
> output += alphabet[(j+turn) % alphabet.length];
> }
> return output;
> }
> String decipher(String input) {
> String output = "";
> for (int i = 0; i < input.length(); i++) for (int j = 0; j < alphabet.length; j++) if (input.charAt(i) == alphabet[j]) {
> if(j-turn < 0) {
> output += alphabet[alphabet.length + j -turn];
> } else {
> output += alphabet[(j-turn) % alphabet.length];
> }
> }
> return output;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 16, 2020, 7:06pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/6 "2020-12-16T19:06:48Z")

</div>

**#3: chess board (empty)** (preparations for bishop challenge ([https://edabit.com/challenge/6gkY9f4hNauYgwMpt](https://edabit.com/challenge/6gkY9f4hNauYgwMpt)))

It creates a chess grid and adds functions to turn “a3” into “16” and the other way around.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6c7a1f7852d5ebbb44124ed970b3e98d320f1853.png)

> **Code**
>
> ```auto
> //chess board. It displays the chess board along with the functions gridify(String) which turns "<letter><number> (like e2)" into numbers x + y * w. The ungridify reverses it
> //Made by time-bender (CodeMasterX on processing forum)
> char letters[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
> char numbers[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
> //String input = letters[(int)random(letters.length)] + "" + numbers[(int)random(numbers.length)] + "";
> 
> void setup() {
> size(600, 600);
> }
> 
> void draw() {
> background(0);
> for (int i = 0; i < 8; i++) {
> for (int j = 0; j < 8; j++) {
> fill( (( (i + j * 8) % 2 == j % 2)? color(255) : color(0)) );
> rect(i*width/8, j*height/8, width/8, height/8);
> fill(255, 0, 255);
> text(ungridify(i+j*8), (i+0.5)*width/8, (j+0.5)*height/8);
> }
> }
> }
> 
> int gridify(String input) {
> int num = -1;
> 
> for (int i = 0; i < numbers.length; i++) {
> if (numbers[i] == input.charAt(1)) {
> num = i * 8;
> }
> }
> 
> for (int i = 0; i < letters.length; i++) {
> if (letters[i] == input.charAt(0)) {
> num+=i;
> }
> }
> 
> return num;
> }
> 
> String ungridify(int input) {
> int x = input % 8, y = floor(input/8);
> 
> return letters[x] + "" + numbers[y];
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 16, 2020, 7:46pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/7 "2020-12-16T19:46:18Z")

</div>

if you have any interesting ideas just reply : )

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 16, 2020, 7:55pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/8 "2020-12-16T19:55:37Z")

</div>

**#4: bounce game** A simple game where a ball bounces from sides.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2c29aaf65264208d62e841be429037fc69513acd.png)

> **Code**
>
> ```auto
> int dir = 0;
> float x = 300, y = 300, r = 10, tr = 15, tx = 300, ty = 500,score = 0, buffer = 20, speed = 16;
> 
> void setup() {
> size(600,600);
> textAlign(3,3);
> }
> 
> void draw() {
> background(0);
> fill(255);
> circle(x,y,r*2);
> if(dir == 0) {
> y-=speed;
> } else if(dir == 1) {
> y+=speed;
> } else if(dir == 2) {
> x-=speed;
> } else if(dir == 3) {
> x+=speed;
> }
> if(x+r>width) {
> dir = 2;
> } else if(x - r < buffer) {
> dir = 3;
> }
> if(y+r>height) {
> dir = 0;
> } else if(y - r < 0) {
> dir = 1;
> }
> fill(255,0,0);
> circle(tx,ty,tr*2);
> if(dist(tx,ty,x,y) < tr + r) {
> tx = random(r,width-r);
> ty = random(r,height-r);
> score++;
> speed+=0.1;
> }
> fill(0,0,255);
> text("Score: " + score + " Speed: " + speed, width/2,buffer/2);
> }
> 
> void keyPressed() {
> if(key == 'w') dir = 0;
> if(key == 's') dir = 1;
> if(key == 'a') dir = 2;
> if(key == 'd') dir = 3;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 18, 2020, 10:02am UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/9 "2020-12-18T10:02:45Z")

</div>

**#5 Discord emoji counter** It prints emoji codes (1️⃣, 2️⃣,… 1️⃣0️⃣0️⃣)!

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1706ca6a61607b1d309ce38e8b0138e5dcc15bc5.png)

> **Code**
>
> ```auto
> String num[] = {":zero:",":one:",":two:",":three:",":four:",":five:",":six:",":seven:",":eight:",":nine:"};
> void setup() {
> for(int i = 2000; i < 2051; i++) print(getNum(i+"") + ", ");
> }
> String getNum(String number) {
> String output = "";
> for(int i = 0; i < (number+"").length(); i++) {
> String a = "" +(number+"").charAt(i);
> output += num[int(a)];
> }
> return output;
> }
> 
> ```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bf0b922b6748356ef99e9607ccc066378d94e8b9.png)

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 18, 2020, 10:05am UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/10 "2020-12-18T10:05:07Z")

</div>

**#6 Polar rose** Draws a polar rose

> **Code**
>
> ```auto
> int k = 200, br = 200, xc = 300, yc = 300, n = 500;
> void setup() {
> size(600,600);
> }
> void draw() {
> background(0);
> stroke(255);
>   
> float px = br * cos( cos(0))+xc, py = br * sin( cos(0)) + yc;
> for(int i = 0; i < n; i++) {
> float theta = TWO_PI/n*i;
> float r = br * cos(4*theta);
> float x = r * cos(theta) + xc, y = r * sin(theta) + yc;
> if(i != 0) {
> line(px,py,x,y);
> }
> px = x;
> py = y;
> }
> }
> 
> ```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b0832e531f3bfc0f6f1a2039bceebb62200eb4ff.png)

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 20, 2020, 1:47pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/11 "2020-12-20T13:47:44Z")

</div>

**#7 Denominators** It prints out all the denominators of `<num>` and all ways to multiply two numbers to get it!

(12 / 1,2,3,4,6,12, 1 \* 12 = 12, 2 \* 6 = 12, 3 \* 4 = 12, 4 \* 3 = 12, 6 \* 2 = 12, 12 \* 1 = 12)

> **Code**
>
> ```auto
> int num = 5040;
> ArrayList<Integer> dev = new ArrayList<Integer>();
> for(int i = 1; i < num+1; i++) {
> if(num % i == 0) {
> print(i+", ");
> dev.add(i);
> }
> }
> println();
> for(int i = 0; i < dev.size(); i++) {
> int a = dev.get(i), b = dev.get( dev.size() - i - 1 );
> println(a,b," | " + a + " * " + b + " = " + (a*b));
> }
> 
> ```

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b3ee97c7d28d45f6545075ff755c285b28a2a72b.png)

try using:  
[Anti prime numbers aka Highly composite numbers](https://en.wikipedia.org/wiki/Highly_composite_number)  
(numbers with highest number of devisors; 1,2,4,6,12,23,36,…840…5040…10080,…)

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 21, 2020, 7:16pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/12 "2020-12-21T19:16:31Z")

</div>

**#8 Planetary swarm** creates a swarm of balls (or planets) that act according to “realistic” gravity

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/dfb22fc75b4cd3aeecbb01d43177d2d294d11e13.png)

> **Code**
>
> ```auto
> ArrayList<Ball> balls = new ArrayList<Ball>();
> float g = 0.1, am = 500; //am = artificial mass, force applied to all if mouse is pressed
> 
> void setup() {
> fullScreen();
> balls.add(new Ball());
> balls.add(new Ball());
> //balls.get(0).setStats(400, 400, 10000, 0, 0);
> //balls.get(1).setStats(500, 500, 100, -0.4, 0);
> for(int i = balls.size(); i < 30; i++) {
> balls.add(new Ball());
> balls.get(i).setStats(random(width), random(height), random(10,50), 0, 0);
> }
> }
> 
> void draw() {
> background(0);
> for (int i = 0; i < balls.size(); i++) {
> for (int j = 0; j < balls.size(); j++) {
> if ( i != j) {
> balls.get(i).applyForce(balls.get(j).x, balls.get(j).y, balls.get(j).mass);
> }
> }
> }
> for (int i = 0; i < balls.size(); i++) {
> balls.get(i).display();
> }
> println(pow(0, 2));
>   
>   
> fill(255,50);
> circle(mouseX,mouseY,sqrt(am));
> if(mousePressed) {
> for(int i = 0; i < balls.size(); i++) {
> balls.get(i).applyForce(mouseX,mouseY,1000);
> }
> }
> }
> 
> class Ball {
> float x, y, mass, r, xs=0, ys=0, res;
> 
> void setStats(float x_, float y_, float mass_, float xs_, float ys_) {
> x = x_;
> y = y_;
> mass = mass_;
> r = sqrt(mass);
> res = 1/2 * mass;
> xs = xs_;
> ys = ys_;
> }
> void display() {
> fill(255);
> circle(x, y, r*2);
> }
> void applyForce(float x2, float y2, float m2) {
> float cForce = 1/2 * mass * (abs(xs)+abs(ys));
> float f = g * mass * m2 / pow(dist(x, y, x2, y2), 2);
> float dir = atan2(y2-y, x2-x);
> float combinedF = cForce + f;
> 
> float vel = sqrt(2*combinedF/mass);
> if (dist(x, y, x2, y2) > sqrt(m2) + sqrt(mass)+ 1) {
> xs += cos(dir)*vel;
> ys += sin(dir)*vel;
> x += xs;
> y += ys;
> }
> }
> }
> 
> void mouseWheel(MouseEvent scroll) {
> float e = scroll.getCount();
> am *= pow(1.1,-e);
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [December 22, 2020, 11:47pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/13 "2020-12-22T23:47:50Z")

</div>

Why don’t you create a user interface, where one can enter a text and encrypt it or one can enter an encrypted text and it has to be deciphered?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 23, 2020, 7:11am UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/14 "2020-12-23T07:11:13Z")

</div>

I’ll try that Thanks for the idea : )

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 28, 2020, 12:07pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/15 "2020-12-28T12:07:33Z")

</div>

**#9 Circles in a circle** Python mode

```auto
acc = 100
r = 100
r2 = 100
def setup():
    size(600,600)
    stroke(255)
def draw():
    background(0)
    n=frameCount
    a = TWO_PI/n
    b = TWO_PI/acc
    for i in range(n):
        x = width/2 + cos(a*i)*r2
        y = height/2 + sin(a*i)*r2
        for j in range(acc):
            line(x + cos(b*j)*r, y + sin(b*j)*r, x + cos(b*(j+1))*r, y + sin(b*(j+1))*r)

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/541b25acc3b59aeefef635a565b18d4bdc4906d3.png)

Here is a faster version where I use ellipse() function instead of drawing acc-sided polygons

```auto
acc = 100
r = 100
r2 = 100
def setup():
    size(600,600)
    stroke(255)
    frameRate(5)
def draw():
    background(0)
    noFill()
    n=frameCount
    a = TWO_PI/n
    b = TWO_PI/acc
    for i in range(n):
        x = width/2 + cos(a*i)*r2
        y = height/2 + sin(a*i)*r2
        ellipse(x,y,r*2,r*2)
    

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 31, 2020, 6:24pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/16 "2020-12-31T18:24:06Z")

</div>

**#10 Growing circles** Java mode -  
Draws circles that grow until they hit other circles OR hit the larger circle

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4d3efbc21f268e12638bfd5ed6bbb066f60bc717.png)  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d4c36851d557781450d7e77fcd13a9a0e58560f1.png)

> **Code**
>
> ```auto
> ArrayList<Circle> circles = new ArrayList<Circle>();
> float growSpeed = 0.5, r = 200, cx = 300, cy = 300, buffer1 = 1, buffer2 = 3;
> int addEveryNFrames = 2;
> 
> void setup() {
> size(600,600);
> circles.add(new Circle(300,300,0));
> }
> void draw() {
> background(0);
> circle(cx,cy,r*2);
> autoPlace(1000);
> for(int i = 0; i < circles.size(); i++) {
> circles.get(i).display();
> }
> if(frameCount % addEveryNFrames == 0) {
> for(int i = 0; i < circles.size(); i++) {
> circles.get(i).grow();
> }
> }
> }
> 
> class Circle {
> float x,y,d;
> boolean canGrow = true;
> Circle(float x_, float y_, float d_) {
> x = x_;
> y = y_;
> d = d_;
> }
> void display() {
> fill(255);
> noFill();
> stroke(255);
> circle(x,y,d);
> }
> void grow() {
> if(canGrow && check()) {
> d+=growSpeed;
> } else {
> canGrow = false;
> }
>     
> }
> boolean check() {
> for(int i = 0; i < circles.size(); i++) {
> float distance = dist(circles.get(i).x,circles.get(i).y, x, y);
> if(distance != 0) {
> if(distance < (d + circles.get(i).d)/2 + buffer1) {
> return false;
> }
> }
> }
> if(dist(cx,cy,x,y) > r - d/2 - buffer2) {
> return false;
> }
>     
> return true;
> }
> }
> 
> void mousePressed() {
> if(canPlace(mouseX,mouseY)) circles.add(new Circle(mouseX,mouseY,0));
> }
> boolean canPlace(float x_, float y_) {
> for(int i = 0; i < circles.size(); i++) {
> if(dist(x_,y_,circles.get(i).x,circles.get(i).y) < circles.get(i).d/2) {
> return false;
> }
> }
>   
>   
> return true;
> }
> void autoPlace(int tries) {
> for(int i = 0; i < tries; i++) {
> //float x = random(width), y = random(height);
> float dir = random(TWO_PI), move = random(r), x = cos(dir)*move + width/2, y = sin(dir)*move + height/2;
> if(canPlace(x,y)) {
> circles.add(new Circle(x,y,0));
> break;
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 31, 2020, 6:51pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/17 "2020-12-31T18:51:19Z")

</div>

**#11 ring packer** Java Mode  
Packs circles in a ring.Changes their sizes to fit.  
OLDER VERSION

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1a859cc29604f7944f2cb7524316d2882bd03ae5.png)

> **Code**
>
> ```auto
> float r = 200, cx = 300, cy = 300;
> int n =15;
> 
> void setup() {
> size(600,600);
> frameRate(1);
> }
> void draw() {
> background(0);
> noFill();
> stroke(255);
> for(int i = 0; i < n; i++) {
> float a = TWO_PI/n;
> circle(cx + cos(a*i)*r, cy + sin(a*i)*r, TWO_PI*r/n);
> }
> n++;
> }
> 
> ```

for values under 15 the circles overlap

NEWER VERSION WITHOUT BUGS

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/118b6816a6ee4fc1478ef03c3256b4ab83290c3a.png)

> **Code**
>
> ```auto
> float r = 200, cx = 300, cy = 300;
> int n = 1;
> 
> void setup() {
> size(600,600);
> frameRate(2);
> }
> void draw() {
> background(0);
> noFill();
> stroke(255);
> float s = dist( cos(0)*r, sin(0)*r, cos(TWO_PI/n)*r, sin(TWO_PI/n)*r);
> for(int i = 0; i < n; i++) {
> float a = TWO_PI/n;
> circle(cx + cos(a*i)*r, cy + sin(a*i)*r, s);
> }
> n++;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [January 1, 2021, 9:02pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/18 "2021-01-01T21:02:28Z")

</div>

#10 looks like a model for foam creation.  
If there is a barrier at which bubbles don’t grow anymore, a probability, which decides if smaller bubbles get destroyed/combine with a larger one or not and there is a controllable speed of growth with limited time of growth, you can get an equilibrium for different foam formation conditions and predict final porosity/bubble size distribution.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 1, 2021, 9:29pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/19 "2021-01-01T21:29:51Z")

</div>

**#12 tic tac toe** Java Mode  
Visual tic tac toe in 91 lines of code

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/56498fb6e6db233d1f9878370e77745e46c57c2c.png)

> **Code**
>
> ```auto
> int w = 3, h = 3, scl=200, turn=1;
> int grid[][] = new int[w][h];
> void setup() {
> size(600, 600);
> }
> void draw() {
> background(0);
> strokeWeight(5);
> noFill();
> stroke( ( (turn == 1)? color(0, 0, 255) : color(255, 0, 0) ) );
> rect(0, 0, width, height);
> stroke(255);
> for (int i = 0; i < w-1; i++) line( map(i+1, 0, w, 0, width), 0, map(i+1, 0, w, 0, width), height);
> for (int j = 0; j < h-1; j++) line( 0, map(j+1, 0, h, 0, height), height, map(j+1, 0, h, 0, height));
> for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
> stroke( ( (grid[i][j] == 1)? color(0, 0, 255) : color(255, 0, 0) ) );
> if (grid[i][j] == 1) circle((i+0.5)*scl, (j+0.5)*scl, scl/2);
> else if (grid[i][j] == -1) {
> line((i+0.25)*scl, (j+0.25)*scl, (i+0.75)*scl, (j+0.75)*scl);
> line((i+0.75)*scl, (j+0.25)*scl, (i+0.25)*scl, (j+0.75)*scl);
> }
> }
> println(checkWin(1), checkWin(-1));
> if (checkWin(1)) {
> println("Circle wins");
> noLoop();
> //clearStage();
> } else if (checkWin(-1)) {
> println("Cross wins");
> noLoop();
> //clearStage();
> }
> }
> void mousePressed() {
> int mx = floor(map(mouseX, 0, width, 0, w)), my = floor( map( mouseY, 0, height, 0, h));
> if (grid[mx][my] == 0) {
> grid[mx][my] = turn; 
> turn *= -1;
> }
> }
> void keyPressed() { 
> if (key == ' ') {
> clearStage(); 
> loop();
> }
> }
> boolean checkWin(int inf) {
> boolean win = true;
> for (int i = 0; i < w; i++) if (grid[i][i] != inf) win = false;
> if (win == true) { 
> pushStyle(); 
> strokeWeight(15); 
> stroke(0, 255, 0); 
> line(scl/2, scl/2, scl*2.5, scl*2.5); 
> popStyle(); 
> return true;
> } else win = true;
> for (int i = 0; i < w; i++) if (grid[w-1-i][i] != inf) win = false;
> if (win == true) { 
> pushStyle(); 
> strokeWeight(15); 
> stroke(0, 255, 0); 
> line(scl*2.5, scl/2, scl/2, scl*2.5); 
> popStyle(); 
> return true;
> } else win = true;
> for (int k = 0; k < h; k++) { for (int i = 0; i < w; i++) if (grid[i][k] != inf) win = false;
> if (win == true) { 
> pushStyle(); 
> strokeWeight(15); 
> stroke(0, 255, 0); 
> line((0.5)*scl, (k+0.5)*scl, (2.5)*scl, scl*(k+0.5)); 
> popStyle(); 
> return true;
> } else win = true;
> }
> for (int k = 0; k < h; k++) { for (int i = 0; i < w; i++) if (grid[k][i] != inf) win = false;
> if (win == true) { 
> pushStyle(); 
> strokeWeight(15); 
> stroke(0, 255, 0); 
> line((k+0.5)*scl, (0.5)*scl, (k+0.5)*scl, scl*(2.5)); 
> popStyle(); 
> return true;
> } else win = true;
> }
> return false;
> }
> void clearStage() {
> for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) grid[i][j] = 0;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 3, 2021, 2:19pm UTC](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301/20 "2021-01-03T14:19:37Z")

</div>

**#13 Meteor shower** Java mode

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8be26ee37931ea7765dd56f3bc3189b39a9d1946.png)

> **Code**
>
> ```auto
> ArrayList<star> stars = new ArrayList<star>();
> ArrayList<particle> particles = new ArrayList<particle>();
> float globalG = 0.1, colorOffset = 50, minStarSpeed = 2, maxStarSpeed = 6, minParticleSpeed = 2, maxParticleSpeed = 3, starSize = 8;
> void setup() {
> fullScreen();
> //size(600,600);
> colorMode(HSB);
> }
> void draw() {
> background(0);
> for(int i = 0; i < stars.size(); i++) {
> stars.get(i).move();
> stars.get(i).display();
> //checking for stars to delete
> if(stars.get(i).x > width*1.5 || stars.get(i).y > height*1.5) {
> stars.remove(i);
> }
> }
> for(int i = 0; i < particles.size(); i++) {
> particles.get(i).move();
> particles.get(i).display();
> //deleting particels
> if(particles.get(i).age > particles.get(i).maxAge) {
> particles.remove(i);
> }
> }
> if(random(100) > 90) {
> addStar(random(width/2),random(height/2),random(minStarSpeed,maxStarSpeed),random(minStarSpeed,maxStarSpeed),color(random(255),255,255));
> }
> }
> void mousePressed() {
> addStar(mouseX,mouseY,5,5,color(random(255),255,255));
> }
> void addStar(float x, float y, float xs, float ys, color clrFill) {
> stars.add(new star(x,y,xs,ys,clrFill));
> }
> 
> class star {
> float xs,ys, x,y, r = starSize;
> color clr;
> star(float x_, float y_, float xs_, float ys_, color clr_) {
> x = x_;
> y = y_;
> xs = xs_;
> ys = ys_;
> clr = clr_;
> }
> void display() {
> fill(clr);
> noStroke();
> circle(x,y,r*2);
> }
> void move() {
> x += xs;
> y += ys;
> float rep = random(2,10);
> for(int i = 0; i < rep; i++) {
> float dir = random(TWO_PI), speed = random(minParticleSpeed,maxParticleSpeed);
> addParticle(x,y,cos(dir)*speed,sin(dir)*speed,0,20, color(hue(clr)+random(-colorOffset/2, colorOffset/2),255,255));
> }
> }
> }
> class particle {
> float x,y,xs,ys,age,maxAge;
> int id;
> color clr;
> particle(float x_, float y_, float xs_, float ys_, int startAge_, int maxAge_, color clrFill) {
> x = x_;
> y = y_;
> xs = xs_;
> ys = ys_;
> age = startAge_;
> maxAge = maxAge_;
> clr = clrFill;
> }
> void display() {
> fill(clr, map( age,0,maxAge,255,0));
> noStroke();
> circle(x,y,5);
> }
> void move() {
> ys+=globalG;
> x += xs;
> y += ys;
> age++;
> }
> }
> void addParticle(float x, float y, float xs, float ys, int startAge, int maxAge, color clrFill) {
> particles.add(new particle(x,y,xs,ys,startAge,maxAge, clrFill));
> }
> 
> ```

[Next page](https://discourse.processing.org/t/my-projects-library-codemasterxs-gallery-megathread/26301.md?page=2)
