public static Vector2 GetWeightedCenter(this IVertexSource vertexSource) { var polygonBounds = vertexSource.GetBounds(); int width = 128; int height = 128; // Set the transform to image space var polygonsToImageTransform = Affine.NewIdentity(); // move it to 0, 0 polygonsToImageTransform *= Affine.NewTranslation(-polygonBounds.Left, -polygonBounds.Bottom); // scale to fit cache polygonsToImageTransform *= Affine.NewScaling(width / (double)polygonBounds.Width, height / (double)polygonBounds.Height); // and move it in 2 pixels polygonsToImageTransform *= Affine.NewTranslation(2, 2); // and render the polygon to the image var imageBuffer = new ImageBuffer(width + 4, height + 4, 8, new blender_gray(1)); imageBuffer.NewGraphics2D().Render(new VertexSourceApplyTransform(vertexSource, polygonsToImageTransform), Color.White); // center for image var centerPosition = imageBuffer.GetWeightedCenter(new MapOnMaxIntensity()); // translate to vertex source coordinates polygonsToImageTransform.inverse_transform(ref centerPosition.X, ref centerPosition.Y); return(centerPosition); }
public static Vector2 GetPositionToAlignTo(IVertexSource objectToAlignTo, Side2D boundingFacesToAlignTo, Vector2 extraOffset) { Vector2 positionToAlignTo = new Vector2(); if (IsSet(boundingFacesToAlignTo, Side2D.Left, Side2D.Right)) { positionToAlignTo.X = objectToAlignTo.GetBounds().Left; } if (IsSet(boundingFacesToAlignTo, Side2D.Right, Side2D.Left)) { positionToAlignTo.X = objectToAlignTo.GetBounds().Right; } if (IsSet(boundingFacesToAlignTo, Side2D.Bottom, Side2D.Top)) { positionToAlignTo.Y = objectToAlignTo.GetBounds().Bottom; } if (IsSet(boundingFacesToAlignTo, Side2D.Top, Side2D.Bottom)) { positionToAlignTo.Y = objectToAlignTo.GetBounds().Top; } return(positionToAlignTo + extraOffset); }
private static double MaxXyDistFromCenter(IVertexSource vertexSource) { double maxDistSqrd = 0.000001; var center = vertexSource.GetBounds().Center; foreach (var vertex in vertexSource.Vertices()) { var position = vertex.position; var distSqrd = (new Vector2(position.X, position.Y) - new Vector2(center.X, center.Y)).LengthSquared; if (distSqrd > maxDistSqrd) { maxDistSqrd = distSqrd; } } return Math.Sqrt(maxDistSqrd); }
public SetCenter2D(IVertexSource item, Vector2 offset, bool onX = true, bool onY = true) { var center = item.GetBounds().Center; Vector2 consideredOffset = Vector2.Zero; // zero out anything we don't want if (onX) { consideredOffset.X = offset.X - center.X; } if (onY) { consideredOffset.Y = offset.Y - center.Y; } Transform = Affine.NewTranslation(consideredOffset); VertexSource = item; }
public Align2D(IVertexSource item, Side2D boundingFacesToAlign, Vector2 positionToAlignTo, string name = "") { var bounds = item.GetBounds(); if (IsSet(boundingFacesToAlign, Side2D.Left, Side2D.Right)) { positionToAlignTo.X = positionToAlignTo.X - bounds.Left; } if (IsSet(boundingFacesToAlign, Side2D.Right, Side2D.Left)) { positionToAlignTo.X = positionToAlignTo.X - bounds.Left - (bounds.Right - bounds.Left); } if (IsSet(boundingFacesToAlign, Side2D.Bottom, Side2D.Top)) { positionToAlignTo.Y = positionToAlignTo.Y - bounds.Bottom; } if (IsSet(boundingFacesToAlign, Side2D.Top, Side2D.Bottom)) { positionToAlignTo.Y = positionToAlignTo.Y - bounds.Bottom - (bounds.Top - bounds.Bottom); } Transform = Affine.NewTranslation(positionToAlignTo); VertexSource = item; }
public SetCenter2D(IVertexSource item, Vector2 position) { Transform = Affine.NewTranslation(position - item.GetBounds().Center); VertexSource = item; }