/// <summary> /// Determines the boundaries of the grids non-null content. /// </summary> /// <returns></returns> public Rectangle GetContentBoundaries() { Rectangle bounds = new Rectangle(this.width, this.height, 0, 0); int count = this.sequence.Count; T[] data = this.sequence.Data; for (int i = 0; i < count; i++) { if (GenericOperator.Equal(data[i], default(T))) { continue; } int cX = i % this.width; int cY = i / this.width; bounds.X = Math.Min(bounds.X, cX); bounds.Y = Math.Min(bounds.Y, cY); bounds.Width = Math.Max(bounds.Width, cX); bounds.Height = Math.Max(bounds.Height, cY); } bounds.Width = 1 + Math.Max(0, bounds.Width - bounds.X); bounds.Height = 1 + Math.Max(0, bounds.Height - bounds.Y); return(bounds); }
/// <summary> /// Copies the grids contents to the specified other grid. /// </summary> /// <typeparam name="U"></typeparam> /// <param name="target"></param> /// <param name="destX"></param> /// <param name="destY"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="srcX"></param> /// <param name="srcY"></param> /// <param name="selector"></param> public void CopyTo <U>(Grid <U> target, int destX = 0, int destY = 0, int width = -1, int height = -1, int srcX = 0, int srcY = 0, Func <T, U, U> selector = null) { if (width == -1) { width = this.width; } if (height == -1) { height = this.height; } int beginX = MathF.Max(0, -destX, -srcX); int beginY = MathF.Max(0, -destY, -srcY); int endX = MathF.Min(width, this.width, target.width - destX, this.width - srcX); int endY = MathF.Min(height, this.height, target.height - destY, this.height - srcY); if (endX - beginX < 1) { return; } if (endY - beginY < 1) { return; } U[] targetData = target.sequence.Data; T[] sourceData = this.sequence.Data; if (selector != null) { for (int i = beginX; i < endX; i++) { for (int j = beginY; j < endY; j++) { int sourceN = srcX + i + this.width * (srcY + j); int targetN = destX + i + target.width * (destY + j); targetData[targetN] = selector(sourceData[sourceN], targetData[targetN]); } } } else { for (int i = beginX; i < endX; i++) { for (int j = beginY; j < endY; j++) { int sourceN = srcX + i + this.width * (srcY + j); int targetN = destX + i + target.width * (destY + j); targetData[targetN] = GenericOperator.Convert <T, U>(sourceData[sourceN]); } } } }
/// <summary> /// Determines the boundaries of the grids non-null content. /// </summary> /// <returns></returns> public void GetContentBoundaries(out Point2 topLeft, out Point2 size) { topLeft = new Point2(this.width, this.height); size = new Point2(0, 0); int count = this.sequence.Count; T[] data = this.sequence.Data; for (int i = 0; i < count; i++) { if (GenericOperator.Equal(data[i], default(T))) { continue; } int cX = i % this.width; int cY = i / this.width; topLeft.X = Math.Min(topLeft.X, cX); topLeft.Y = Math.Min(topLeft.Y, cY); size.X = Math.Max(size.X, cX); size.Y = Math.Max(size.Y, cY); } size.X = 1 + Math.Max(0, size.X - topLeft.X); size.Y = 1 + Math.Max(0, size.Y - topLeft.Y); }