コード例 #1
0
ファイル: Voronoi.cs プロジェクト: mikebrasher/RoboNav
        private void DeleteNodes(Site site, Node seed)
        {
            Tree <Node> deleted = new Tree <Node>(seed);
            Tree <Node> next    = deleted.GetNext();

            while (next != null)
            {
                Node node = next.Node;
                nodes.Remove(node);
                next.IsLeaf = true;
                List <Edge> edgeList = new List <Edge>(node.Edges); // save a copy to loop over so we can modify node.edges
                foreach (Edge e in edgeList)
                {
                    Node other = e.OtherNode(node);
                    if (other != null)
                    {
                        if (site.Distance(other.Point) < other.Clearance)
                        {
                            next.AddChild(other);
                        }
                        else
                        {
                            CartesianList <Point> intersections = new CartesianList <Point>();
                            foreach (Site side in e.Sites)
                            {
                                List <Curve> bisectors = side.GetBisectors(site);
                                foreach (Curve b in bisectors)
                                {
                                    CartesianList <Point> sideIntersection = e.Curve.Intersect(b);
                                    foreach (Point x in sideIntersection)
                                    {
                                        if (site.Influences(x) && side.Influences(x))
                                        {
                                            intersections.AddNear(x);
                                        }
                                    }
                                }
                            }

                            foreach (Point x in intersections)
                            {
                                Node newNode = AddNodeNear(x);

                                if (newNode == other)
                                {
                                    other.Connected = false;
                                }
                                else
                                {
                                    new Edge(e.Left, e.Right, e.Curve, other, newNode, edgePrefab, cylinders);
                                }
                            }
                        }

                        e.RemoveFromNodes();
                    }
                }

                next = deleted.GetNext();
            }
        }
コード例 #2
0
        public void Generate()
        {
            wall2point = new Point[maze.Length, 4];

            for (int iwall = 0; iwall < maze.Length; ++iwall)
            {
                Transform wall = maze[iwall];

                for (int icorner = 0; icorner < 4; ++icorner)
                {
                    Point p = Corner(wall, icorner);

                    if (points.ContainsNear(p))
                    {
                        p = points.GetNear(p);
                    }
                    else
                    {
                        points.Add(p);

                        List <WallIndex> walls = new List <WallIndex>();
                        point2wall.Add(p, walls);
                    }

                    point2wall[p].Add(new WallIndex(iwall, icorner));
                    wall2point[iwall, icorner] = p;
                }
            }

            WallIndex currentWall = new WallIndex(0, 2);
            Point     current     = vertices.AddNear(LookupPoint(currentWall));
            Point     first       = current;

            Point     prev     = null;
            WallIndex peekWall = null;
            Point     peek     = null;

            for (int ii = 0; ii < 100; ++ii)
            {
                prev = current;

                currentWall = FindNextWall(currentWall).Rotate;

                current = LookupPoint(currentWall);
                if (current.IsCoincident(first))
                {
                    edges.Add(new PolygonEdge(prev, first));
                    break;
                }

                // combine parallel wall edges
                for (int jj = 0; jj < 5; ++jj)
                {
                    peekWall = FindNextWall(currentWall).Rotate;

                    peek = LookupPoint(peekWall);
                    Vector v0 = current - prev;
                    Vector v1 = peek - current;

                    if (v1.IsParallel(v0)) // (peek - current).IsParallel(current - prev))
                    {
                        currentWall = peekWall;
                        current     = peek;
                    }
                    else
                    {
                        break;
                    }
                }

                current = vertices.AddNear(current);
                edges.Add(new PolygonEdge(prev, current));
            }

            DetermineBounds();
        }