翻译/转换?距中心点的列表,具有偏移量/距离 [英] Translating/transforming? list of points from its center with an offset/distance

查看:120
本文介绍了翻译/转换?距中心点的列表,具有偏移量/距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我目前正在尝试将点列表以其中心作为参考,并以所需的偏移量进行移动.但是我遇到了问题.

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);
        }
    }

注意: Point类是一个自己的实现,与System.Drawing.Point非常相似.

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.

这就是我所说的:

        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.

黑色像素是形状.

蓝色像素是翻译后的像素.

Blue pixels are the translated pixels.

但是您可以看到它在x/y轴上不成比例.

But as you can see it's not proportional on x/y axis.

我遵循了以下两个答案:

I have followed this two answers:

比例翻译

给出起点和终点以及距离,沿直线计算一个点

但是在第一个答案中,我在Unity3D上缺少Matrix类.

But in the first answer I'm lacking Matrix class on Unity3D.

所以,我不知道我缺少什么.

So, I don't know what I'm missing.

推荐答案

您需要使用比例尺,而不是为每个像素平移量,否则它会像这样失真.

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;

这篇关于翻译/转换?距中心点的列表,具有偏移量/距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆