Trying to do something like a cat's cradle but can't changed the string's position.
in
Contributed Library Questions
•
11 months ago
Hi, every one, I am a novice in processing and I encounter a problem. There are three strings in this sketch, I can place the strings in same positions when clicked the mouse
. But I want to place three strings in different positions. How can I do this?
Can anyone help me?
Thanks!!!
This code is written by Karl Hiner. And I recreate some effects.
/* OpenProcessing Tweak of *@*
http://www.openprocessing.org/sketch/42428*@* */
/* !do not delete the line above, required for linking your tweak if you re-upload */
/**********************************************************************
Pluck the string by holding the mouse down and releasing.
Adjust the speed, damping, and number of harmonics of the simulation.
The equation for this simulation is due to:
***********************************************************************/
import controlP5.*;
ControlP5 controlP5;
ControlP5 controlP52;
final float PI_SQUARED = PI*PI;
boolean plucked = false;
int t = 0; // time since pluck
int m_tot = 10; // harmonics - each harmonic requires more computation
float c = 100; // speed
float amp = 0; // amplitude
float d; // x position of pluck
float damp = 1.1; // damping constant
void setup() {
size(800, 600, P2D);
strokeWeight(4);
smooth();
controlP5 = new ControlP5(this);
controlP5.setColorLabel(0);
controlP5.addSlider("damp", 1, 1.35, 1.1, 5, 10, 80, 15);
controlP5.addSlider("c", 20, 300, 100, 5, 30, 80, 15).setLabel("speed");
controlP5.addSlider("m_tot", 1, 15, 10, 5, 50, 80, 15).setLabel("harmonics");
}
void draw_a_string(float y_pos) {
if (plucked) {
t++;
amp/= damp;
}
float w = PI*(c/width);
beginShape();
if (plucked) {
for (int x = 0; x < width; x += 5) {
float sum = 0;
for (int m = 1; m <= m_tot; ++m)
sum += (1.0/(m*m)*sin((m*PI_SQUARED*d)/width)*sin((m*PI*x)/width)*cos(m*w*t));
float y = (1.5*amp*width*width)/(PI_SQUARED*d*(width - d))*sum;
y += y_pos;
vertex(x, y);
}
}
else {
vertex(0, y_pos);
vertex(d, y_pos + amp);
vertex(width - 5, y_pos);
}
endShape();
}
void draw() {
noFill(); //or it will look like triangle
background(0);
//set the string color
stroke(255, 30, 56);
//draw a string. Parameter is the vertical height that the string is drawn.
draw_a_string(height * 0.25);
//set the string color
stroke(30, 255, 56);
//draw a string. Parameter is the vertical height that the string is drawn.
draw_a_string(height * 0.5);
//set the string color
stroke(30, 56, 255);
//draw a string. Parameter is the vertical height that the string is drawn.
draw_a_string(height * 0.75);
controlP5.draw(); // we need to manually draw controlP5 when using P2D
}
void mousePressed() {
if (!controlP5.window().isMouseOver()) {
plucked =false ;
t = 0;
amp = mouseY - height/2;
d = mouseX;
}
}
void mouseDragged() { // when mouse are drugged
if (!controlP5.window().isMouseOver()) {
d = mouseX;
amp = mouseY - height/2;
}
}
void mouseReleased() {
plucked = false;
//reset the string back to 0
//amp = 0;
}
void damp(float value) {
damp = value;
}
void c(float value) {
c = value;
}
void m_tot(float value) {
m_tot = (int)value;
}
1