r/processing • • Mar 01 '20

Circle draws Sin Waves - interesting program I made

177 Upvotes

10 comments sorted by

13

u/liberal_destroyer3 Mar 01 '20

I can paste code if anyone wants it

5

u/PennyStockPanda Mar 01 '20

just do a tutorial like the coding train haha

3

u/dillibilli26 Mar 01 '20

hey, I'd really appreciate if you could share the code this looks super cool!

1

u/FlanRT Mar 01 '20

I want it.

6

u/liberal_destroyer3 Mar 01 '20

Here is code (A bit long)

Main tab:

ArrayList<PVector> downWave =new ArrayList<PVector>();
ArrayList<PVector> rightWave =new ArrayList<PVector>();
slider s1;
slider s2;
slider s3;
float angle;
float r=10;
PVector p = new PVector(r, 0);
PVector c;
float angleSpeed=0.05;
float lineLength=150;
float waveSpeed=1;

void setup() {
  size(800,800,FX2D);
  pixelDensity(2);
  c= new PVector(150, 150);
  s1=new slider(new PVector(350,400),400,0,150);
  s2=new slider(new PVector(350,500),400,0,0.7,0.1);
  s3=new slider(new PVector(350,600),400,0,10,1);
}
void draw() {
  background(200);
  doSliders();
  shiftWaves();
  checkOffScreen();
  rotations();
  addPoints();
  drawLines();
  drawCircle();
  drawWaves();
}

Tab with all of the functions:

void drawCircle() {
  noFill();
  stroke(0);
  strokeWeight(2);
  ellipse(c.x, c.y, r*2, r*2);
  noStroke();
  fill(255, 0, 0);
  ellipse(p.x+c.x, p.y+c.y, 10, 10);
  ellipse(c.x+lineLength, p.y+c.y, 10, 10);
  ellipse(p.x+c.x, c.y+lineLength, 10, 10);
}
void rotations() {
  angle+=angleSpeed;
  p.x=cos(angle)*r;
  p.y=sin(angle)*r;
}
void drawLines() {
  stroke(127, 100);
  strokeWeight(3);
  line(c.x+lineLength, 0, c.x+lineLength, c.y+lineLength);
  line(0, c.y+lineLength, c.y+lineLength, c.x+lineLength);
  line(c.x+p.x, 0, p.x+c.x, c.y+lineLength);
  line(0, c.y+p.y, c.x+lineLength, p.y+c.y);
}
void addPoints() {
  rightWave.add(new PVector(c.x+lineLength, p.y+c.y));
  downWave.add(new PVector(p.x+c.x, c.y+lineLength));
}
void shiftWaves() {
  ArrayList<PVector> downWaveTemp =new ArrayList<PVector>();
  ArrayList<PVector> rightWaveTemp =new ArrayList<PVector>();
  for (int i=0; i<downWave.size(); i++) {
    PVector part1=downWave.get(i);
    PVector part2=rightWave.get(i);
    downWaveTemp.add(part1);
    rightWaveTemp.add(part2);
  }
  for (int i = downWave.size() - 1; i >= 0; i--) {
    downWave.remove(i);
    rightWave.remove(i);
  }
  for (int i=0; i<downWaveTemp.size(); i++) {
    PVector part1=downWaveTemp.get(i);
    PVector part2=rightWaveTemp.get(i);
    rightWave.add(new PVector(part2.x+waveSpeed, part2.y));
    downWave.add(new PVector(part1.x, part1.y+waveSpeed));
  }
}
void checkOffScreen() {
  for (int i = downWave.size() - 1; i >= 0; i--) {
    PVector p1=downWave.get(i);
    if(p1.y>height) {
      downWave.remove(i);
      rightWave.remove(i);
    }
  }
}
void drawWaves() {
  connect(downWave);
  connect(rightWave);
}
void connect(ArrayList<PVector> point) {
  for (int i=0; i<point.size()-2; i++) {
    stroke(0, 255);
    int j=i+1;
    PVector john=point.get(i);
    PVector smith=point.get(j);
    line(john.x, john.y, smith.x, smith.y);
  }
}
void doSliders() {
  s1.go();
  s2.go();
  s3.go();
  r=s1.getSlider();
  angleSpeed=pow(s2.getSlider(),1);
  waveSpeed=s3.getSlider();
  
}

Slider class:

class slider {
  color Color=color(220);
  color sliderColor=color(200);
  color highlightColor=color(190);
  color heldColor=color(170);
  float r=9;
//—————————————————————————————————————————————————————————————————

  PVector pos;
  float len;
  float startValue;
  float endValue;
  float sliderPos;
  boolean highlight=false;
  boolean held=false;
  slider(PVector pos, float len, float startValue, float endValue) {
    this.pos = pos;
    this.len = len;
    this.startValue = startValue;
    this.endValue = endValue;
    sliderPos=(pos.x+len+pos.x)/2;
    println(sliderPos);
  }
  slider(PVector pos, float len, float startValue, float endValue, float startPos) {
    this.pos = pos;
    this.len = len;
    this.startValue = startValue;
    this.endValue = endValue;
    float x = map(startPos, startValue, endValue, pos.x, pos.x+len);
    sliderPos=x;
  }
  float getSlider() {
    float x = map(sliderPos, pos.x, pos.x+len, startValue, endValue);
    return x;
  }
  void go() {
    update();
    display();
  }
  void update() {
    if(held) {
      cursor(HAND);
      sliderPos=mouseX; 
      sliderPos=constrain(sliderPos,pos.x,pos.x+len);
    } else if(highlight) {
      cursor(HAND);
    } else {
      cursor(ARROW);
    }
    
  }
  void display() {
    strokeWeight(1);
    int b=5;
    float rt;
    fill(Color);
    rect(pos.x-b, pos.y-b,len+b*2, b*2,6);
    if(held) {
     fill(heldColor);
     rt=r+2;
    } else if(highlight) {
      fill(highlightColor);
      rt=r+2;
    } else {
      fill(sliderColor);
      rt=r;
    }
    println(sliderPos);
    circle(sliderPos, pos.y, rt*2);
  }
  boolean mouseOver() {
    return dist(mouseX,mouseY,sliderPos,pos.y)<r;
  }
  void checkHighlight() {
    if(mouseOver()) {
      highlight=true;
    } else {
      highlight=false;
    }
  }
  void checkPressed() {
    if(mouseOver()) {
      held=true;
    } else {
      held=false;
    }
  }
}

Mouse functions for sliders

void mouseMoved() {
 s1.checkHighlight();
 s2.checkHighlight();
 s3.checkHighlight();
}
void mousePressed() {
 s1.checkPressed();
 s2.checkPressed();
 s3.checkPressed();
}
void mouseReleased() {
 s1.held=false;
 s1.checkHighlight();
 s2.held=false;
 s2.checkHighlight();
 s3.held=false;
 s3.checkHighlight();
}

3

u/Simplyfire Mar 02 '20

I really like your animation, but I would like to comment on the code.

There's a convention in java that class names should be capitalized so it should be Slider rather than slider, it makes it more readable for others that are already used to it. And the function getSlider could probably be named getValue or something like that, you aren't really getting a slider object out of it, you're getting the current value the slider is pointing at.

1

u/Volfegan Mar 03 '20

The slider class you created is really well done.

6

u/bluegre3n Mar 01 '20

That's cool. There's a Eurorack synth oscillator module called Polygogo that uses that idea to generate waveforms.

Have you considered extending your sketch to be a synth?

1

u/liberal_destroyer3 Mar 01 '20

Not sure what you mean

3

u/Pixelator2033 Mar 02 '20

In eurorack synthesiser format, there's oscillator called polygogo that generates sound from geometrical shapes in the same way. Check out r/modular