views:

79

answers:

2

Hi everyone

I am making a graphics application in which I can edit a polyline by dragging the control point of it. However, I'd like to make it a bit easier to use by making it elastic; When dragging a control point, instead of moving a single point, I'd like the points within a certain distance of that point to be moved as well, depending on how hard the control point is 'pulled'.

Does anyone know a simple algorithm for this? It may be quite rudimentary, as the primary requirement is speed.

Actually, knowing how to call such behaviour would also be nice, so I can look it up on google. I tried 'snaking' line, but that seems to refer to active contours, which isn't what I'm looking for.

Thanks

+4  A: 

At a simple level you could achieve this with a little help form Hooke's law. You can basically view your polyline as a string, made up of a load of vertices connected by springs:

o-o-o-o-o-o-o-o-o

Each vertex is connected to another vertex by a spring, which will contract if stretched and repel if squashed.

So, when a control point is moved, the connected springs will either expand (stretch) or contract (shrink). This, in turn applies a force to any vertex sharing that spring. So if I pulled the first vertex up and left, the spring would apply a force to the vertex to the right, drawing it closer. This continues to the next (with some energy dissipating) until all springs are 'comfortable'.

That's the basics of it, each time a control point moves you need to solve the equation to all vertices/springs and the points will 'snake' for you.

If you want further examples google for 'Rope Physics' or 'Cloth physics' (as a rope is a 1D cloth). Ignore gravity for your purposes though, obviously.

davbryn
Thanks a lot, that is exactly what I needed.
Aegir
@vhdirk/davbryn, I doubt that this is what you need; hooke's law does not model how the control points of polyline would move, nor would you be able to even establish a set of equations to solve without further conditions (which would then determine the actual behaviour). Rope physics and cloth physics is also much more about non-stretchable materials in a gravity field and/or with external forces applied (more about shape it forms) and (imo) is much too complicated.
Unreason
+2  A: 

Basically you are looking for a method to move (deform/transform) multiple points.

Let's assume you have given the direction and strength of the move which would result in dx, dy for the point x, y.

Your transformation will at least two more parameters

  1. The radius r in which the points will be affected
  2. Since the points in the middle would be affected more and at the edge you should define how to interpolate the falloff (linear, normal distribution, etc...)

For linear interpolations the points that are affected would move according to the following formula:

r[i] = sqrt(sqr(x-x[i])+sqr(y-y[i]))

so if r[i] < r

x[i]' = x[i] + dx*(1-r[i]/r) y[i]' = y[i] + dy*(1-r[i]/r)

this is for linear interpolation dx[i] = dx - r[i]/r

Unreason
Thanks, after reading about this rope physics, a came to the conclusion as you did; that clearly wasn't what I was looking for :)However, the method you suggested is spot on. I implemented it with a variable radius (twice the strength of the move), which seems to work fairly wel.
Aegir