Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Clipper. How to cut a polygon using an open polygon?
Started by pahuchiy Jan 24 2019 12:58 AM

6 replies to this topic
clipper
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

pahuchiy

[GLOBAL: userInfoPane.html]
pahuchiy
  • Observer

  • 5 posts
  • Corona SDK

line_clipping2.png

The example with ovals uses only closed paths. But how do I get the result like in the picture? Particularly interested in intersection. Do I understand correctly that with open paths I need to use PolyTree? I did not find any such example.



[TOPIC: post.html]
#2

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 805 posts
  • Corona SDK

Hi, pahuchiy.

 

Yes, in reviewing the docs it does sound like a poly-tree is what you want, assuming you need to know the open- or closed-ness of the paths. And I have indeed neglected such an example.  :(

 

Roughly, it might go something like:

local clipper = require("plugin.clipper")

local my_clipper = clipper.NewClipper()
local tree = clipper.NewPolyTree()

-- populate and add to paths to my_clipper

my_clipper:Execute("Intersection", { out = tree })

local open = clipper.OpenPathsFromPolyTree(tree)
local closed = clipper.ClosedPathsFromPolyTree(tree)

I'll try to add such an example, though I can't say how soon. Looks like the docs need a few minor fixes too.


  • pahuchiy likes this

[TOPIC: post.html]
#3

pahuchiy

[GLOBAL: userInfoPane.html]
pahuchiy
  • Observer

  • 5 posts
  • Corona SDK

thanks but not working

local clipper = require("plugin.clipper")
local my_clipper = clipper.NewClipper()
local tree = clipper.NewPolyTree()
print("this text does not appear in the console")
local path1={100,100, 300,100, 300,200, 100, 200}
local path2={140, 70, 220, 320}
local UD1 = clipper.NewPath()
for i = 1, #path1,2 do
  UD1:AddPoint(path1[i], path1[i+1])
end
local subj = clipper.NewPathArray()
subj:AddPath(UD1 )

local UD2 = clipper.NewPath()
for i = 1, #path2,2 do
  UD2:AddPoint(path2[i], path2[i+1])
end
local clip = clipper.NewPathArray()
clip:AddPath(UD2 )
my_clipper:AddPaths(subj, "SubjectClosed")
my_clipper:AddPaths(clip, "Clip")
my_clipper:Execute("Intersection", { out = tree })
local open = clipper.OpenPathsFromPolyTree(tree)
local closed = clipper.ClosedPathsFromPolyTree(tree)


[TOPIC: post.html]
#4

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

I've not seen or used Clipper before (will check it out) but just in case this helps, take a look at my slicer:

 

https://github.com/HoraceBury/slicer

 

There is an interactive build, too.


  • StarCrunch, XeduR @Spyric and pahuchiy like this

[TOPIC: post.html]
#5

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 805 posts
  • Corona SDK

@pahuchiy If that line of text isn't appearing, the function is probably failing somehow. (Rule #1: the one thing you forget to try will fail publicly  :)) I'll have to take a look.

 

@horacebury Very cool. I only skimmed the source a bit. What sort of process are you following?



[TOPIC: post.html]
#6

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

I'm not. Essentially, it works out the intersections and copies the list of points from the original into two new tables. The hard part is making sure the intersected lines don't generate duplicates.



[TOPIC: post.html]
#7

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 805 posts
  • Corona SDK

I finally had a chance to dig into this yesterday and I believe now have it working. I basically have a rough approximation of the polyline / polygon case at the bottom of this page, cycling between the clip types, and agreeing with the results shown there.

 

There were indeed some issues.  :P

 

I might see about adapting this into another sample too.

 

I should be able to do all the non-Windows builds in the next day or so and then upload all the binaries at once, including the whole gamut for Android.

 

Minor breaking change: in that clip type link, I read some of those rules about open / closed paths a bit clumsily and just assumed only closed clip paths made sense. I've now broken them up into "Clip" and "ClipClosed", following the style used by subject paths. For this reason I'll probably wait until the new uploads go live before updating the samples.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: clipper