My projects library! - CodeMasterX's gallery megathread

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

#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