Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shape loses its fill styling with successive subtracts #99

Open
gverger opened this issue Oct 11, 2023 · 3 comments
Open

Shape loses its fill styling with successive subtracts #99

gverger opened this issue Oct 11, 2023 · 3 comments

Comments

@gverger
Copy link

gverger commented Oct 11, 2023

Hello,

Your library is (almost) a life savior for me, thanks!

I have an issue with a shape that I want to create. I want so subtract several rectangles from a circle, like so:

import micycle.pgs.*;

void setup() {
  background(255);
  size(800, 600);
  smooth();
}

void draw() {
  fill(#505050);
  noStroke();
  translate(400, 300);
 
  // create a donut
  var outer = createShape(ELLIPSE, 0, 0, 200, 200);
  var inner = createShape(ELLIPSE, 0, 0, 100, 100);
  outer = PGS_ShapeBoolean.subtract(outer, inner);
  
  // display on left side
  shape(outer, -250, 0);
  
  // make a first hole
  var hole1 = createShape(RECT, 0, -20, 200, 40);
  outer = PGS_ShapeBoolean.subtract(outer,hole1);
  
  // display in the center
  shape(outer);

  // make a second hole
  var hole2 = createShape(RECT, 0, -20, 200, 40);
  hole2.rotate(PI/3);
  outer = PGS_ShapeBoolean.subtract(outer,hole2);
  
  // display on right side
  shape(outer, 250, 0);
}

image

I would expect the result to be the same as the second shape + one more hole, but it loses its color.
Is there a limitation in subtractions, or did I discover a bug?

Thank you!

@micycle1
Copy link
Owner

micycle1 commented Oct 11, 2023

Making the second cut changes the nature of the shape from a single shape to a GROUP shape (now having 2 child shapes).

During shape boolean operations, PGS creates a new, non-stylised shape. To preserve styling (color, fill, etc.) it then copies the style of the input shape to the new shape.

In your case the new shape is a GROUP shape and the apply styling operation is happening to the group shape, but that doesn't automatically apply styling to child shapes -- I suppose this is an oversight in the code.

For now you can use this to get the styling as intended:

// display on right side
PGS_Conversion.disableAllStroke(outer); // applies to all child shapes
PGS_Conversion.setAllFillColor(outer, #505050); // applies to all child shapes
shape(outer, 250, 0);

image

@gverger
Copy link
Author

gverger commented Oct 11, 2023

Thanks, that makes sense. It seems surprising when you are not familiar with the internals of it, especially when I see that PRESERVE_STYLE is true.

From my novice point of view I would have expected that these 2 lines you provided are somehow automatically applied.

Anyway, for my current usage, I'll manage with this workaround, feel free to close this issue if you consider you won't touch the current behavior.

Thanks again!

@micycle1
Copy link
Owner

From my novice point of view I would have expected that these 2 lines you provided are somehow automatically applied.

Yes, I agree. It's oversight in the code and I'll fix it.

@micycle1 micycle1 changed the title Shape loses its filling with successive subtracts Shape loses its fill styling with successive subtracts May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants