0

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.

z3nth10n
  • 2,341
  • 2
  • 25
  • 49
  • 1
    What happens if you do `Vector2 _v = n * (magnitude * offset) + pivot;` ? – Ruzihm Nov 20 '18 at 19:52
  • This happens: https://i.imgur.com/nIQnoxN.png, I did the following to avoid OutOfBounds exception: `(offset / magnitude) * 20f` – z3nth10n Nov 20 '18 at 19:57
  • Ok, I did what you said, and this is the output: https://i.imgur.com/vFx4VJf.png, now the problem, that this is not an offset, this is an scale. So I will try to do an offset (doing some proportion calculations) – z3nth10n Nov 20 '18 at 20:02
  • 1
    I did a cross-multiplication to the average magnitudes of all points. But now I understand what I'm doing, I can't translate 10 units every pixel, because this will give an unproportional result, instead, I should use scales. `float avgMagnitude = points.Average(v => ((Vector2)v - pivot).magnitude), scale = (offset + avgMagnitude) / avgMagnitude;` Please, post an answer if you want to be rewarded. Thanks. – z3nth10n Nov 20 '18 at 20:22
  • Glad I could help, and thanks. – Ruzihm Nov 20 '18 at 21:27

1 Answers1

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;
Ruzihm
  • 19,749
  • 5
  • 36
  • 48