My projects library #2! - CodeMasterX's gallery megathread

#34 Triangular spiral Java mode


My 10th attempt at this.

Code
ArrayList<tri> tris = new ArrayList<tri>();
float lrp = 0.6;
void setup() {
  size(600,600);
  tris.add(new tri(100,500,300,500));
}
void draw() {
  background(0);
  for(int i = 0; i < tris.size(); i++) tris.get(i).show();
}

class tri {
  float x1,y1,x2,y2,x3,y3,x4,y4;
  boolean last=true;
  tri(float x1_, float y1_, float x2_, float y2_) {
    x1 = x1_; y1 = y1_; x2 = x2_; y2 = y2_;
    float dir = atan2(y2-y1,x2-x1)-PI/2, r = dist(x1,y1,x2,y2);
    x3 = x1 + cos(dir)*r;
    y3 = y1 + sin(dir)*r;
    x4 = lerp(x3,x2,lrp);
    y4 = lerp(y3,y2,lrp);
  }
  void show() {
    display(x1,y1,x2,y2);
  }
  void dupe() {
    if(last) tris.add(new tri(x3,y3,x4,y4));
    last=false;
  }
}
void keyPressed() { if(key == ' ') for(int i = 0, m = tris.size(); i < m; i++) tris.get(i).dupe(); }
void display(float x1, float y1, float x2, float y2) {
  float dir = atan2(y2-y1,x2-x1)-PI/2, r = dist(x1,y1,x2,y2);
  float x3 = x1 + cos(dir)*r,y3 = y1 + sin(dir)*r;
  triangle(x1,y1,x2,y2,x3,y3);
}

#34.5 Triangular spiral - interactive Java mode


version 2 - interactive. You can draw the first rectangle. You can also set the lrp variable mid game

Version 2
float lrp = 0.55, iter = 15;
float sx = -1, sy=-1, ex=-1, ey=-1;
boolean done = false, started = false, outOfBounds = false;
void setup() {
  size(600, 650);
  textAlign(3,3);
}
void draw() {
  background(0);
  if (done) display(sx, sy, ex, ey, iter);
  else if (started) display(sx, sy, mouseX, mouseY, iter);
  rect(0, 600, width, 50);
  fill(0);
  text(lrp,map(lrp,0,1,0,width),625);
  if(mousePressed && mouseY > 600) lrp = map(mouseX,0,width,0,1);
  fill(255); 
}
void mousePressed() {
  if (mouseY < 600) {
    sx=mouseX;
    sy=mouseY;
    done=false;
    started = true;
    outOfBounds = false;
  } else outOfBounds = true;
}
void mouseReleased() {
  if (!outOfBounds) {
    ex=mouseX;
    ey=mouseY;
    done=true;
  }
}
void display(float x1, float y1, float x2, float y2, float it) {
  float dir = atan2(y2-y1, x2-x1)-PI/2, r = dist(x1, y1, x2, y2);
  float x3 = x1 + cos(dir)*r, y3 = y1 + sin(dir)*r;
  triangle(x1, y1, x2, y2, x3, y3);
  float x4 = lerp(x3, x2, lrp);
  float y4 = lerp(y3, y2, lrp);
  if (it > 0) display(x3, y3, x4, y4, it-1);
}

2 Likes

This is the third library
Project Library #3

1 Like

Fourth library is out!
Project library #4

Fifth library is here!
Project library #5