如何通过翻转数组来翻转多边形?
我有 2 个整数数组,用于创建一个多边形(看起来像一条鱼)。我需要对数组做什么才能水平翻转多边形?
x = new int[]
{ 0, 18, 24, 30, 48, 60, 60, 54, 60, 48, 30, 24, 0 };
y = new int[]
{ 0, 18, 6, 0, 0, 12, 18, 24, 24, 36, 36, 30, 36 };
I have 2 arrays of ints I am using to create a polygon (that looks like a fish). What do I need to do to the arrays to flip the polygon horizontally?
x = new int[]
{ 0, 18, 24, 30, 48, 60, 60, 54, 60, 48, 30, 24, 0 };
y = new int[]
{ 0, 18, 6, 0, 0, 12, 18, 24, 24, 36, 36, 30, 36 };
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要找到
x
数组的最大值。在本例中为60
。然后使用循环将每个 x 坐标设置为 60 - x,如下所示:You need to find the maximum value of the
x
array. In this case it is60
. Then set each x coordinate to60 - x
using a loop, like this:使用以下公式修改 x 坐标 x = 60 - x
Modify x coordinate using this formulae x = 60 - x
http://en.wikipedia.org/wiki/Rotation_matrix
http://en.wikipedia.org/wiki/Rotation_matrix
我认为你所说的“翻转”的意思是让一条从左到右的鱼变成从右到左的鱼。这意味着您有效地反映了线 x=a 周围的鱼,其中 a 是鱼中点的水平坐标。在这种情况下,a=(max(x[])-min(x[]))/2。
对于每个点,我们检查它是否位于 x=a 的左侧或右侧。如果它在左侧,我们简单地更改它,使其现在与右侧的距离相同,否则我们对其进行更改,使其与左侧的距离相同。
我认为以下(未经测试的)代码可以工作。我将所有值保留为整数,因此可能会出现轻微的失真。但调整代码应该很容易,直到失真消失。
I think what you mean by 'flip' is to make a fish that is headed left to right change into one that is right to left. This means that you are effectively reflecting the fish around the line x=a, where a is the horizontal coordinate of the mid point of the fish. In this case a=(max(x[])-min(x[]))/2.
For each point, we check if it is to the left or right of x=a. If it is to the left, we simple change it so that it is now the same distance to the right, otherwise we change it so that it is to the same distance to the left.
I think the following (untested) code will work. I'm keeping all values as ints, so a slight distortion might occur. But it should be easy to tweak the code until the distortion disappears.
您可能会发现使用图形类的平移和缩放方法比操作数组内容更容易。
You might find it easier to use the graphics class' translate and scale methods rather than manipulating the array contents.
如果您使用 Point2D 抽象而不是两个整数数组,您可能会发现这要容易得多。这些不是独立的实体,而是相关的。那么为什么你写的代码就好像它们根本没有关系一样呢?你的点和多边形类在哪里?抽象在哪里?
如果我理解你所说的水平“翻转”的意思,我认为你想要关于 y 轴的反射。如果这是真的,您所要做的就是更改所有 x 坐标的符号,然后就完成了。
因此,具有端点 A (xa, ya) 和 B (xb, yb) 的向量变为 (-xa, ya) 和 (-xb, yb)。
You might find this a lot easier if you were using a Point2D abstraction instead of two arrays of ints. These aren't separate entities, they're related. So why are you writing code as if they had no relationship at all? Where are your Point and Polygon classes? Where's the abstraction?
If I understand what you mean by "flipping" horizontally, I think you want reflection about the y-axis. If that's true, all you have to do is change the signs of all the x-coordinates and you're done.
So a vector with endpoints A (xa, ya) and B (xb, yb) becomes (-xa, ya) and (-xb, yb).