1

I'm learning processing and trying to create an SVG file containing a complex shape (path) which should be a union of a hexagon and a rectangle. But, unfortunately, I can't find any set operations on PShape.

a hexrect

kelin
  • 11,323
  • 6
  • 67
  • 104
  • Found similar question on [discourse.processing.org](https://discourse.processing.org/t/boolean-operation-in-polygons/23439?u=kelin) – kelin Feb 21 '22 at 09:20
  • [PGS](https://github.com/micycle1/PGS) provides shape boolean operations for Processing. – micycle Mar 01 '22 at 16:34

1 Answers1

2

I found useful ErraticGenerator's answer on Processing forum. The idea is to use g.js library mixed with p5js drawing methods.

Include the script in index.html:

<script src="https://cdn.rawgit.com/nodebox/g.js/master/dist/g.min.js"></script>

Sketch example:

function setup() {
  createCanvas(400, 400)
  noLoop()
}

function draw() {
  let rect = g.rect(50, 50, 100, 100)
  let ellipse = g.ellipse(100, 100, 100, 100)
  let union = g.compound(rect, ellipse, 'union')
  union.fill = null
  union.stroke = 'red'
  union.draw(drawingContext)
}

Path union

This also works in combo with p5.js-svg!

kelin
  • 11,323
  • 6
  • 67
  • 104