Hemesh substraction one with many works, but intersection does not.
in
Contributed Library Questions
•
16 days ago
Hi,
I am working with "Hemesh" Library intersection and substraction functions.
I have applied substraction, calculating not one element with another, but one with many.
Sadly, it works with substraction function, but for intersection, I miss something.
The substracted part is in blue color, and intersected part should be in red (but it does not work, but I either do not get any errors)
The code:
- import wblut.math.*;
- import wblut.processing.*;
- import wblut.core.*;
- import wblut.hemesh.*;
- import wblut.geom.*;
- import java.util.List;
- WB_Render render;
- void setup() {
- size(1280, 720, OPENGL);
- smooth();
- //initialize WB_Render
- render = new WB_Render(this);
- }//setup
- void draw() {
- background(255);
- //size of polygon, call it radius;
- int dim = 200;
- ArrayList <WB_Polygon2D> inputPolys = new ArrayList <WB_Polygon2D> ();
- ArrayList <WB_Point2d> points = new ArrayList <WB_Point2d> ();
- //Polygon a initialization (in this case triangle)
- points.add(new WB_Point2d(width/2, height/3-dim));
- points.add(new WB_Point2d(width/2+dim, height/2+dim/2));
- points.add(new WB_Point2d(width/2-dim, height/2+dim/2));
- inputPolys.add(new WB_Polygon2D (points));
- ///////////////////////////////////////////////////
- //clear points or bad things would happen
- points.clear();
- //Polygon b initialization (in this case triangle)
- points.add( new WB_Point2d(mouseX, mouseY-dim) );
- points.add( new WB_Point2d(mouseX+dim, mouseY+dim/2) );
- points.add( new WB_Point2d(mouseX-dim, mouseY+dim/2) );
- inputPolys.add(new WB_Polygon2D (points));
- ///////////////////////////////////////////////////
- points.clear();
- //Polygon a initialization (in this case triangle)
- points.add(new WB_Point2d(width/4, height/3-dim));
- points.add(new WB_Point2d(width/5+dim, height/5+dim/2));
- points.add(new WB_Point2d(width/5-dim, height/5+dim/2));
- inputPolys.add(new WB_Polygon2D (points));
- ///////////////////////////////////////////////////
- points.clear();
- //Polygon a initialization (in this case triangle)
- points.add(new WB_Point2d(width/4, height/3+dim));
- points.add(new WB_Point2d(width/5-dim, height/8+dim/2));
- points.add(new WB_Point2d(width/8+dim, height/5+dim/2));
- inputPolys.add(new WB_Polygon2D (points));
- ////////////////////////////////////////////////////////////////////////substraction - blue color
- List <WB_Polygon2D> subtractPolys = new ArrayList <WB_Polygon2D> ();
- for (int i = 0; i < inputPolys.size(); i++) {
- subtractPolys.addAll(subtractManyFromOne(inputPolys.get(i), inputPolys));
- }
- fill(0,0,255);
- noStroke();
- for (WB_Polygon2D p : subtractPolys) {
- render.drawPolygon2D(p);
- }
- ///////////////////////////////////////////////////////////////////////////intersection - red color
- List <WB_Polygon2D> intersectPolys = new ArrayList <WB_Polygon2D> ();
- for (int i = 0; i < inputPolys.size(); i++) {
- intersectPolys.addAll(intersectManyFromOne(inputPolys.get(i), inputPolys));
- }
- fill(255,0,0);
- for (WB_Polygon2D i : intersectPolys) {
- fill(0);
- render.drawPolygon2D(i);
- }
- noFill();
- stroke(0);
- // for (WB_Polygon2D p : inputPolys) {
- // render.drawPolygon2D(p);
- // }
- }//draw
- List <WB_Polygon2D> subtractManyFromOne(WB_Polygon2D one, List <WB_Polygon2D> many) {
- List <WB_Polygon2D> allFragments=new ArrayList <WB_Polygon2D> ();// this will collect all the pieces that remain from "one" after subtracting "many"
- allFragments.add(one); //start with the source poly "one"
- for (WB_Polygon2D other:many) {
- if (other!=one) {//this way "one" can be part an element of "many" and you don't have to worry about self-subtracting...
- // Subtracting a polygon can result in several fragments.To subtract another polygon you need to do that for all fragments. Giving you a new list of
- // fragments etc.
- List <WB_Polygon2D> AllFragmentsMinusOther=new ArrayList <WB_Polygon2D> ();// this stores all new fragments
- for (WB_Polygon2D fragment: allFragments) {
- AllFragmentsMinusOther.addAll(WB_Polygon2D.subtract(fragment, other));
- }
- allFragments=AllFragmentsMinusOther;//for the next poly top subtract you need to start from the new list of fragments, etc.
- }
- }
- return allFragments;
- }
- List <WB_Polygon2D> intersectManyFromOne(WB_Polygon2D one, List <WB_Polygon2D> many) {
- List <WB_Polygon2D> allFragments=new ArrayList <WB_Polygon2D> ();// this will collect all the pieces that remain from "one" after intersecting "many"
- allFragments.add(one); //start with the source poly "one"
- for (WB_Polygon2D other:many) {
- if (other!=one) {//this way "one" can be part an element of "many" and you don't have to worry about self-intersection...
- // Intersecting a polygon can result in several fragments.To intersect another polygon you need to do that for all fragments. Giving you a new list of
- // fragments etc.
- List <WB_Polygon2D> AllFragmentsMinusOther=new ArrayList <WB_Polygon2D> ();// this stores all new fragments
- for (WB_Polygon2D fragment: allFragments) {
- AllFragmentsMinusOther.addAll(WB_Polygon2D.intersection(fragment, other));
- }
- allFragments=AllFragmentsMinusOther;//for the next poly top intersect you need to start from the new list of fragments, etc.
- }
- }
- return allFragments;
- }
- for (int i = 0; i < inputPolys.size(); i++) {
- WB_Polygon2D a = inputPolys.get(i);
- for (int j = i+1; j < inputPolys.size(); j++) {
- WB_Polygon2D b = inputPolys.get(j);
- List <WB_Polygon2D> polys = WB_Polygon2D.intersection(a, b);
- for (WB_Polygon2D p : polys) {
- fill(0, 50);
- render.drawPolygon2D(p);
- fill(0);
- render.drawPolygon2DVertices(p, 5);
- }//for
1