Loading...
Logo
Processing Forum
Hey everybody,

i'm pretty new to processing. I try to draw a sine curve between two points. That would be the first step. Afterwards i would like to dynamically change frequency and amplitude of the sine curve.

Lets say, i have two Points P(200, 100) and Q(300, 600). How can i connect these two point with a sine curve??
The position of the point will not stay constant. They will change with user interaction.

If anyone can help me, would be really nice. I'm getting crazy with this. I hope the attached image illustrates what im trying to achieve.


Replies(3)

Do you want 

a) a curve which is mathematically as close to a sine wave as you can get  (or)

b) a curve which resembles a sine wave, but is merely approximate.

if b, there are fairly efficient ways to do this using bezier curves.  if a, we can do it by plotting an actual sine wave, but it's
less efficient.
See this sketch.  You can move the points, and change the frequency by typing numbers. It supports variable amplitude as well, although I haven't made any UI for controlling it.

Copy code
  1. PVector p1 = new PVector(10,10);
  2. PVector p2 = new PVector(100,100);

  3. float gFreq = 2; // frequency
  4. float gAmp = 50; // amplitude in pixels

  5. void setup()
  6. {
  7.   size(500,500);
  8.   smooth();
  9. }

  10. void sineTo(PVector p1, PVector p2, float freq, float amp)
  11. {
  12.   float d = PVector.dist(p1,p2);
  13.   float a = atan2(p2.y-p1.y,p2.x-p1.x);
  14.   noFill();
  15.   pushMatrix();
  16.     translate(p1.x,p1.y);
  17.     rotate(a);
  18.     beginShape();
  19.       for (float i = 0; i <= d; i += 1) {
  20.         vertex(i,sin(i*TWO_PI*freq/d)*amp);
  21.       }
  22.     endShape();
  23.   popMatrix();
  24. }

  25. void draw()
  26. {
  27.   background(255);
  28.   sineTo(p1,p2,gFreq,gAmp);
  29.   fill(255,255,0);
  30.   ellipse(p1.x,p1.y,20,20);
  31.   ellipse(p2.x,p2.y,20,20);

  32.   if (mousePressed) {
  33.       if (dist(p1.x,p1.y,pmouseX,pmouseY) < 10) {
  34.         p1.x += mouseX - pmouseX;
  35.         p1.y += mouseY - pmouseY;
  36.       }  else if (dist(p2.x,p2.y,pmouseX,pmouseY) < 10) {
  37.         p2.x += mouseX - pmouseX;
  38.         p2.y += mouseY - pmouseY;
  39.       }
  40.   }
  41. }

  42. void keyPressed() {
  43.   if (key >= '1' && key <= '9') {
  44.     gFreq = key - '0';
  45.   }
  46. }

Great work, thanks a lot! And so easy! Nice!