public void subtract(Shape s) { int area = this.set.Count; Point? center = s.getCenter(); int cx = center.Value.X; int cy = center.Value.Y; foreach (Point? p in s.set) { this.set.Remove(p); } this.sumX -= area * cx; this.sumY -= area * cy; // BIG WARNING! Margin points and coords are not moved back. }
/** * Extracts a horizontal or vertical shape of the original Shape. * The area of the new, smaller flat shape will be smaller thatn the areaLimit. * sizeLimit specifies the width of the flat, extracted shape. */ public static Shape reduceShapeHVAreaLimit(Shape originalShape, Point? firstPoint, int sizeLimit, bool horizontal, int areaLimit) { int px, py, nx, ny; int[] neighx = { -1, 0, 1, -1, 1, -1, 0, 1 }, neighy = { -1, -1, 1, 0, 0, 1, 1, 1 }; Shape reducedShape = new Shape(); Queue<Point?> queue = new Queue<Point?>(); if (originalShape == null) { return null; } if (!originalShape.contains(firstPoint)) { return null; } queue.Enqueue(firstPoint); while (true) { Point? head = queue.Dequeue(); reducedShape.Add(head); int reducedArea = reducedShape.Area; Point? shapeCenter = reducedShape.getCenter(); px = head.Value.X; py = head.Value.Y; for (int i = 0; i < 8; i++) { nx = px + neighx[i]; ny = py + neighy[i]; Point? neighbour = new Point(nx, ny); if ((originalShape.contains(neighbour)) && (!reducedShape.contains(neighbour)) && (!queue.Contains(neighbour)) && (queue.Count() + reducedArea < areaLimit) && (((horizontal) && (Math.Abs(nx - shapeCenter.Value.X) < sizeLimit)) || ((!horizontal) && (Math.Abs(ny - shapeCenter.Value.Y) < sizeLimit))) ) { queue.Enqueue(neighbour); } } if ((reducedArea > areaLimit) || (queue.Count == 0)) { break; } } return reducedShape; }