背景
我正在开发一个房间绘图工具应用程序,用户可以在其中绘制墙壁以形成房间。但是,我无法弄清楚如何以有效且有效的方式检测新房间。 每个房间都包含以下内容: -点列表(点商店连接墙) - 墙壁列表(墙壁存储头尾点) -地面 在封闭的墙壁上检测到第一个房间,通过跳过墙壁收集所有点,直到再次找到第一个点,以便所有点也按顺时针排序。 [A,B,C,D][1,2,3,4]
问题
现在用户可以画新的墙了,如果在房间里面画墙,很容易分割房间。但是如果他们在房间外面画墙,就很难找到检测新房间的路线。例如: 假设用户在左下角绘制了一个新房间,从新点 E(在墙 4 上的 D、A 之间)开始,在点 I(在墙 3 上的 C、D 之间)结束 如果我使用与检测第一个房间相同的方法,可能会出现异常结果。因此,我需要一种新方法或修复旧方法。
为了检测第一个房间,我这样编码:
void DetectClosing(Point endPoint) //Try to find closed walls to generate floor
{
//To-do: detect expand new room or split room
//Detect from onDrag, inside = split, outside = expand
if (floors.Count == 0)
{
List<Point> Points = new List<Point>();
Point target = endPoint;
Points.Add(target);
Room room = new Room();
Wall walltarget = target.walls[0];
room.walls.Add(walltarget);
while (true)
{
foreach (var item in walltarget.points) //Find the next point from current wall
{
if (item == target) continue; //Wall has two points, skip self
target = item;
print("Next point");
break;
}
if (target == wallPoints[0]) break; //End of while-loop upon reaching the first point again
Points.Add(target);
foreach (var item in target.walls) //Find the next wall from new point, must edit
{
if (item == walltarget) continue; //Point has two or more walls, skip self
walltarget = item;
print("Next wall");
break;
}
}
room.points = MeshManager.Instance.points = wallPoints;
room.floor = MeshManager.Instance.CreateFloor();
}
}
观察,新房间应该是[E,D,I,H,G,F][9,10,8,7,6,5] (点和墙可能有不同的顺序,但应顺时针排序) 第一个房间添加了点 E、I 和墙 9,10.
但是,我对获取检测房间的路径感到困惑。我们可以看到 E 点和 I 有 3 面墙,这意味着房间可以是 [E,D,I,H,G,F] 或 [E,A,B,C,I,H,G,F, E]
该方法必须获得房间 [E,D,I,H,G,F] 并正确检测新房间。