在python中使用Clipper-lib生成多边形偏移

2024-05-16 22:48:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用Clipper lib(http://www.angusj.com/delphi/clipper.php)在闭合多边形中生成偏移量。在

因为我使用的是python2.7,所以我使用pyclipper(https://pypi.python.org/pypi/pyclipper)来做同样的事情。在

遗憾的是,我不能理解C++中的剪刀的多边形偏移例子:

 #include "clipper.hpp"  
    ...
    using namespace ClipperLib;

    int main()
    {
      Path subj;
      Paths solution;
      subj << 
        IntPoint(348,257) << IntPoint(364,148) << IntPoint(362,148) << 
        IntPoint(326,241) << IntPoint(295,219) << IntPoint(258,88) << 
        IntPoint(440,129) << IntPoint(370,196) << IntPoint(372,275);
      ClipperOffset co;
      co.AddPath(subj, jtRound, etClosedPolygon);
      co.Execute(solution, -7.0);

      //draw solution ...
      DrawPolygons(solution, 0x4000FF00, 0xFF009900);
    }

在python中实现。在

我只看到pyclipper的一个示例(剪裁,而不是偏移):

^{pr2}$

不幸的是,作为一个没有经验的程序员,我无法前进。在

请在这方面帮助我。在

提前谢谢。在


Tags: pypicomhttplibwww多边形solutionco
1条回答
网友
1楼 · 发布于 2024-05-16 22:48:50

在pyclipper中也是这样:

subj = ((348, 257), (364, 148), (362, 148), (326, 241), (295, 219), (258, 88), (440, 129), (370, 196), (372, 275))

pco = pyclipper.PyclipperOffset()
pco.AddPath(subj, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
pco.Execute(-7.0)

""" Result (2 polygons, see image below):
[[[365, 260], [356, 254], [363, 202]], [[425, 133], [365, 191], [371, 149], [370, 145], [368, 142], [364, 141], [362, 141], [358, 142], [355, 145], [322, 230], [301, 215], [268, 98]]]
"""

我们试图使pyclipper方法和函数的命名尽可能接近python包装器的原始名称。同样,它应该被用来模仿基本库。唯一大的区别在于Execute函数的使用方式,正如这里所解释的pyclipper - How to use。在

您可以检查tests以更好地掌握用法。在

offset

相关问题 更多 >