private static MaskF Transform(MaskF mask, Matrix3x2 transformation) { var original = mask.UnderlayingArray; var transformedPoints = new Vector2[original.Length]; // Initializing the transformed bounds double sumX = 0; double sumY = 0; var first = Vector2.Transform(original[0], transformation); float left = first.X; float top = first.Y; float right = first.X; float bottom = first.Y; // Calculating the transformed coordinates for (int i = 0; i < original.Length; i++) { var pt = Vector2.Transform(original[i], transformation); sumX += pt.X; sumY += pt.Y; UpdateBoundaries(pt, ref left, ref top, ref right, ref bottom); transformedPoints[i] = pt; } Vector2 transformedCenter = new Vector2((float)(sumX / original.Length), (float)(sumY / original.Length)); MaskF transformedMask = FromArrayAndProperties(transformedPoints, transformedCenter, left, top, right, bottom); return transformedMask; }
// public enum CoordinateColisionOption { AllowCollisions, AssignToEmptyNeighbor } public static Mask ConvertToMask(MaskF mask) { if (mask == null) throw new ArgumentNullException(nameof(mask)); var original = mask.UnderlayingArray; var usedPoints = new HashSet<Point>(); for (int i = 0; i < original.Length; i++) { var convertedPoint = original[i].ToPoint(); if (!usedPoints.Add(convertedPoint)) { // Testing the 8 closest coordinates int x = convertedPoint.X; int y = convertedPoint.Y; if (usedPoints.Add(new Point(x - 1, y - 1)) || usedPoints.Add(new Point(x - 1, y)) || usedPoints.Add(new Point(x - 1, y + 1)) || usedPoints.Add(new Point(x, y - 1)) || usedPoints.Add(new Point(x, y + 1)) || usedPoints.Add(new Point(x + 1, y - 1)) || usedPoints.Add(new Point(x + 1, y)) || usedPoints.Add(new Point(x + 1, y + 1)) ) continue; } } var convertedMask = new Mask(usedPoints); return convertedMask; }
public static MaskF Translate(MaskF mask, float xOffset, float yOffset) { if (mask == null) throw new ArgumentNullException(nameof(mask)); var translation = Matrix3x2.CreateTranslation(xOffset, yOffset); return Transform(mask, translation); }
/// <remarks>The rotation is clockwise.</remarks> public static MaskF Rotate(MaskF mask, float radians, Vector2 center) { if (mask == null) throw new ArgumentNullException(nameof(mask)); var rotation = Matrix3x2.CreateRotation(radians, center); return Transform(mask, rotation); }