问题
Well, I'm currently trying to move a list of points using its center as reference with a desired offset. But I'm having problems.
I have implemented the current function:
public static IEnumerable<Point> Translate(this IEnumerable<Vector2> points, float offset, Vector2 pivot)
{
foreach (Vector2 v in points)
{
float magnitude = (v - pivot).magnitude;
Vector2 n = (v - pivot).normalized;
Vector2 _v = n * (magnitude + offset) + pivot;
yield return new Point(_v);
}
}
Note: Point
class is an own implementation very similar to System.Drawing.Point
.
I calculate the normal (the direction between the center and the current point), then I multiply by the magnitude and the offset.
And this how I call it:
Polygon pol = File.ReadAllText(PolyPath).Deserialize<Polygon>();
Point[] ps = pol.Vertices.Select(v => (v - pol.Position) + offset).ToArray();
var textureCenter = new Vector2(tex.Width / 2, tex.Height / 2); // This is 221 / 2 & 137 / 2
var es_incr = ps.Select(p => (Vector2)p).Translate(effectDistance.x, textureCenter).ToList();
// Then I draw es_incr using Bresenham algorithm: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
But I get this "fat shape":
Red pixels are tests that I'm doing.
Black pixels is the shape.
Blue pixels are the translated pixels.
But as you can see it's not proportional on x/y axis.
I have followed this two answers:
Proportional Translation
Given a start and end point, and a distance, calculate a point along a line
But in the first answer I'm lacking Matrix class on Unity3D.
So, I don't know what I'm missing.
回答1:
You need to use scales instead of translating a flat amount for every pixel, or it gets distorted like that.
float avgMagnitude = points.Average(v => ((Vector2)v - pivot).magnitude);
scale = (offset + avgMagnitude) / avgMagnitude;
来源:https://stackoverflow.com/questions/53400394/translating-transforming-list-of-points-from-its-center-with-an-offset-distance