Computing a polygon that surrounds a multi-point line

隐身守侯 提交于 2019-12-03 14:45:31

As I see, this problem is similar to polygon buffering problem.

I think following approach can help you:

  • For each segment of your track, find two lines - one to the left and one to the right.
  • Then, iterate for over your ofsetted lines, and resolve intersections. For example: http://img25.imageshack.us/img25/7660/temprhk.png
  • Add caps to ends, and you're done! :)

And some code:

Moving a line to the left:

Line2D l; 
double indent; // distance from central line
double dx = ln.getX2() - ln.getX1();
double dy = ln.getY2() - ln.getY1();
double length = ln.getP1().distance(ln.getP2());
double newX1 = l.getX1() - indent*(dy/length);
double newY1 = l.getY1() + indent*(dx/length);
double newX2 = l.getX2() - indent*(dy/length);
double newY2 = l.getY2() + indent*(dx/length);
Line2D leftLine = new Line2D.Double(newX1, newY1, newX2, newY2);

For moving it to the right, change "+" to "-" and vice versa in the last 4 lines of code.

About working with intersections - if two line segment intersect, you just output the intersection point. If they do not, then situation is a bit more complicated - you can, of course, still output the intersection, but in case of rapidly turning track, there will be strange outbursts. I inserted an arc segment in similar situation, but the code is to big and scattered, so I can't paste it here.
Or, you can do as you show on your picture - just connect end points.


And, by the way, if speed is not a big issue, you can use even better way - for each line of track, find left and right lines, add caps, pack it all into Path2D, then create Area from Path2D.
In such case, you can make this "line with caps" as intersection of three areas: rectangle, whose points are just end points of right and left line, and two circles with centers on original track segment ends.

When you compute Areas for all lines, just intersect them using Area add() method.

This approach deals with just any situations, even self-intersections and breaks in the track.

xan

See my answer to a similar question, "How to draw an outline around any line."

Same idea as Rogach provides here, but perhaps different drawings and explanations will help clarify it.

If you don't want to write the code for the buffering as described by Rogach, JTS could do the magic for you. See the developer guide for a quick introduction.

Half-baked suggestion: Calculate the normal to each segment. Then, for each vertex V_i, interpolate the normals from its adjacent segments to get n_i (normalise it again) and add two vertices at V_i +/- a*n_i where a is some scaling factor.

If you join these points, you won't get exactly your blue polygon, but it might be good enough.

You may have to keep track of which "side" the new vertices are on. If you can close the curve without self-intersections this just becomes a point in polygon test for each vertex.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!