My projects library! - CodeMasterX's gallery megathread

#14 Fractal trees with projectiles Java mode

Code
ArrayList<ball> balls = new ArrayList<ball>();
float splitTimer = 64, moveSpeed = 1, m = 0.9, splitInto = 3, splitAngle = PI/3;
float splitDir[] = new float[int(splitInto)];
void setup() {
  size(600, 600);
  balls.add(new ball(300, 300, -PI/2, moveSpeed));
  for(int i = 0; i < splitDir.length; i++) {
    splitDir[i] = map(i,0,splitInto-1,-splitAngle,splitAngle);
  }
}
void triggerSplit() {
  int ballsSize = balls.size();
  for (int i = 0; i < ballsSize; i++) {
    balls.get(i).splitBall();
  }
}
void draw() {
  background(0);
  for (int i = 0; i < balls.size(); i++) {
    balls.get(i).move();
    balls.get(i).display();
  }
  if (frameCount % floor(splitTimer) == 0) { 
    triggerSplit();
    splitTimer*=m;
  }
}
class ball {
  float x, y, dir, speed = 10;
  ball(float x_, float y_, float dir_, float speed_) {
    x = x_;
    y = y_;
    dir = dir_;
    speed = speed_;
  }
  void display() {
    fill(255);
    circle(x, y, 5);
  }
  void move() {
    x+= cos(dir)*speed;
    y+= sin(dir)*speed;
  }
  void splitBall() {
    for(int i = 0; i < splitInto-1; i++) {
      addBall(x,y,dir+splitDir[i], speed);
    }
    dir += splitDir[int(splitInto)-1];
  }
}
void addBall(float x_, float y_, float dir_, float speed_) {
  balls.add(new ball(x_, y_, dir_, speed_));
}

Current number of segments is 3

just add trails and remove balls.get(i).dispaly() to draw the fractal tree

2 Likes

#14_2 fractal trees with projectiles V2 Java mode

Code
ArrayList<ball> balls = new ArrayList<ball>();
float splitTimer = 64, moveSpeed = 1, m = 0.9, splitInto = 3, splitAngle = PI/3;
float splitDir[] = new float[int(splitInto)];
void setup() {
  surface.setTitle("aa");;
  size(600, 600);
  balls.add(new ball(300, 300, -PI/2, moveSpeed));
  for(int i = 0; i < splitDir.length; i++) {
    splitDir[i] = map(i,0,splitInto-1,-splitAngle,splitAngle);
  }
  background(0);
}
void triggerSplit() {
  int ballsSize = balls.size();
  for (int i = 0; i < ballsSize; i++) {
    balls.get(i).splitBall();
  }
}
void draw() {
  for (int i = 0; i < balls.size(); i++) {
    balls.get(i).move();
    balls.get(i).display();
  }
  if (frameCount % floor(splitTimer) == 0) { 
    triggerSplit();
    splitTimer*=m;
  }
}
class ball {
  float x, y, dir, speed = 10;
  ball(float x_, float y_, float dir_, float speed_) {
    x = x_;
    y = y_;
    dir = dir_;
    speed = speed_;
  }
  void display() {
    //fill(255);
    //circle(x, y, 5);
    stroke(255);
    point(x,y);
  }
  void move() {
    x+= cos(dir)*speed;
    y+= sin(dir)*speed;
  }
  void splitBall() {
    for(int i = 0; i < splitInto-1; i++) {
      addBall(x,y,dir+splitDir[i], speed);
    }
    dir += splitDir[int(splitInto)-1];
  }
}
void addBall(float x_, float y_, float dir_, float speed_) {
  balls.add(new ball(x_, y_, dir_, speed_));
}
void keyPressed() {
  saveFrame("pictures/fractalTree-######.png");
}

1 Like

#15 lotteries! Java mode
image

Code
ArrayList<lottery> Lotteries = new ArrayList<lottery>();
String result = "";
void setup() {
  Lotteries.add(new lottery("result 1,result 2,result 3,result 4,result 5","30,30,30,8,2"));
  textAlign(3,3);
}
void draw() {
  background(0);
  text(result,width/2,height/2);
}
void mousePressed() {
  result = Lotteries.get(0).playLottery();
}



class lottery {
  ArrayList<String> options = new ArrayList<String>(); //options are basically | common, uncommon, rare, epic, legendary, mythical, ...
  ArrayList<Float> chance = new ArrayList<Float>(); //chance in %, linked to <options> ArrayList
  lottery(String options_, String chances_) {
    String opt[] = split(options_,",");
    String chnc[] = split(chances_,",");
    for(int i = 0; i < opt.length; i++) {
      options.add(opt[i]);
      chance.add(float(chnc[i]));
    }
  }
  String playLottery() {
    float startChance = 0, rndNum = random(100);
    for(int i = 0; i < chance.size(); i++) {
      if(rndNum < startChance + chance.get(i)) {
        return options.get(i);
      } else {
        startChance += chance.get(i);
      }
    }
    return options.get(0);
  }
}

#16 bouble sort Java Mode

(contains normal code, short code and explained code)

Code
Normal code
ArrayList<Float> value = new ArrayList<Float>();
int n = 600, rangeMin = 000, rangeMax = 600;
boolean graphic = true;
void setup() {
  size(600, 600);
  for (int i = 0; i < n; i++) value.add(random(rangeMin, rangeMax)); //try using value.add((float)i);
}
void draw() {
  background(0);
  stroke(255);
  for (int i = 0; i < n; i++) line(i, height - value.get(i), i, height);
  if (graphic) for (int i = 0; i < n-frameCount; i++) swap(i, i+1);
  else sortArray();
}
void swap(int a, int b) {
  if (value.get(b) > value.get(a)) {
    float temp = value.get(b);
    value.set(b, (float)value.get(a));
    value.set(a, temp);
  }
}
void sortArray() {
  for (int k = 1; k < n; k++) for (int i = 0; i < n-k; i++) swap(i, i+1);
}

Extra short code
ArrayList<Float> value = new ArrayList<Float>();
int n = 600, rangeMin = 000, rangeMax = 600;
void setup() {
  size(600, 600);
  for (int i = 0; i < n; i++) value.add(random(rangeMin, rangeMax)); //try using value.add((float)i);
}
void draw() {
  background(0);
  stroke(255);
  for (int i = 0; i < n; i++) line(i, height - value.get(i), i, height);
  for (int i = 0; i < n-frameCount; i++) if (value.get(i+1) > value.get(i)) swap(i, i+1);
}
void swap(int a, int b) {
  float temp = value.get(b);
  value.set(b, (float)value.get(a));
  value.set(a, temp);
}

Explained code
ArrayList<Float> value = new ArrayList<Float>();
int n = 600, rangeMin = 000, rangeMax = 600;
boolean graphic = true;
void setup() {
  size(600, 600);
  for (int i = 0; i < n; i++) { 
    value.add(random(rangeMin, rangeMax)); //try using value.add((float)i);
  }
}
void draw() {
  background(0);
  stroke(255);
  for (int i = 0; i < n; i++) { //drawing the array
    line(i, height - value.get(i), i, height);
  }
  if (graphic) { //if graphic it sorts one time per frame else it sorts instantly
    for (int i = 0; i < n-frameCount; i++) {
      swap(i, i+1);
    }
  } else {
    sortArray();
  }
}
void swap(int a, int b) { //swaps two numbers, but only if value a is higher then the value b (value a is values.get(a) and b is the same but with b)
  if (value.get(b) > value.get(a)) {
    float temp = value.get(b);
    value.set(b, (float)value.get(a));
    value.set(a, temp);
  }
}
void sortArray() { //function that sorts values array with bouble sort
  for (int k = 1; k < n; k++) { 
    for (int i = 0; i < n-k; i++) { //the reason behind n - k is because the last number is automatically the lowest number
      swap(i, i+1);
    }
  }
}

#17 Etch a sketch Java mode

Features:

  • press arrow keys / awsd to move
  • has a crosshair to help aligning
  • use backspace to erase last
  • use space to erase all
  • use mouseWheel to change step size
  • press t to toggle size circle (displays the step size)
Code
ArrayList<PVector> point = new ArrayList<PVector>();
float x = 300, y = 300, stepSize = 2;
boolean showSize = false, hideSize = true;
void setup() {
  size(600, 600);
  point.add(new PVector(x, y, frameCount));
}
void draw() {
  background(0);
  stroke(0, 255, 0, 50);
  line(x, 0, x, height);
  line(0, y, width, y);
  fill(0, 0, 255, 50);
  if (showSize) circle(x, y, stepSize*2);
  drawTrack();
}
void keyPressed() {
  if (key == 'a' || keyCode == LEFT) x-=stepSize;
  else if (key == 'd' || keyCode == RIGHT)
    x+=stepSize;
  else if (key == 'w' || keyCode == UP)
    y-=stepSize;
  else if (key == 's' || keyCode == DOWN) 
    y+=stepSize;
  if (keyCode == BACKSPACE && point.size() > 0) {
    point.remove(point.size()-1);
    x = point.get(point.size()-1).x;
    y = point.get(point.size()-1).y;
  } else if (key == 't') hideSize = !hideSize;
  else if(key == ' ') point.clear();
  else point.add(new PVector(x, y, frameCount));
  showSize = !hideSize;
}
void mouseWheel(MouseEvent e) {
  stepSize = abs(stepSize - e.getAmount() );
  showSize = true;
}

void drawTrack() {
  stroke(255);
  for (int i = 0; i < point.size()-1; i++) {
    line(point.get(i).x, point.get(i).y, point.get(i+1).x, point.get(i+1).y);
  }
}

#18 X in row (flexible 4inRow) Java mode
A flexible 4 in row program! Features:

  • how many in row to win (default 4)
  • customizable width and height (default 7*6)
  • current turn display: outline (red if red is next, blue if blue is next)
  • auto check winner: displays a green outline if someone wins and draws a line to show
Code

55 lines!

int w = 7, h = 6, turn = 1, scl=100, inRow = 4;
int grid[][]= new int[w][h];
color clr[] = {color(255, 0, 0), color(0, 0, 255), color(0, 255, 0)}; //first is p1, second p2, third winClr
void setup() {
  size(700, 600);
}
void draw() {
  background(0);
  noFill();
  stroke( ((checkWin() != 0)? clr[2] : ((turn == -1)? clr[1] : clr[0])));
  strokeWeight(5);
  rect(0, 0, width, height);
  strokeWeight(0);
  for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
    fill( ((grid[i][j] == 1)? clr[0] : clr[1]));
    if (grid[i][j] != 0) circle((i+0.5)*scl, (j+0.5)*scl, scl);
  }
  if (checkWin() != 0) noLoop();
}
void mousePressed() {
  int mx = constrain(floor(map(mouseX, 0, width, 0, w)), 0, w);
  for (int i = h-1; i >= 0; i--) if (grid[mx][i] == 0) {
    grid[mx][i] = turn;
    turn *= -1;
    break;
  }
}
int checkWin() {
  for (int i = 0; i < w+1-inRow; i++) for (int j = 0; j < h; j++) if (equal(i, j, 1, 0, inRow) && grid[i][j] != 0) {
    drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+inRow-0.5)*scl, (j+0.5)*scl);
    return grid[i][j];
  }
  for (int i = 0; i < w; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, 0, 1, inRow) && grid[i][j] != 0) {
    drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+0.5)*scl, (j+inRow-0.5)*scl);
    return grid[i][j];
  }
  for (int i = 0; i < w+1-inRow; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, 1, 1, inRow) && grid[i][j] != 0) {
    drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+inRow-0.5)*scl, (j+inRow-0.5)*scl);
    return grid[i][j];
  }
  for (int i = inRow-1; i < w; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, -1, 1, inRow) && grid[i][j] != 0) {
    drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i-(inRow-1.5))*scl, (j+inRow-0.5)*scl);
    return grid[i][j];
  }
  return 0;
}
boolean equal(int x, int y, int xs, int ys, int inrow) {
  for (int i = 0; i < inrow; i++) if (grid[x][y] != grid[x+xs*i][y+ys*i]) return false;
  return true;
}
void drawWinLine(float x, float y, float x2, float y2) {
  stroke(0, 255, 0);
  strokeWeight(scl/4);
  line(x, y, x2, y2);
}

VERSION 2 Java mode
Can work as tictactoe, just disable gravity (boolean gravity = false) and set w,h and inRow to 3
image (settings are set to tictactoe)

Version 2
int w = 3, h = 3, turn = 1, scl=100, inRow = 3;
int grid[][]= new int[w][h];
color clr[] = {color(255, 0, 0), color(0, 0, 255), color(0, 255, 0)}; //first is p1, second p2, third winClr
boolean gravity = !false;
void setup() {
  size(300, 300);
}
void draw() {
  background(0);
  noFill();
  stroke( ((checkWin() != 0)? clr[2] : ((turn == -1)? clr[1] : clr[0])));
  strokeWeight(5);
  rect(0, 0, width, height);
  strokeWeight(0);
  for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
    fill( ((grid[i][j] == 1)? clr[0] : clr[1]));
    if (grid[i][j] != 0) circle((i+0.5)*scl, (j+0.5)*scl, scl);
  }
  if (checkWin() != 0) noLoop();
}
void mousePressed() {
  int mx = constrain(floor(map(mouseX, 0, width, 0, w)), 0, w), my = constrain(floor(map(mouseY, 0, height, 0, h)), 0, h);
  for (int i = h-1; i >= 0 && !gravity; i--) if (grid[mx][i] == 0) {
    grid[mx][i] = turn;
    turn *= -1;
    break;
  }
  if(gravity && grid[mx][my] == 0) {
    grid[mx][my] = turn;
    turn *= -1;
  }
}
int checkWin() {
  for (int i = 0; i < w+1-inRow; i++) for (int j = 0; j < h; j++) if (equal(i, j, 1, 0, inRow) && grid[i][j] != 0) {
    drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+inRow-0.5)*scl, (j+0.5)*scl);
    return grid[i][j];
  }
  for (int i = 0; i < w; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, 0, 1, inRow) && grid[i][j] != 0) {
    drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+0.5)*scl, (j+inRow-0.5)*scl);
    return grid[i][j];
  }
  for (int i = 0; i < w+1-inRow; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, 1, 1, inRow) && grid[i][j] != 0) {
    drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+inRow-0.5)*scl, (j+inRow-0.5)*scl);
    return grid[i][j];
  }
  for (int i = inRow-1; i < w; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, -1, 1, inRow) && grid[i][j] != 0) {
    drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i-(inRow-1.5))*scl, (j+inRow-0.5)*scl);
    return grid[i][j];
  }
  return 0;
}
boolean equal(int x, int y, int xs, int ys, int inrow) {
  for (int i = 0; i < inrow; i++) if (grid[x][y] != grid[x+xs*i][y+ys*i]) return false;
  return true;
}
void drawWinLine(float x, float y, float x2, float y2) {
  stroke(0, 255, 0);
  strokeWeight(scl/4);
  line(x, y, x2, y2);
}
1 Like

#19 Infinite triangles Java mode
press any key to create next step : P

Code
ArrayList<baseLine> base = new ArrayList<baseLine>();
int it = 0,n=3,rr=50,cx=300,cy=300;
void setup() {
  size(600,600);
  //base.add(new baseLine(0,100,500,0,400));
  stroke(255);
  for(int i = 0; i < n; i++) {
    PVector a = raycast(cx,cy,TWO_PI/n*(i+0.5),rr);
    line(a.x,a.y,cx,cy);
    float x = a.x, y = a.y;
    base.add(new baseLine(0,x,y,TWO_PI/n*(i+1),rr*3));
  }
}
void draw() {
  background(0);  
  for(int i = 0; i < base.size(); i++) {
    base.get(i).display();
  }
}
void keyPressed() {
  splitBase();
}
void splitBase() {
  int a = base.size();
  for(int i = 0; i < a; i++) {
    if(base.get(i).iter == it) base.get(i).addNew();
  }
  it++;
}
class baseLine {
  int iter;
  float x, y, dir, r;
  baseLine(int iter_, float x_, float y_, float dir_, float r_) {
    x = x_;
    y = y_;
    dir = dir_;
    r = r_;
    iter = iter_;
  }
  void display() {
    stroke(255);
    PVector info = raycast(x,y,dir,r);
    float x2 = info.x, y2 = info.y;
    line(x,y,x2,y2);
  }
  void addNew() {
    PVector info = raycast(x,y,dir,r/3);
    base.add(new baseLine(iter+1,info.x, info.y, dir - TWO_PI/6, r/3));
    PVector info2 = raycast(info.x,info.y,dir-TWO_PI/6,r/3);
    base.add(new baseLine(iter+1,info2.x, info2.y, dir - TWO_PI/6 + TWO_PI/3, r/3));

  }
  
}
PVector raycast(float x, float y, float dir, float r) {
  return( new PVector( x + cos(dir)*r, y + sin(dir) * r));
}
1 Like

this thread is discontinued due to limited amounts of edits;
link to the next thread:
Project libary #2

Third library!
Project library #3

Fourth library!
Project library #4

Fifth library!
Project library #5