We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Controlling the tension of a closed curve
Page Index Toggle Pages: 1
Controlling the tension of a closed curve (Read 688 times)
Controlling the tension of a closed curve
Nov 20th, 2009, 10:01am
 
I am attempting to create a closed curve from a set of points (ie a complete loop with no obvious start/end point). I can do this easily enough using curveVertex.

However I would like to be able to control the tension of the curve. ie. be able to vary it from a 'loose' ellipse type shape to a 'tight' linear mapping with sharp corners at vertices.

I guess I could probably do something with bezier curves with intelligent placement of the control points, but I am not sure how I would be able to relate this to tension in any obvious way.

Any thoughts?


In case anyone's not sure what I mean, the following sketch allows you to add points with a mouse click. Once at least 3 points are clicked, a closed curve is drawn through them.

Quote:
// Click to add a point and shift-drag mouse to move a point.

private ArrayList coords;
private boolean isShiftDown;
private PVector pClosest;

void setup()
{
  coords = new ArrayList();
  isShiftDown = false;

  size(400,400);
  smooth();
  fill(50,0,0,20);  
}

void draw()
{
  background(255);
  
  // Draw the boundary using Processing's curve method.
  if (coords.size() >= 3)
  {
    strokeWeight(2);
    stroke(100,0,0);
  
    beginShape();
    for (Iterator i = coords.iterator(); i.hasNext();)
    {
      PVector p = (PVector)i.next();
      curveVertex(p.x,p.y);
    }        
    
    // We need to overlap with the first three points to close the boundary.
    for (int i=0; i<3; i++)
    {
      PVector p = (PVector)coords.get(i);
      curveVertex(p.x,p.y);
    }
    endShape();
  }
  
  // Draw the original points.
  strokeWeight(5);
  stroke(50,0,0,100);
  
  beginShape(POINTS);
  for (Iterator i=coords.iterator(); i.hasNext();)
  {
    PVector p = (PVector)i.next();
    vertex(p.x,p.y);
  }
  endShape();
  
}

void mouseClicked()
{
  if (!isShiftDown)
  {
    coords.add(new PVector(mouseX,mouseY));
  }
}

void mousePressed()
{
  if (isShiftDown)
  {
    float dMin = Float.MAX_VALUE;
    pClosest = null;
    PVector mouse = new PVector(mouseX,mouseY);
    
    for (Iterator i=coords.iterator(); i.hasNext();)
    {
      PVector p = (PVector)i.next();    
      float d = mouse.dist(p);
      if (d < dMin)
      {
        dMin = d;
        if (dMin < 20)
        {
          pClosest = p;
        }
      }
    }
  } 
}

void mouseDragged()
{
  if ((pClosest != null) && (isShiftDown))
  {
     pClosest.x = mouseX;
     pClosest.y = mouseY;
  }
}

void keyPressed()
{
  if ((key==CODED) && (keyCode == SHIFT))
  {
    isShiftDown = true;
  }
}

void keyReleased()
{
  if ((key==CODED) && (keyCode == SHIFT))
  {
    isShiftDown = false;
  }
}
Re: Controlling the tension of a closed curve
Reply #1 - Nov 20th, 2009, 2:52pm
 
Try this:

http://processing.org/reference/curveTightness_.html

Re: Controlling the tension of a closed curve
Reply #2 - Nov 20th, 2009, 3:23pm
 
Perfect - thanks. I've no idea how I managed to miss that. I've been using curves for ages, but never noticed the tightness method.

Cheers.

Jo.
Page Index Toggle Pages: 1