My projects library! - CodeMasterX's gallery megathread

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:

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

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

#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

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);
1 Like

#1

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

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);
1 Like

#2: cesar cypher (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
//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;
}

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

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

Code
//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];
}

1 Like

if you have any interesting ideas just reply : )

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

Code
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;
}

#5 Discord emoji counter It prints emoji codes (:one:, :two:,… :one::zero::zero:)!

Code
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;
}

#6 Polar rose Draws a polar rose

Code
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;
  }
}

#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
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

try using:
Anti prime numbers aka Highly composite numbers
(numbers with highest number of devisors; 1,2,4,6,12,23,36,…840…5040…10080,…)

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

Code
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);
}

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?

1 Like

I’ll try that Thanks for the idea : )

#9 Circles in a circle Python mode

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)

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

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)
    

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


Code
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;
    }
  }
}
3 Likes

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

Code
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

Code
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++;
}

#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.

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

Code
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;
}

1 Like

#13 Meteor shower Java mode

Code
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));
}