C# - 将已经相邻的矩形合并为顺时针多边形点列表? [英] C# - merge already adjacent rectangles into a clockwise polygon point list?

查看:181
本文介绍了C# - 将已经相邻的矩形合并为顺时针多边形点列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject!



我已经制作了一个洪水填充递归函数,以便找到阵列中彼此相邻的所有瓦片。我想使用这个瓦片列表来使用它们的矩形信息制作一个多边形 - 多边形需要以顺时针顺序结束,但是,每次我尝试这样做时我遇到一个错误我似乎没有理解。



我所做的所有尝试都是不成功的,这真的让我很烦。没有例外,但是,由于点数顺序错误或位置完全错误,所有多边形最终都无效。



瓷砖没有按X或Y排序。我错过了什么吗?



我尝试了什么:



我尝试过的是像这样运行列表:



Hello Codeproject!

I've made a flood fill recursive function in order to find all tiles in an array that are adjacent to one another. I'd like to use this list of tiles to make a polygon using their rectangle information - the polygon needs to end up being in a clockwise order, however, each time I try and do this I run into an error I do not seem to understand.

All the attempts I've made have been unsuccesful and it's really bothering me. No exceptions are thrown, however, all the polygons end up being invalid due to the points being either in the wrong order, or in the completely wrong position.

The tiles are not sorted by X, or Y. Am I missing something?

What I have tried:

What I've tried doing is run through the list like so:

for (int i =0; i<TileList.Count; i++) {


    PointList.AddRange(new Vector2[] {
            new Vector2(TileList[i].BoundingBox.X, TileList[i].BoundingBox.Y),
            new Vector2(TileList[i].BoundingBox.X + TileList[i].BoundingBox.Width, TileList[i].BoundingBox.Y),
            new Vector2(TileList[i].BoundingBox.X + TileList[i].BoundingBox.Width, TileList[i].BoundingBox.Y + TileList[i].BoundingBox.Height),
            new Vector2(TileList[i].BoundingBox.X, TileList[i].BoundingBox.Y + TileList[i].BoundingBox.Height),
    });


}







for(int i =0; i<PointList.Count; i++)
{
    for (int j = 0; j < PointList.Count; j++)
    {
        if(PointList[i] != PointList[j])
        {
            if (Vector2.Distance(PointList[i], PointList[j]) < 2)
            {
                PointList.Remove(PointList[i]);
                PointList.Remove(PointList[j]);
            }
        }
    }

}

PointList = PointList.OrderBy(p => Math.Atan2(p.X, p.Y)).ToList();

推荐答案

您需要做的是获取相关矩形的点列表,并确定它们的角度中心参考点。一旦你这样做,你可以从最低值到最高值对角度进行排序,并按顺序处理每个角度(基本上是顺时针方向)。



我会留下你相当的谷歌技能,以了解如何计算角度。
What you need to do is get a list of points of the rectangles in question, and determine their angle from a central reference point. Once you do that, you can sort the angles from lowest to highest value, and process each one in that order (essentially, in a clockwise direction).

I will leave it to your considerable google skills to find out how to calculate the angles.


你需要分析你的问题。

通过相邻,据我所知,2个矩形共享一个共同的一面。

取一些积分:

You need to do an analyze of your problem.
By adjacent, I understand that 2 rectangles are sharing a common side.
Take some points:
r
A  B  C
D  E  F



这给出了矩形ABED和BCFE。



检查2个矩形是否是相邻与否,你需要在两个矩形之间找到共同的2点。

然后,一旦你知道他们分享积分,你必须合并它们并获得ACFD。

您需要设置一种方法,保证合并后,所有点都将保持顺时针。




this gives rectangles ABED and BCFE.

To check if the 2 rectangles are adjacent or not, you need to find common 2 points between both rectangles.
Then, once you know they share the points, you have to merge them and get ACFD.
You need to device a method that guaranty that after merging, all points will remain clockwise.

PointList.Remove(PointList[i]);
PointList.Remove(PointList[j]);



删除有下一步移动的副作用元素下来。要了解代码是如何出错的,请使用调试器并注意列表中的更改。



当您不了解代码的作用或原因时它完成了它的工作,答案是调试器

使用调试器查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


remove have the side effect of moving next elements down. To understand how the code go wrong, use the debugger and pay attention to the changes in the list.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于C# - 将已经相邻的矩形合并为顺时针多边形点列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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