翻转顶点数组

发布于 2024-10-23 18:51:04 字数 493 浏览 1 评论 0原文

我有一个组成二维多边形的位置顶点数组。

        Vector2[] _chassisConcaveVertices =
        {
            new Vector2(5.122f, 0.572f),
            new Vector2(3.518f, 0.572f),
            new Vector2(3.458f, 0.169f),
            new Vector2(2.553f, 0.169f),
            new Vector2(2.013f, 0.414f),
            new Vector2(0.992f, 0.769f),
            new Vector2(0.992f, 1.363f),
            new Vector2(5.122f, 1.363f),
        };

我可以使用什么算法来修改位置以便翻转生成的多边形?我需要水平和垂直翻转多边形。

I have an array of position vertices that make up a 2D polygon.

        Vector2[] _chassisConcaveVertices =
        {
            new Vector2(5.122f, 0.572f),
            new Vector2(3.518f, 0.572f),
            new Vector2(3.458f, 0.169f),
            new Vector2(2.553f, 0.169f),
            new Vector2(2.013f, 0.414f),
            new Vector2(0.992f, 0.769f),
            new Vector2(0.992f, 1.363f),
            new Vector2(5.122f, 1.363f),
        };

What algorithm can I use to modify the positions so the resultant polygon is flipped? I need to flip the polygon both horizontally and vertically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无需解释 2024-10-30 18:51:04

假设 Vector2 类有两个属性/成员名称 xy

    public Vector2[] FlipHorizontally(Vector2[] original)
    {
        Vector2[] flipped = new Vector2[original.Length];
        for (int i = 0; i < original.Length; i++)
        {
            flipped[i] = new Vector2(-1 * original[i].x, original[i].y);
        }
        return flipped;
    }

    public Vector2[] FlipVertically(Vector2[] original)
    {
        Vector2[] flipped = new Vector2[original.Length];
        for (int i = 0; i < original.Length; i++)
        {
            flipped[i] = new Vector2(original[i].x, -1 * original[i].y);
        }
        return flipped;
    }

这将相对于两个轴翻转顶点。您无需指定新多边形相对于原始多边形的定位。如果您需要“移动”它,那么您只需向所有 x 添加一个值,并向所有 y 添加另一个值。

Assuming the Vector2 class has two properties/members names x and y:

    public Vector2[] FlipHorizontally(Vector2[] original)
    {
        Vector2[] flipped = new Vector2[original.Length];
        for (int i = 0; i < original.Length; i++)
        {
            flipped[i] = new Vector2(-1 * original[i].x, original[i].y);
        }
        return flipped;
    }

    public Vector2[] FlipVertically(Vector2[] original)
    {
        Vector2[] flipped = new Vector2[original.Length];
        for (int i = 0; i < original.Length; i++)
        {
            flipped[i] = new Vector2(original[i].x, -1 * original[i].y);
        }
        return flipped;
    }

This will flip the vertices in relation to the two axis. You do not specify what positioning you intend for the new polygon in relation to the original. If you need to "move" it then you simply need to add one value to all x and another value to all y.

素染倾城色 2024-10-30 18:51:04

如果您围绕点 (0.0f,0.0f) 翻转,您只需对这些值求负即可。所以你的形状将是:

    Vector2[] _chassisConcaveVertices =
    {
        new Vector2(-5.122f, -0.572f),
        new Vector2(-3.518f, -0.572f),
        new Vector2(-3.458f, -0.169f),
        new Vector2(-2.553f, -0.169f),
        new Vector2(-2.013f, -0.414f),
        new Vector2(-0.992f, -0.769f),
        new Vector2(-0.992f, -1.363f),
        new Vector2(-5.122f, -1.363f),
    };

如果你围绕一个点 (x,y) 翻转,那么每个点的 x 值将是 (x - (px - x)) 或 (2*xp.x) ,而 (y - (py) - y)) 或 (2*yp.y) 表示 y 值。

这解释了:
。是你想要翻转的点
* 是您要翻转的点
O 是您想要最终得到的点

  x axis
    ^
    |
    . -
    | | <-
    | | <- Let this be distance a
    * -
    | | <-
    | | <- This should be equal to a
    O -
    |
    |
    -------> y axis

假设 的 x 值。 * 和 O 分别是 t、m 和 b(顶部、中间和底部)。如您所见,距离 a = tm 且 b = ma。因此 b = m-(tm) = m-t+m = m*2-t

然后您可以使用这个原理编写一个算法来翻转不同点周围的所有点,这将给您翻转的多边形!

If you're flipping around the point (0.0f,0.0f) you simply need to negate the values. So your shape would be:

    Vector2[] _chassisConcaveVertices =
    {
        new Vector2(-5.122f, -0.572f),
        new Vector2(-3.518f, -0.572f),
        new Vector2(-3.458f, -0.169f),
        new Vector2(-2.553f, -0.169f),
        new Vector2(-2.013f, -0.414f),
        new Vector2(-0.992f, -0.769f),
        new Vector2(-0.992f, -1.363f),
        new Vector2(-5.122f, -1.363f),
    };

If you are flipping around a point (x,y) then each point will be (x - (p.x - x)) or (2*x-p.x) for the x value and (y - (p.y - y)) or (2*y-p.y) for the y value.

This explains:

. is the point you want to flip

* is the point you want to flip around
O is the point you want to end up with

  x axis
    ^
    |
    . -
    | | <-
    | | <- Let this be distance a
    * -
    | | <-
    | | <- This should be equal to a
    O -
    |
    |
    -------> y axis

Let's say the x values of . * and O are t, m and b respectively (top, middle and bottom). As you can see, the distance a = t-m and the b = m-a. Therefore b = m-(t-m) = m-t+m = m*2-t

You can then use this principle to write an algorithm to flip all the points around a different point and this will give you your flipped polygon!

往事风中埋 2024-10-30 18:51:04

首先,你必须取多边形内切圆周的中心,然后取垂直和水平穿过中心的线,对于每个点,首先在垂直线的另一侧取对称线,然后重复该操作对于水平线另一侧的对称点,所有点都会翻转。

再见!

First you have to take the center of the circumference that inscribes the polygon, then take the line that goes vertical and horitzontal across the center, for each point, first, take the symetric at the other side of the vertical line, then repeat the action for the symetric point at the other side of the horitzontal, you will have all point flipped.

See you!

沫雨熙 2024-10-30 18:51:04

不确定是否有“正确”的方法,但是像下面这样的方法应该可以水平和垂直地翻转多边形。

  1. 通过将 x 点和 y 点的平均值相加来找到中心点。
  2. 对于多边形中的每个点,计算距中心点的垂直和水平距离,并将每个值加倍到该点。

未经测试的示例:

    Vector2[] _chassisConcaveVertices =
    {
        new Vector2(5.122f, 0.572f),
        new Vector2(3.518f, 0.572f),
        new Vector2(3.458f, 0.169f),
        new Vector2(2.553f, 0.169f),
        new Vector2(2.013f, 0.414f),
        new Vector2(0.992f, 0.769f),
        new Vector2(0.992f, 1.363f),
        new Vector2(5.122f, 1.363f),
    };

    // find center
    float sumX = 0;
    float sumY = 0;
    foreach (var vector in _chassisConcaveVertices)
    {
        sumX += vector.X;
        sumY += vector.Y;
    }
    Vector2 center = new Vector2(
        sumX / _chassisConcaveVertices.Length, 
        sumY / _chassisConcaveVertices.Length);

    // create a new version of the polygon flipped
    Vector2[] flipped = _chassisConcaveVertices
        .Select(v => new Vector2(
            v.X + (center.X - v.X) * 2, 
            v.Y + (center.Y - v.Y) * 2))
        .ToArray();

Not sure if there is a "correct" way, but something like the below should work to flip a polygon in place, both horizontally and vertically.

  1. Find the center point by adding averaging x points and y points.
  2. For each point in the polygon, calculate the vertical and horizontal distance distance from the center point and add double each value to the point.

Untested example:

    Vector2[] _chassisConcaveVertices =
    {
        new Vector2(5.122f, 0.572f),
        new Vector2(3.518f, 0.572f),
        new Vector2(3.458f, 0.169f),
        new Vector2(2.553f, 0.169f),
        new Vector2(2.013f, 0.414f),
        new Vector2(0.992f, 0.769f),
        new Vector2(0.992f, 1.363f),
        new Vector2(5.122f, 1.363f),
    };

    // find center
    float sumX = 0;
    float sumY = 0;
    foreach (var vector in _chassisConcaveVertices)
    {
        sumX += vector.X;
        sumY += vector.Y;
    }
    Vector2 center = new Vector2(
        sumX / _chassisConcaveVertices.Length, 
        sumY / _chassisConcaveVertices.Length);

    // create a new version of the polygon flipped
    Vector2[] flipped = _chassisConcaveVertices
        .Select(v => new Vector2(
            v.X + (center.X - v.X) * 2, 
            v.Y + (center.Y - v.Y) * 2))
        .ToArray();
想你只要分分秒秒 2024-10-30 18:51:04

水平翻转的测试示例。
支持碰撞器中的多个路径:
路径相对于碰撞器的中心位置是镜像的。

void FlipHorizontally(PolygonCollider2D tpolygon)
{
    // find centre X
    float sumX = 0;
    foreach (Vector2 v2 in tpolygon.points)
        sumX += v2.x;
    float centreX = sumX / tpolygon.points.Length;

    // Flip each path in the polygon
    for (int k = 0; k < tpolygon.pathCount; k++)
    {
        List<Vector2> mirrored = new List<Vector2>();
        List<Vector2> pathPoints = new List<Vector2>(tpolygon.GetPath(k));
        for (int i = 0; i < pathPoints.Count; i++)
        {
            Vector2 v2 = pathPoints[i];
            float xx = v2.x - 2 * (v2.x - centreX);
            mirrored.Add(new Vector2(xx, v2.y));
        }
        tpolygon.SetPath(k, mirrored);
    }
    Debug.Log("flipped paths: " + tpolygon.pathCount);

}

Tested example for horizontal flip.
Supports multiple paths in a collider:
The paths are mirrored relative to centre position of the collider.

void FlipHorizontally(PolygonCollider2D tpolygon)
{
    // find centre X
    float sumX = 0;
    foreach (Vector2 v2 in tpolygon.points)
        sumX += v2.x;
    float centreX = sumX / tpolygon.points.Length;

    // Flip each path in the polygon
    for (int k = 0; k < tpolygon.pathCount; k++)
    {
        List<Vector2> mirrored = new List<Vector2>();
        List<Vector2> pathPoints = new List<Vector2>(tpolygon.GetPath(k));
        for (int i = 0; i < pathPoints.Count; i++)
        {
            Vector2 v2 = pathPoints[i];
            float xx = v2.x - 2 * (v2.x - centreX);
            mirrored.Add(new Vector2(xx, v2.y));
        }
        tpolygon.SetPath(k, mirrored);
    }
    Debug.Log("flipped paths: " + tpolygon.pathCount);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文