It tends to be domains. I guess it's not required, but owning the same domain is a good way to guarantee no clashes, right? I'll get on this soonish, anyways.
Polygon decomposition for AS3 - source attached
General DiscussionHi there, nice work. It has bugs though - if you try to decompose this figure:
It will returnt these polygons:
As you see, there're two of them containing only 1 point, that causing b2D errors.
Entry figure has no self - intersections.
Here is screenshot:

Code:
var points:Array = [new Point(-750, 300),new Point(-350, 298),new Point(190, 312),new Point(171, 276),new Point(190, 260.5),new Point(187, 278.25),new Point(600, 300),new Point(1600, 300),new Point(1600, 710),new Point(-750, 710)]
//decomposing
var decomp:PolyDecompBayazit = new PolyDecompBayazit(points)
decomp.decompose(tracer)
function tracer(dec:PolyDecompBayazit)
{
trace(dec.points.length, dec.points)
}
//decomposing
var decomp:PolyDecompBayazit = new PolyDecompBayazit(points)
decomp.decompose(tracer)
function tracer(dec:PolyDecompBayazit)
{
trace(dec.points.length, dec.points)
}
It will returnt these polygons:
Code:
5 (x=1600, y=710),(x=-750, y=710),(x=-750, y=300),(x=-350, y=298),(x=190, y=312)
1 (x=177.58554844219032, y=288.4778812588869)
1 (x=177.58554844219032, y=288.4778812588869)
4 (x=190, y=312),(x=177.58554844219032, y=288.4778812588869),(x=187, y=278.25),(x=600, y=300)
4 (x=600, y=300),(x=1600, y=300),(x=1600, y=710),(x=190, y=312)
1 (x=177.58554844219032, y=288.4778812588869)
1 (x=177.58554844219032, y=288.4778812588869)
4 (x=190, y=312),(x=177.58554844219032, y=288.4778812588869),(x=187, y=278.25),(x=600, y=300)
4 (x=600, y=300),(x=1600, y=300),(x=1600, y=710),(x=190, y=312)
As you see, there're two of them containing only 1 point, that causing b2D errors.
Entry figure has no self - intersections.
Here is screenshot:

I'll look into this and see what happens on my local copy. I fixed a few bugs between the submission to Boris and the version that I have in my game.
Hi, any news?
There is a change here in the constructor:
In combineClosePoints, I had to increase the epsilon to eliminate some extra points, I believe that this was due to converting a curve to a high resolution of straight segments:
And then in the main decomposition method, I had to add this check, but I never did fully understand the problem visually.
Code:
public function PolyDecompBayazit(points:Array) {
this.points = points;
combineClosePoints();
combineColinearPoints();
combineClosePoints();
if(this.points.length > 2)
makeCCW();
}
this.points = points;
combineClosePoints();
combineColinearPoints();
combineClosePoints();
if(this.points.length > 2)
makeCCW();
}
In combineClosePoints, I had to increase the epsilon to eliminate some extra points, I believe that this was due to converting a curve to a high resolution of straight segments:
Code:
if(sqdist(a, b) > 0.035) {
And then in the main decomposition method, I had to add this check, but I never did fully understand the problem visually.
Code:
if(lowerPoly.points.length == this.points.length || upperPoly.points.length == this.points.length) {
return;
}
// solve smallest poly first
return;
}
// solve smallest poly first
I've changed the code as you've write, but nothing changed - once again, it returns 5 polys with two of them having only one point...
Can you upload your latest version of decomposer? I will really appreciate it. THanks!
Can you upload your latest version of decomposer? I will really appreciate it. THanks!
here it is
Code:
package {
import flash.geom.*;
import Box2D.Common.Math.*;
public class PolyDecompBayazit {
public static function area(a:Point, b:Point, c:Point):Number {
return (((b.x - a.x)*(c.y - a.y))-((c.x - a.x)*(b.y - a.y)));
}
public static function right(a:Point, b:Point, c:Point):Boolean {
return area(a, b, c) < 0;
}
public static function rightOn(a:Point, b:Point, c:Point):Boolean {
return area(a, b, c) <= 0;
}
public static function left(a:Point, b:Point, c:Point):Boolean {
return area(a, b, c) > 0;
}
public static function leftOn(a:Point, b:Point, c:Point):Boolean {
return area(a, b, c) >= 0;
}
public static function sqdist(a:Point, b:Point):Number {
var dx:Number = b.x - a.x;
var dy:Number = b.y - a.y;
return dx * dx + dy * dy;
}
public static function getIntersection(start1:Point, end1:Point, start2:Point, end2:Point):Point {
var a1:Number = end1.y - start1.y;
var b1:Number = start1.x - end1.x;
var c1:Number = a1 * start1.x + b1 * start1.y;
var a2:Number = end2.y - start2.y;
var b2:Number = start2.x - end2.x;
var c2:Number = a2 * start2.x + b2 * start2.y;
var det:Number = a1 * b2 - a2*b1;
if (Math.abs(det) > Number.MIN_VALUE) { // lines are not parallel
return new Point((b2 * c1 - b1 * c2) / det, (a1 * c2 - a2 * c1) / det);
}
return null;
}
public function combineColinearPoints() {
// combine similar points
var combinedPoints:Array = [];
for(var i:int = 0; i < points.length; i++) {
var a:Point = at(i - 1), b:Point = at(i), c:Point = at(i + 1);
if(getIntersection(a, b, b, c) != null)
combinedPoints.push(b);
}
points = combinedPoints;
}
public var points:Array;
public function PolyDecompBayazit(points:Array) {
this.points = points;
combineClosePoints();
combineColinearPoints();
combineClosePoints();
if(this.points.length > 2)
makeCCW();
}
public function combineClosePoints():void {
var combinedPoints:Array = [];
for(var i:int = 0; i < points.length; i++) {
var a:Point = at(i);
var b:Point = at(i + 1);
if(sqdist(a, b) > 0.035) {
combinedPoints.push(a);
}
}
points = combinedPoints;
}
public function at(i:int):Point {
var s:int = points.length;
return points[(i + s) % s];
}
public function isReflex(i:int):Boolean {
return right(at(i - 1), at(i), at(i + 1));
}
public function polyFromRange(lower:int, upper:int):PolyDecompBayazit {
if(lower < upper)
return new PolyDecompBayazit(points.slice(lower, upper + 1));
else
return new PolyDecompBayazit(points.slice(lower).concat(points.slice(0, upper + 1)));
}
public static var polyID:int = 0;
public function decompose(callback:Function, recurseLevel:int = 0):void {
if(recurseLevel >= 100) {
TS.log("polygon recurse level = " + recurseLevel);
}
if(points.length < 3) return;
for (var i:int = 0; i < points.length; ++i) {
if (isReflex(i)) {
// Find closest two vertices in range from a reflex point (two the vertices are by going CW and CCW around polygon)
// See first diagram on this page: http://mnbayazit.com/406/bayazit
var upperDist:Number = Number.MAX_VALUE, upperIntersection:Point = new Point(), upperIndex:int;
var lowerDist:Number = Number.MAX_VALUE, lowerIntersection:Point = new Point(), lowerIndex:int;
for (var j:int = 0; j < points.length; ++j) {
if (left(at(i - 1), at(i), at(j)) && rightOn(at(i - 1), at(i), at(j - 1))) { // if line intersects with an edge
var intersectionPoint:Point = getIntersection(at(i - 1), at(i), at(j), at(j - 1)); // find the point of intersection
if (right(at(i + 1), at(i), intersectionPoint)) { // make sure it's inside the poly
var distance:Number = sqdist(at(i), intersectionPoint);
if (distance < lowerDist) { // keep only the closest intersection
lowerDist = distance;
lowerIntersection = intersectionPoint;
lowerIndex = j;
}
}
}
if (left(at(i + 1), at(i), at(j + 1)) && rightOn(at(i + 1), at(i), at(j))) {
intersectionPoint = getIntersection(at(i + 1), at(i), at(j), at(j + 1));
if (left(at(i - 1), at(i), intersectionPoint)) {
distance = sqdist(at(i), intersectionPoint);
if (distance < upperDist) {
upperDist = distance;
upperIntersection = intersectionPoint;
upperIndex = j;
}
}
}
}
var lowerPoly:PolyDecompBayazit, upperPoly:PolyDecompBayazit;
// if there are no vertices to connect to, choose a point in the middle
if (lowerIndex == (upperIndex + 1) % points.length) {
var steinerPoint:Point = new Point(
(lowerIntersection.x + upperIntersection.x) * 0.5,
(lowerIntersection.y + upperIntersection.y) * 0.5);
lowerPoly = polyFromRange(i, upperIndex);
lowerPoly.points.push(steinerPoint);
if (i < upperIndex)
upperPoly = polyFromRange(lowerIndex, i);
else
upperPoly = polyFromRange(0, i);
upperPoly.points.push(steinerPoint);
} else {
// connect to the closest point within the triangle
// at(n) handles mod points.length, so increase upperIndex to make for loop easy
if (lowerIndex > upperIndex) upperIndex += points.length;
// Find closest point in range
var closestIndex:int, closestDist:Number = Number.MAX_VALUE, closestVert:Point;
for (j = lowerIndex; j <= upperIndex; ++j) {
if (leftOn(at(i - 1), at(i), at(j)) && rightOn(at(i + 1), at(i), at(j))) {
distance = sqdist(at(i), at(j));
if (distance < closestDist) {
closestDist = distance;
closestVert = at(j);
closestIndex = j % points.length;
}
}
}
lowerPoly = polyFromRange(i, closestIndex);
upperPoly = polyFromRange(closestIndex, i);
}
if(lowerPoly.points.length == this.points.length || upperPoly.points.length == this.points.length) {
polyID++;
for(j = 0; j < points.length; j++) {
//Main.debugger.drawText("" + polyID + "-" + j + ": " + (at(j).x * 30).toPrecision(4) + ", " + (-4447 + at(j).y * 30).toPrecision(4), new b2Vec2(at(j).x, at(j).y), 0xFFFFFF, true);
}
return;
}
// solve smallest poly first
if (lowerPoly.points.length < upperPoly.points.length) {
lowerPoly.decompose(callback, recurseLevel + 1);
upperPoly.decompose(callback, recurseLevel + 1);
} else {
upperPoly.decompose(callback, recurseLevel + 1);
lowerPoly.decompose(callback, recurseLevel + 1);
}
return;
}
}
if(points.length >= 3) callback(this);
}
public function makeCCW():void {
var br:int = 0;
// find bottom right point
for (var i:int = 1; i < points.length; ++i) {
if (at(i).y < at(br).y || (at(i).y == at(br).y && at(i).x > at(br).x)) {
br = i;
}
}
// reverse poly if clockwise
if (!left(at(br - 1), at(br), at(br + 1))) {
points.reverse();
}
}
}
}
import flash.geom.*;
import Box2D.Common.Math.*;
public class PolyDecompBayazit {
public static function area(a:Point, b:Point, c:Point):Number {
return (((b.x - a.x)*(c.y - a.y))-((c.x - a.x)*(b.y - a.y)));
}
public static function right(a:Point, b:Point, c:Point):Boolean {
return area(a, b, c) < 0;
}
public static function rightOn(a:Point, b:Point, c:Point):Boolean {
return area(a, b, c) <= 0;
}
public static function left(a:Point, b:Point, c:Point):Boolean {
return area(a, b, c) > 0;
}
public static function leftOn(a:Point, b:Point, c:Point):Boolean {
return area(a, b, c) >= 0;
}
public static function sqdist(a:Point, b:Point):Number {
var dx:Number = b.x - a.x;
var dy:Number = b.y - a.y;
return dx * dx + dy * dy;
}
public static function getIntersection(start1:Point, end1:Point, start2:Point, end2:Point):Point {
var a1:Number = end1.y - start1.y;
var b1:Number = start1.x - end1.x;
var c1:Number = a1 * start1.x + b1 * start1.y;
var a2:Number = end2.y - start2.y;
var b2:Number = start2.x - end2.x;
var c2:Number = a2 * start2.x + b2 * start2.y;
var det:Number = a1 * b2 - a2*b1;
if (Math.abs(det) > Number.MIN_VALUE) { // lines are not parallel
return new Point((b2 * c1 - b1 * c2) / det, (a1 * c2 - a2 * c1) / det);
}
return null;
}
public function combineColinearPoints() {
// combine similar points
var combinedPoints:Array = [];
for(var i:int = 0; i < points.length; i++) {
var a:Point = at(i - 1), b:Point = at(i), c:Point = at(i + 1);
if(getIntersection(a, b, b, c) != null)
combinedPoints.push(b);
}
points = combinedPoints;
}
public var points:Array;
public function PolyDecompBayazit(points:Array) {
this.points = points;
combineClosePoints();
combineColinearPoints();
combineClosePoints();
if(this.points.length > 2)
makeCCW();
}
public function combineClosePoints():void {
var combinedPoints:Array = [];
for(var i:int = 0; i < points.length; i++) {
var a:Point = at(i);
var b:Point = at(i + 1);
if(sqdist(a, b) > 0.035) {
combinedPoints.push(a);
}
}
points = combinedPoints;
}
public function at(i:int):Point {
var s:int = points.length;
return points[(i + s) % s];
}
public function isReflex(i:int):Boolean {
return right(at(i - 1), at(i), at(i + 1));
}
public function polyFromRange(lower:int, upper:int):PolyDecompBayazit {
if(lower < upper)
return new PolyDecompBayazit(points.slice(lower, upper + 1));
else
return new PolyDecompBayazit(points.slice(lower).concat(points.slice(0, upper + 1)));
}
public static var polyID:int = 0;
public function decompose(callback:Function, recurseLevel:int = 0):void {
if(recurseLevel >= 100) {
TS.log("polygon recurse level = " + recurseLevel);
}
if(points.length < 3) return;
for (var i:int = 0; i < points.length; ++i) {
if (isReflex(i)) {
// Find closest two vertices in range from a reflex point (two the vertices are by going CW and CCW around polygon)
// See first diagram on this page: http://mnbayazit.com/406/bayazit
var upperDist:Number = Number.MAX_VALUE, upperIntersection:Point = new Point(), upperIndex:int;
var lowerDist:Number = Number.MAX_VALUE, lowerIntersection:Point = new Point(), lowerIndex:int;
for (var j:int = 0; j < points.length; ++j) {
if (left(at(i - 1), at(i), at(j)) && rightOn(at(i - 1), at(i), at(j - 1))) { // if line intersects with an edge
var intersectionPoint:Point = getIntersection(at(i - 1), at(i), at(j), at(j - 1)); // find the point of intersection
if (right(at(i + 1), at(i), intersectionPoint)) { // make sure it's inside the poly
var distance:Number = sqdist(at(i), intersectionPoint);
if (distance < lowerDist) { // keep only the closest intersection
lowerDist = distance;
lowerIntersection = intersectionPoint;
lowerIndex = j;
}
}
}
if (left(at(i + 1), at(i), at(j + 1)) && rightOn(at(i + 1), at(i), at(j))) {
intersectionPoint = getIntersection(at(i + 1), at(i), at(j), at(j + 1));
if (left(at(i - 1), at(i), intersectionPoint)) {
distance = sqdist(at(i), intersectionPoint);
if (distance < upperDist) {
upperDist = distance;
upperIntersection = intersectionPoint;
upperIndex = j;
}
}
}
}
var lowerPoly:PolyDecompBayazit, upperPoly:PolyDecompBayazit;
// if there are no vertices to connect to, choose a point in the middle
if (lowerIndex == (upperIndex + 1) % points.length) {
var steinerPoint:Point = new Point(
(lowerIntersection.x + upperIntersection.x) * 0.5,
(lowerIntersection.y + upperIntersection.y) * 0.5);
lowerPoly = polyFromRange(i, upperIndex);
lowerPoly.points.push(steinerPoint);
if (i < upperIndex)
upperPoly = polyFromRange(lowerIndex, i);
else
upperPoly = polyFromRange(0, i);
upperPoly.points.push(steinerPoint);
} else {
// connect to the closest point within the triangle
// at(n) handles mod points.length, so increase upperIndex to make for loop easy
if (lowerIndex > upperIndex) upperIndex += points.length;
// Find closest point in range
var closestIndex:int, closestDist:Number = Number.MAX_VALUE, closestVert:Point;
for (j = lowerIndex; j <= upperIndex; ++j) {
if (leftOn(at(i - 1), at(i), at(j)) && rightOn(at(i + 1), at(i), at(j))) {
distance = sqdist(at(i), at(j));
if (distance < closestDist) {
closestDist = distance;
closestVert = at(j);
closestIndex = j % points.length;
}
}
}
lowerPoly = polyFromRange(i, closestIndex);
upperPoly = polyFromRange(closestIndex, i);
}
if(lowerPoly.points.length == this.points.length || upperPoly.points.length == this.points.length) {
polyID++;
for(j = 0; j < points.length; j++) {
//Main.debugger.drawText("" + polyID + "-" + j + ": " + (at(j).x * 30).toPrecision(4) + ", " + (-4447 + at(j).y * 30).toPrecision(4), new b2Vec2(at(j).x, at(j).y), 0xFFFFFF, true);
}
return;
}
// solve smallest poly first
if (lowerPoly.points.length < upperPoly.points.length) {
lowerPoly.decompose(callback, recurseLevel + 1);
upperPoly.decompose(callback, recurseLevel + 1);
} else {
upperPoly.decompose(callback, recurseLevel + 1);
lowerPoly.decompose(callback, recurseLevel + 1);
}
return;
}
}
if(points.length >= 3) callback(this);
}
public function makeCCW():void {
var br:int = 0;
// find bottom right point
for (var i:int = 1; i < points.length; ++i) {
if (at(i).y < at(br).y || (at(i).y == at(br).y && at(i).x > at(br).x)) {
br = i;
}
}
// reverse poly if clockwise
if (!left(at(br - 1), at(br), at(br + 1))) {
points.reverse();
}
}
}
}
Thanks alot ! Now its work well for me...
Shit... Doesn't work again!
The code:
The result:

(White shape - entry figure, colored ones - decomposed polys)
As yoou see, the upper part of entry figure remains skipped...
Any help? Maybe I'm doing it wrong?
Anybody knows another decomposing scripts?
The code:
Code:
var points:Array = [new Point(-750, 300),new Point(-350, 298),new Point(121, 297),new Point(244.75, 364.25),new Point(382.5, 96.5),new Point(514.35, 52.85),new Point(334.25, 251.25),new Point(600, 300),new Point(1600, 300),new Point(1600, 710),new Point(-750, 710)];
//decomposing
var decomp:PolyDecompBayazit = new PolyDecompBayazit(points);
decomp.decompose(tracer);
function tracer(dec:PolyDecompBayazit)
{
graphics.lineStyle(20, 0xFFFFFF * Math.random(), 0.5);
graphics.moveTo(dec.points[0].x, dec.points[0].y);
for each(var p:Point in dec.points)
{
graphics.lineTo(p.x, p.y)
}
graphics.lineTo(dec.points[0].x, dec.points[0].y)
trace(dec.points.length, dec.points);
}
graphics.lineStyle(3, 0xFFFFFF, 1);
graphics.moveTo(points[0].x, points[0].y);
for each(var p:Point in points)
{
graphics.lineTo(p.x, p.y)
}
graphics.lineTo(points[0].x, points[0].y);
//decomposing
var decomp:PolyDecompBayazit = new PolyDecompBayazit(points);
decomp.decompose(tracer);
function tracer(dec:PolyDecompBayazit)
{
graphics.lineStyle(20, 0xFFFFFF * Math.random(), 0.5);
graphics.moveTo(dec.points[0].x, dec.points[0].y);
for each(var p:Point in dec.points)
{
graphics.lineTo(p.x, p.y)
}
graphics.lineTo(dec.points[0].x, dec.points[0].y)
trace(dec.points.length, dec.points);
}
graphics.lineStyle(3, 0xFFFFFF, 1);
graphics.moveTo(points[0].x, points[0].y);
for each(var p:Point in points)
{
graphics.lineTo(p.x, p.y)
}
graphics.lineTo(points[0].x, points[0].y);
The result:
Quote:
6 (x=-750, y=710),(x=-750, y=300),(x=-350, y=298),(x=121, y=297),(x=244.75, y=364.25),(x=473.9262805493945, y=710)
4 (x=244.75, y=364.25),(x=283.723357648362, y=288.4959781462872),(x=334.25, y=251.25),(x=600, y=300)
5 (x=600, y=300),(x=1600, y=300),(x=1600, y=710),(x=473.9262805493945, y=710),(x=244.75, y=364.25)
4 (x=244.75, y=364.25),(x=283.723357648362, y=288.4959781462872),(x=334.25, y=251.25),(x=600, y=300)
5 (x=600, y=300),(x=1600, y=300),(x=1600, y=710),(x=473.9262805493945, y=710),(x=244.75, y=364.25)

(White shape - entry figure, colored ones - decomposed polys)
As yoou see, the upper part of entry figure remains skipped...
Any help? Maybe I'm doing it wrong?
Anybody knows another decomposing scripts?
Um, what's changed? I've made minor changes to the decomposition code too, but I'd like the checked in copy to stay up to date.
Original thread: https://box2d.org/forum/viewtopic.php?t=4395