Page 1 of 1

Fusion of shapes

Posted: Mon Jan 08, 2018 7:05 pm
by caramel
Hi,

Please, could you tell me if there is a way to construct a shape base on fusion of other shapes like the piture attached :
newShape = fusion([Shape1, Shape2, Shape3]);

Many thanks!
Caramel

Re: Fusion of shapes

Posted: Mon Jan 08, 2018 9:39 pm
by J-J
Hi Caramel,
Welcome to the forum.
Please, could you tell me if there is a way to construct a shape base on fusion of other shapes like the piture attached
That should be easy using the "break out segment" (D2) tool, just click on the segments you wish to remove.

Re: Fusion of shapes

Posted: Tue Jan 09, 2018 10:40 am
by andrew
Presumably, you are looking for a programmatic way (since you posted to the developer forum).

For polygon shapes (i.e. only straight segments), you can use clipper which comes with QCAD Professional:

Code: Select all

var clipper = new RPolygonClipper();

// add subject paths (islands: ccw, holes: cw):
clipper.addSubjectPath([new RVector(0,0), new RVector(100,0), new RVector(100,100), new RVector(0,100)]);
clipper.addSubjectPath([new RVector(50,50), new RVector(150,50), new RVector(150,150), new RVector(50,150)]);

// union:
clipper.execute(RS.Union, RS.NonZero);

// print result:
for (var c=0; c<clipper.getSolutionPathCount(); c++) {
    var vertices = clipper.getSolutionPath(c);
    qDebug("polygon:", vertices);
}

Re: Fusion of shapes

Posted: Tue Jan 09, 2018 1:34 pm
by caramel
Hi andrew,
Thanks for your response,

Yes I'm looking for a programmatic way and just for polygon shapes (with straight segments).

I tested your code with QCAD Professional and it works,
But when I test with other shapes :

Code: Select all

  var clipper = new RPolygonClipper();

  clipper.addSubjectPath([new RVector(0,0), new RVector(10,0), new RVector(10,10), new RVector(0,10)]);
  clipper.addSubjectPath([new RVector(-2,2), new RVector(12,2), new RVector(12,-2), new RVector(-2,-2)]);
  
  // union:
  clipper.execute(RS.Union, RS.NonZero);
  // print result:
  for (var c=0; c<clipper.getSolutionPathCount(); c++) {
      var vertices = clipper.getSolutionPath(c);
      qDebug("polygon:" , vertices)
      qDebug("area:" , Mod_Line2P.getArea(vertices))
  }

I get these results :
polygon: RVector(12, 2, 0, true),RVector(10, 2, 0, true),RVector(10, 10, 0, true),RVector(0, 10, 0, true),RVector(0, 2, 0, true),
RVector(10, 2, 0, true),RVector(10, 0, 0, true),RVector(0, 0, 0, true),RVector(0, 2, 0, true),
RVector(-2, 2, 0, true),RVector(-2, -2, 0, true),RVector(12, -2, 0, true)
area: 116

When I excpect these results :
polygon expected: RVector(12, 2, 0, true),RVector(10, 2, 0, true),RVector(10, 10, 0, true),RVector(0, 10, 0, true),RVector(0, 2, 0, true),
RVector(-2, 2, 0, true),RVector(-2, -2, 0, true),RVector(12, -2, 0, true)
area expected: 136

Is this a bug ?

Re: Fusion of shapes

Posted: Tue Jan 09, 2018 2:18 pm
by andrew
I think you might have missed the bit in the code about islands being CCW (counter-clockwise) and holes being CW (clockwise). Your second polygon is CW, hence treated as hole.

You might want to use RPolyline::getOrientation() to check for orientation CW or CCW, RPolyline::reverse() to reverse and RPolyline::getArea() to compute the area.

Re: Fusion of shapes

Posted: Tue Jan 09, 2018 2:58 pm
by caramel
You have absolutely right, I missed with orientation as you mentioned, now I get the exact expect results.

Thanks a lot !