2

I am searching for a library which can do the decomposition of polygons. I want define define directions or lines in which the polygon should be fragmented, as seen here:

an irregular polygon with internal subdivision lines, some vertical some not

So that I get the small polygons. Anyone know a library which supports this?

Or any ideas?

AakashM
  • 62,551
  • 17
  • 151
  • 186
Hunk
  • 479
  • 11
  • 33
  • From the picture it is not obvious - what is the criteria of adding line into the resulting partition (lines, that are edges are present, but also there are some lines that goes through sides of a polygon) – Alma Do Aug 06 '13 at 07:20
  • the line should have the directions of the edges and start on every edge point. But my goal is to define random directions and do the decomposition in this directions. – Hunk Aug 06 '13 at 08:33
  • So, it is just a set of lines? I mean, the input is: polugon that will be decomposed and some number of lines (let them be parallels to polygon's edges). And what are you expecting as the result? A set of new polygons? – Alma Do Aug 06 '13 at 08:36
  • yes i want as output a array of the small polygons, and input is the polygon and a set of lines – Hunk Aug 06 '13 at 08:46
  • I found this library do you know a function which can solve my problem? http://www.boost.org/doc/libs/1_54_0/libs/polygon/doc/index.html – Hunk Aug 08 '13 at 06:34

2 Answers2

1

I'm not sure which language are you using. I have a library, written for my purposes, that can get a full partition by given line set and return polygons as a result. It is written on PHP and called dimension and, using it, you can solve your question like way:

  1. Define your polygon by a set of lines LineSet_2D or a Polygon_2D
  2. Define partition lines also through Line_2D
  3. Use LineSet_2D method getPolygons to find all polygons

I've written an example:

//define or polygon. Note that Polygon_2D can also be used
$rPolygon = new LineSet_2D(
    new Line_2D( 0, 3, 1, 1),
    new Line_2D( 1, 1, 3, 0),
    new Line_2D( 3, 0, 1,-1),
    new Line_2D( 1,-1, 0,-3),
    new Line_2D( 0,-3,-1,-1),
    new Line_2D(-1,-1,-3,0),
    new Line_2D(-3, 0,-1, 1),
    new Line_2D(-1, 1, 0, 3)
);
//define partition line set
$rPartition = new LineSet_2D(
    new Line_2D(-1, 1, 1,-1),
    new Line_2D(-1,-1, 1, 1)
);
//result line set:
$rResultSet    = LineSet_2D::createFromArray(array_merge(
    $rPolygon->getLines(), 
    $rPartition->getLines()
));
//for example, dump plain result:
var_dump($rResultSet->getPolygons());

You can also find this example here But I think it is not exact solution for your question, since my LineSet_2D class will return all looped polygons (i.e. not only 'pieces').

Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • hey, thank you for your answer i am using c++ . What you mean with looped polygons? My solution should be every Polygon for example A,B,c .... in my figure – Hunk Aug 06 '13 at 09:55
  • Well, I think it is not too difficult to adapt PHP to C++ (since C++ has much more reach syntax and OOP abilities). By mentioning looped polygons I've meant that it you'll make a partition via LineSet_2D, you'll get all polygons, not only pieces of original. Example: imagine a square with 2 diagonals. It is divided into 4 pieces, but it has much more real polygons, since there are not only "little" triangle pieces, but also "big" (half of a square) triangles and even not triangles (whole square without one little triangle piece). – Alma Do Aug 06 '13 at 10:00
0

You are looking for a "polygon chop" boolean operation. You may google it for resources available.

Some queues on doing this on your own.. For each slicing line..

  1. Find the intersection points of the slicing line with the edges of the polygon.
  2. For each edge that it intersects, split the edge into two.
  3. Split the polygon corresponding to the splitted edge, into two polygons.

Do the same for all polygons. You will have to take care of special cases, like splitting line going through vertex and so on...

Kshitij Banerjee
  • 1,678
  • 1
  • 19
  • 35
  • u mean operations like diff etc.. ? True on this way this is possible but i think i have a lot of library which do this with 2 polygons, so it should have a few with polygon and line ? – Hunk Aug 06 '13 at 14:10
  • I am not sure of libraries that do this for you. I do not use others, since I work on one. Also, it is more like a data structural problem apart from the intersection routines. So, if you dont find one, which i thnk would most likely be the case. You could go ahead and write one of your own. Its not that tough, and may be fun to do. Plus, you can fix any bugs yourself in the future. – Kshitij Banerjee Aug 14 '13 at 05:59