最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

clipper - Union error using Delphi version of Clipper2 - Stack Overflow

programmeradmin2浏览0评论

I am trying to combine 2 polygons. I downloaded Angus Johnson's Clipper2 a few hours ago and after looking at a few examples I thought I had managed to write the required code. A large number of test cases produced the expected result, but I have one case that does not. In this image the result of combining the red and green polygons is the white polygon (which I have moved to below the original polygons:

The result is as expected in the examples at each end, but the middle one is missing the expected small triangle between the red polygon and the left end of the green polygon.

My logic is:

var plys, sol : TPaths64;
setlength (plys, 2);

// Originally I had code here to load the 2 polygons from my CAD drawing, 
// but the following replicates the polygons giving the problem ...
setlength (plys[0], 8);
plys[0][0].x := 192000;
plys[0][0].y := -179200;
plys[0][1].x := 192000;
plys[0][1].y := 89600;
plys[0][2].x := 678400;
plys[0][2].y := 89600;
plys[0][3].x := 473600;
plys[0][3].y := 294400;
plys[0][4].x := 761600;
plys[0][4].y := 582400;
plys[0][5].x := 576000;
plys[0][5].y := 582400;
plys[0][6].x := 307200;
plys[0][6].y := 435200;
plys[0][7].x := -57600;
plys[0][7].y := 70400;

setlength (plys[1], 7);
plys[1][0].x := -72971;
plys[1][0].y := -21827;
plys[1][1].x := -47371;
plys[1][1].y := 131773;
plys[1][2].x := 368629;
plys[1][2].y := 10173;
plys[1][3].x := 528629;
plys[1][3].y := 170173;
plys[1][4].x := -431371;
plys[1][4].y := 170173;
plys[1][5].x := -520971;
plys[1][5].y := 16573;
plys[1][6].x := -252171;
plys[1][6].y := 67773;

var Clipper := TClipper.Create;
try
  Clipper.AddSubject ( plys);
  Clipper.Execute(ctUnion, frNonZero, sol); 
finally
  Clipper.Free;
end;

{resulting sol is 
(((-57599, 70399), (192000, -179200), (192000, 61803), (368629, 10173), 
  (448056, 89600), (678400, 89600), (473600, 294400), (761600, 582400), 
  (576000, 582400), (307200, 435200), (42173, 170173), (-431371, 170173),
  (-520971, 16573), (-252171, 67773), (-72971, -21827)))
}

In my CAD program the corner of the green polygon is precisely on the edge of the red one, but I rounded the CAD co-ordinates to give integer values to Clipper, so my guess is that the result of this is that the vertex of the green polygon is extremely close to the edge of the red one (but not necessarily precisely on it). Not sure if this is why I am seeing the problem, but I did another test where I modified the red polygon so that that edge at the corner of the green one was exactly vertical (and I moved the vertex of the green to be exactly on it). This ensured that even with the rounded co-ordinates the green vertex would still be precisely on the red edge. The result of this was correct. This image shows the modified green and red polygons (with the cyan result moved below original polygons):

I also tried slightly different code based on a different example to create the union without creating a TClipper object:

var Clip : TPaths64;
setlength (Clip,0);
sol := union(plys, clip, frNonZero);

This produced the exact same results as my original attempt. I also tried different fill rules, but nothing produced the expected result.

The original polygons were both defined in counter clockwise direction. I tried changing the direction of one or both of them but the result did not change.

Initially the number of successful tests made me think that I was probably doing everything correctly, but not being familiar with it I was not certain (hence this question). I decided I could live with this particular problem (and code around it in my application by adding a point to the side of the red polygon where the green one touched it (referring to this particular example). But throwing some real live data at it in my application produced many more results that I considered erroneous. Perhaps I just don't really understand what Clipper's union function is intended to achieve. At any rate I have abandoned the thought of using it and will attempt to write my own union logic. Still interested in any responses to this question though in case I come back to it ....

发布评论

评论列表(0)

  1. 暂无评论