Algorithm to Fill In a Closed 2D Curve [closed]

懵懂的女人 提交于 2021-02-07 03:46:27

问题


I need to find a way of drawing the inside of a closed 2D curve. This curve is actually created using a bicubic Bezier curve, but that's not important I believe.

At the moment there should be no "holes" within the drawn shape. So it will just be totally filled in. It seems like constrained Delaunay triangulation would be the way to go? But there seems to be different ways of doing this. I am looking for a quick and simple solution (but will implement what's needed to make it working).

Programs such as Illustrator have that sort of feature (or SVG -- with the option fill).

I am looking for:

  • techniques to do that
  • point me to a paper/document where the algorithm is explained
  • is the source code of a SVG renderer available somewhere?

EDIT:

  • The application uses OpenGL. I draw the curves myself. Just need to find a way of filling them in.
  • the shape can either be concave or convex

回答1:


Polygons can be filled using the Scanline method. The principle is easy: move an horizontal line and keep a list of the edges it meets. It is called the active list. Then join the intersections from left to right, in pairs. When the edges are sorted by increasing ordinate, the update of the active list from one scanline to the next can be done efficiently.

This works with concave/convex polygons and polygons with holes, and even crossed ones.

To fill a Bezier path, you can flatten it, i.e. turn it to a polygon of many sides.

A direct approach is also possible, based on the scanline idea: first decompose the Bezier curves in monotone sections, i.e. portions that meet on horizontal line only once. This can be done analytically for cubic Beziers by detecting the curve maxima and minima (the equation is quadratic).

Now you can treat the curvilinear polygon exactly as a polygon, knowing that you have one intersection per side. There is a slightly delicate point, computing the intersection. But this is eased by the fact that you know a good approximation of the Bezier arc (the line segment between the same endpoints), and you can update the intersection incrementally, from one scanline to the next.

On the picture, the original endpoints appear in blue. Splitting endoints have been added to obtain monotone sections (the other control points are omitted). The dotted lines shows the polygon that approximates the shape and has the same topology (same active list, same number of intersections with the scanlines).

enter image description here




回答2:


If you must use polygon filling, there is no other option than flattening the curve to get straight sides.

Then use a polygon filling primitive.

If all you have is a triangle filling primitive, you can

  • triangulate the polygon by ear clipping, or decomposition in monotone polygons, or

  • use a simple sweepline method: if you draw an horizontal through every vertex, you will slice the polygon in triangles and trapezoids. A trapezoid can be cut in two triangles. For efficiency, use the active list method.



来源:https://stackoverflow.com/questions/25964313/algorithm-to-fill-in-a-closed-2d-curve

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