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;
}
}