コード例 #1
0
 static bool MyTest(Edge edge, int index, List<Edge> vector)
 {
     BitmapData delaunayLineBmp = edge.MakeDelaunayLineBmp();
     bool notIntersecting = !(_keepOutMask.hitTest(zeroPointF, 1, delaunayLineBmp, zeroPointF, 1));
     delaunayLineBmp.dispose();
     return notIntersecting;
 }
コード例 #2
0
    public static void DrawLine(Delaunay.Edge <MapNode> edge, Color color)
    {
#if UNITY_EDITOR
        Gizmos.color = color;
        Gizmos.DrawLine(new Vector3(edge.Point1.Position.x, edge.Point1.Position.y, 0),
                        new Vector3(edge.Point2.Position.x, edge.Point2.Position.y, 0));
#endif
    }
コード例 #3
0
        private void FortunesAlgorithm()
        {
            Site     newSite, bottomSite, topSite, tempSite;
            Vertex   v, vertex;
            Point    newintstar = default(Point);
            Side     leftRight;
            Halfedge lbnd, rbnd, llbnd, rrbnd, bisector;
            Edge     edge;

            Rect dataBounds = _sites.GetSitesBounds();

            int sqrt_nsites            = (int)(Math.Sqrt(_sites.Count + 4));
            HalfedgePriorityQueue heap = new HalfedgePriorityQueue(dataBounds.Y, dataBounds.Height, sqrt_nsites);
            EdgeList        edgeList   = new EdgeList(dataBounds.X, dataBounds.Width, sqrt_nsites);
            List <Halfedge> halfEdges  = new List <Halfedge>();
            List <Vertex>   vertices   = new List <Vertex>();

            fortunesAlgorithm_bottomMostSite = _sites.Next();
            newSite = _sites.Next();

            for (; ;)
            {
                if (heap.Empty() == false)
                {
                    newintstar = heap.Min();
                }

                if (newSite != null &&
                    (heap.Empty() || CompareByYThenX(newSite, newintstar) < 0))
                {
                    /* new site is smallest */
                    //trace("smallest: new site " + newSite);

                    // Step 8:
                    lbnd = edgeList.EdgeListLeftNeighbor(newSite.Coord); // the Halfedge just to the left of newSite
                    //trace("lbnd: " + lbnd);
                    rbnd = lbnd.edgeListRightNeighbor;                   // the Halfedge just to the right
                    //trace("rbnd: " + rbnd);
                    bottomSite = FortunesAlgorithm_rightRegion(lbnd);    // this is the same as leftRegion(rbnd)
                    // this Site determines the region containing the new site
                    //trace("new Site is in region of existing site: " + bottomSite);

                    // Step 9:
                    edge = Edge.CreateBisectingEdge(bottomSite, newSite);
                    //trace("new edge: " + edge);
                    _edges.Add(edge);

                    bisector = Halfedge.Create(edge, Side.LEFT);
                    halfEdges.Add(bisector);
                    // inserting two Halfedges into edgeList constitutes Step 10:
                    // insert bisector to the right of lbnd:
                    edgeList.Insert(lbnd, bisector);

                    // first half of Step 11:
                    if ((vertex = Vertex.Intersect(lbnd, bisector)) != null)
                    {
                        vertices.Add(vertex);
                        heap.Remove(lbnd);
                        lbnd.vertex = vertex;
                        lbnd.ystar  = vertex.y + newSite.Dist(vertex);
                        heap.Insert(lbnd);
                    }

                    lbnd     = bisector;
                    bisector = Halfedge.Create(edge, Side.RIGHT);
                    halfEdges.Add(bisector);
                    // second Halfedge for Step 10:
                    // insert bisector to the right of lbnd:
                    edgeList.Insert(lbnd, bisector);

                    // second half of Step 11:
                    if ((vertex = Vertex.Intersect(bisector, rbnd)) != null)
                    {
                        vertices.Add(vertex);
                        bisector.vertex = vertex;
                        bisector.ystar  = vertex.y + newSite.Dist(vertex);
                        heap.Insert(bisector);
                    }

                    newSite = _sites.Next();
                }
                else if (heap.Empty() == false)
                {
                    /* intersection is smallest */
                    lbnd       = heap.ExtractMin();
                    llbnd      = lbnd.edgeListLeftNeighbor;
                    rbnd       = lbnd.edgeListRightNeighbor;
                    rrbnd      = rbnd.edgeListRightNeighbor;
                    bottomSite = FortunesAlgorithm_leftRegion(lbnd);
                    topSite    = FortunesAlgorithm_rightRegion(rbnd);
                    // these three sites define a Delaunay triangle
                    // (not actually using these for anything...)
                    //_triangles.push(new Triangle(bottomSite, topSite, rightRegion(lbnd)));

                    v = lbnd.vertex;
                    v.SetIndex();
                    lbnd.edge.SetVertex((Side)lbnd.leftRight, v);
                    rbnd.edge.SetVertex((Side)rbnd.leftRight, v);
                    edgeList.Remove(lbnd);
                    heap.Remove(rbnd);
                    edgeList.Remove(rbnd);
                    leftRight = Side.LEFT;
                    if (bottomSite.y > topSite.y)
                    {
                        tempSite   = bottomSite;
                        bottomSite = topSite;
                        topSite    = tempSite;
                        leftRight  = Side.RIGHT;
                    }
                    edge = Edge.CreateBisectingEdge(bottomSite, topSite);
                    _edges.Add(edge);
                    bisector = Halfedge.Create(edge, leftRight);
                    halfEdges.Add(bisector);
                    edgeList.Insert(llbnd, bisector);
                    edge.SetVertex(SideHelper.Other(leftRight), v);
                    if ((vertex = Vertex.Intersect(llbnd, bisector)) != null)
                    {
                        vertices.Add(vertex);
                        heap.Remove(llbnd);
                        llbnd.vertex = vertex;
                        llbnd.ystar  = vertex.y + bottomSite.Dist(vertex);
                        heap.Insert(llbnd);
                    }
                    if ((vertex = Vertex.Intersect(bisector, rrbnd)) != null)
                    {
                        vertices.Add(vertex);
                        bisector.vertex = vertex;
                        bisector.ystar  = vertex.y + bottomSite.Dist(vertex);
                        heap.Insert(bisector);
                    }
                }
                else
                {
                    break;
                }
            }

            // heap should be empty now
            heap.Dispose();
            edgeList.Dispose();

            for (int hIndex = 0; hIndex < halfEdges.Count; hIndex++)
            {
                Halfedge halfEdge = halfEdges[hIndex];
                halfEdge.ReallyDispose();
            }
            halfEdges.Clear();

            // we need the vertices to clip the edges
            for (int eIndex = 0; eIndex < _edges.Count; eIndex++)
            {
                edge = _edges[eIndex];
                edge.ClipVertices(_plotBounds);
            }
            // but we don't actually ever use them again!
            for (int vIndex = 0; vIndex < vertices.Count; vIndex++)
            {
                vertex = vertices[vIndex];
                vertex.Dispose();
            }
            vertices.Clear();
        }
コード例 #4
0
ファイル: Site.cs プロジェクト: cristiangoga/tw-defense
        private void Connect(List <Vector2> points, int j, Rect bounds, bool closingUp = false)
        {
            Vector2 rightPoint     = points[points.Count - 1];
            Edge    newEdge        = _edges[j] as Edge;
            Side    newOrientation = _edgeOrientations[j];

            // the point that  must be connected to rightPoint:
            if (newEdge.clippedEnds[newOrientation] == null)
            {
                Debug.LogError("XXX: Null detected when there should be a Vector2!");
            }
            Vector2 newPoint = (Vector2)newEdge.clippedEnds[newOrientation];

            if (!CloseEnough(rightPoint, newPoint))
            {
                // The points do not coincide, so they must have been clipped at the bounds;
                // see if they are on the same border of the bounds:
                if (rightPoint.x != newPoint.x &&
                    rightPoint.y != newPoint.y)
                {
                    // They are on different borders of the bounds;
                    // insert one or two corners of bounds as needed to hook them up:
                    // (NOTE this will not be correct if the region should take up more than
                    // half of the bounds rect, for then we will have gone the wrong way
                    // around the bounds and included the smaller part rather than the larger)
                    int   rightCheck = BoundsCheck.Check(rightPoint, bounds);
                    int   newCheck = BoundsCheck.Check(newPoint, bounds);
                    float px, py;
                    if ((rightCheck & BoundsCheck.RIGHT) != 0)
                    {
                        px = bounds.xMax;
                        if ((newCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            py = bounds.yMax;
                            points.Add(new Vector2(px, py));
                        }
                        else if ((newCheck & BoundsCheck.TOP) != 0)
                        {
                            py = bounds.yMin;
                            points.Add(new Vector2(px, py));
                        }
                        else if ((newCheck & BoundsCheck.LEFT) != 0)
                        {
                            if (rightPoint.y - bounds.y + newPoint.y - bounds.y < bounds.height)
                            {
                                py = bounds.yMin;
                            }
                            else
                            {
                                py = bounds.yMax;
                            }
                            points.Add(new Vector2(px, py));
                            points.Add(new Vector2(bounds.xMin, py));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.LEFT) != 0)
                    {
                        px = bounds.xMin;
                        if ((newCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            py = bounds.yMax;
                            points.Add(new Vector2(px, py));
                        }
                        else if ((newCheck & BoundsCheck.TOP) != 0)
                        {
                            py = bounds.yMin;
                            points.Add(new Vector2(px, py));
                        }
                        else if ((newCheck & BoundsCheck.RIGHT) != 0)
                        {
                            if (rightPoint.y - bounds.y + newPoint.y - bounds.y < bounds.height)
                            {
                                py = bounds.yMin;
                            }
                            else
                            {
                                py = bounds.yMax;
                            }
                            points.Add(new Vector2(px, py));
                            points.Add(new Vector2(bounds.xMax, py));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.TOP) != 0)
                    {
                        py = bounds.yMin;
                        if ((newCheck & BoundsCheck.RIGHT) != 0)
                        {
                            px = bounds.xMax;
                            points.Add(new Vector2(px, py));
                        }
                        else if ((newCheck & BoundsCheck.LEFT) != 0)
                        {
                            px = bounds.xMin;
                            points.Add(new Vector2(px, py));
                        }
                        else if ((newCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            if (rightPoint.x - bounds.x + newPoint.x - bounds.x < bounds.width)
                            {
                                px = bounds.xMin;
                            }
                            else
                            {
                                px = bounds.xMax;
                            }
                            points.Add(new Vector2(px, py));
                            points.Add(new Vector2(px, bounds.yMax));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.BOTTOM) != 0)
                    {
                        py = bounds.yMax;
                        if ((newCheck & BoundsCheck.RIGHT) != 0)
                        {
                            px = bounds.xMax;
                            points.Add(new Vector2(px, py));
                        }
                        else if ((newCheck & BoundsCheck.LEFT) != 0)
                        {
                            px = bounds.xMin;
                            points.Add(new Vector2(px, py));
                        }
                        else if ((newCheck & BoundsCheck.TOP) != 0)
                        {
                            if (rightPoint.x - bounds.x + newPoint.x - bounds.x < bounds.width)
                            {
                                px = bounds.xMin;
                            }
                            else
                            {
                                px = bounds.xMax;
                            }
                            points.Add(new Vector2(px, py));
                            points.Add(new Vector2(px, bounds.yMin));
                        }
                    }
                }
                if (closingUp)
                {
                    // newEdge's ends have already been added
                    return;
                }
                points.Add(newPoint);
            }
            if (newEdge.clippedEnds[SideHelper.Other(newOrientation)] == null)
            {
                Debug.LogError("XXX: Null detected when there should be a Vector2!");
            }
            Vector2 newRightPoint = (Vector2)newEdge.clippedEnds[SideHelper.Other(newOrientation)];

            if (!CloseEnough(points[0], newRightPoint))
            {
                points.Add(newRightPoint);
            }
        }
コード例 #5
0
ファイル: Site.cs プロジェクト: cristiangoga/tw-defense
 public void AddEdge(Edge edge)
 {
     _edges.Add(edge);
 }
コード例 #6
0
 static bool MyTest(Edge edge, int index, List<Edge> vector)
 {
     return ((edge.LeftSite != null && edge.LeftSite.Coord() == _coord)
     || (edge.RightSite != null && edge.RightSite.Coord() == _coord));
 }
コード例 #7
0
 public static int CompareSitesDistances(Edge edge0, Edge edge1)
 {
     return(-CompareSitesDistances_MAX(edge0, edge1));
 }