开发者

elastic / snaking line algorithm

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 'sna开发者_运维知识库king' line, but that seems to refer to active contours, which isn't what I'm looking for.

Thanks


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜