Unity.Library.eppz.Geometry icon indicating copy to clipboard operation
Unity.Library.eppz.Geometry copied to clipboard

Polygon subtraction

Open Sajdkick opened this issue 5 years ago • 5 comments

Great stuff you got here! Just one question, you mention in the description that you can perform subtraction. Is it possible to subtract one polygon from another as shown in the picture below? So far i have only managed to do an XOR. image

Thanks!

Sajdkick avatar Jul 14 '18 14:07 Sajdkick

Hey, thanks for the good words! I'm all happy if people find this useful as well! Let me give you a more detailed answer below.

Geri-Borbas avatar Jul 15 '18 05:07 Geri-Borbas

Awesome, thanks for the quick reply!

All i had to do was to change this: clipper.Execute(ClipType.ctUnion, unionPaths);

Into: clipper.Execute(ClipType.ctDifference, unionPaths);

So if you wanted a general function for doing all these kinds of operations you could change the function public Polygon UnionPolygon() into public Polygon BooleanOperation(ClipType clipType)

Super simple, thanks again!

Sajdkick avatar Jul 15 '18 11:07 Sajdkick

Boolean operations are done using Clipper, a great open source freeware library for clipping and offsetting lines and polygons.

So far I only incorporated union, but you can easily extend the library to make extension methods doing different types of clipping operations. See ClipType for the actual Clipper documentation.

In this library, only ClipType.ctUnion is implemented so far. See Polygon.cs#L626.

Geri-Borbas avatar Jul 15 '18 14:07 Geri-Borbas

You can nicely arrange the extensions into an extension class like below.

using System;
using UnityEngine;


namespace EPPZ.Geometry
{


	public static class Polygon_Extensions
	{

                public Polygon IntersectionPolygon(this Polygon _this)
		{ ... }

                public Polygon DifferencePolygon(this Polygon _this)
		{ ... }

                public Polygon XORPolygon(this Polygon _this)
		{ ... }
	}
}

Geri-Borbas avatar Jul 15 '18 14:07 Geri-Borbas

i can't open example scene in unity 2018.3.6. is there any thing to do before importing?

kamrankhorasani avatar Jun 10 '19 04:06 kamrankhorasani