# 2D lineart - culling hidden lines for SVG output / Plotting

**URL:** <https://discourse.processing.org/t/2d-lineart-culling-hidden-lines-for-svg-output-plotting/30031>\
**Category:** Libraries\
**Created:** [May 11, 2021, 5:18am UTC](https://discourse.processing.org/t/2d-lineart-culling-hidden-lines-for-svg-output-plotting/30031 "2021-05-11T05:18:14Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [July 11, 2022, 6:46pm UTC](https://discourse.processing.org/t/2d-lineart-culling-hidden-lines-for-svg-output-plotting/30031/8 "2022-07-11T18:46:07Z")

</div>

This _is_ possible in Processing with [PGS](https://discourse.processing.org/t/processing-geometry-suite/29924) shape boolean operations.

The approach is: for each shape, subtract the union of shapes visually above it.

l will include the algorithm given below as a method in the next version of the library (which has been WIP for some time now!).

## Example

### Input

[left] Circles shaded by layer (lighter = visually on top). [right] Translucent fill to reveal overlap.

 ![1657562678596](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/f/8f1d2c5ea16aa1f3fe5e40dc55f78146e4fd2cdc.jpeg) ![1657561366656](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/1/71513e8d518ef7ce94588ca429239843b1ee4685.jpeg)

### Line culling

Note that some edges duplicated/overlap where shapes have been cut away from each other.

 ![1657561393731](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/b/cb27b5023916c55d0a1a0330ad2e507dcd7dbb6a.jpeg)

### Line culling + dissolve remaining lines

Having removed any overlapping edges, we get pure linework for svg/plotting.

 ![1657562023702](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/4/3496dcf2820fe31ce5de29b93712f297445a4f5a.png)

```auto
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import micycle.pgs.PGS_Conversion;
import micycle.pgs.PGS_ShapeBoolean;
import org.locationtech.jts.dissolve.LineDissolver;

PShape cullLines(PShape shape) {
	var layers = PGS_Conversion.getChildren(shape); // visual top last
	Collections.reverse(layers); // visual top first
	PShape union = layers.get(0); // cascading union

	List<PShape> layersOut = new ArrayList<>();
	layersOut.add(layers.get(0));
	for (int i = 1; i < layers.size(); i++) {
		// for each shape, subtract the union of shapes visually above it
		var layer = PGS_ShapeBoolean.subtract(layers.get(i), union);
		layersOut.add(layer);
		union = PGS_ShapeBoolean.union(union, layers.get(i));
	}

	var culled = PGS_Conversion.fromChildren(layersOut);
	var dissolved = toPShape(LineDissolver.dissolve(fromPShape(culled)));
	
	return dissolved;
	}

```

---

_[View the full topic](https://discourse.processing.org/t/2d-lineart-culling-hidden-lines-for-svg-output-plotting/30031)._
