翻转顶点数组
我有一个组成二维多边形的位置顶点数组。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设
Vector2
类有两个属性/成员名称x
和y
:这将相对于两个轴翻转顶点。您无需指定新多边形相对于原始多边形的定位。如果您需要“移动”它,那么您只需向所有
x
添加一个值,并向所有y
添加另一个值。Assuming the
Vector2
class has two properties/members namesx
andy
: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 ally
.如果您围绕点 (0.0f,0.0f) 翻转,您只需对这些值求负即可。所以你的形状将是:
如果你围绕一个点 (x,y) 翻转,那么每个点的 x 值将是 (x - (px - x)) 或 (2*xp.x) ,而 (y - (py) - y)) 或 (2*yp.y) 表示 y 值。
这解释了:
。是你想要翻转的点
* 是您要翻转的点
O 是您想要最终得到的点
假设 的 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:
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
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!
首先,你必须取多边形内切圆周的中心,然后取垂直和水平穿过中心的线,对于每个点,首先在垂直线的另一侧取对称线,然后重复该操作对于水平线另一侧的对称点,所有点都会翻转。
再见!
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!
不确定是否有“正确”的方法,但是像下面这样的方法应该可以水平和垂直地翻转多边形。
未经测试的示例:
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.
Untested example:
水平翻转的测试示例。
支持碰撞器中的多个路径:
路径相对于碰撞器的中心位置是镜像的。
Tested example for horizontal flip.
Supports multiple paths in a collider:
The paths are mirrored relative to centre position of the collider.