示例#1
0
    bool CheckForNewEdgeEvents(BeachLineArc arc)
    {
        if (arc != null)
        {
            // Check left and right edge of this arc for intersections.
            BeachLineEdge leftEdge  = arc.LeftEdge;
            BeachLineEdge rightEdge = arc.RightEdge;
            if (leftEdge != null && rightEdge != null)
            {
                // Check for edge intersection
                Vector2 intersection;
                if (leftEdge.CheckIntersection(rightEdge, out intersection))
                {
                    // These edges intersect. We need to add a new edge event.

                    // This event may invalidate a previous one. Search for an existing event for this arc.
                    foreach (VoronoiEvent e in events)
                    {
                        if (e is EdgeEvent)
                        {
                            EdgeEvent edgeEvent = e as EdgeEvent;
                            if (edgeEvent.LeftEdge == leftEdge && edgeEvent.RightEdge == rightEdge)
                            {
                                e.Invalidate();
                            }
                        }
                    }

                    events.Add(new EdgeEvent(intersection, arc, leftEdge, rightEdge));
                    events.Sort();
                    return(true);
                }
            }
        }
        return(false);
    }
示例#2
0
    public List <BeachLineEdge> Squeeze(out BeachLineEdge newEdge)
    {
        // These are the output edges
        BeachLineEdge leftEdge  = (BeachLineEdge)Prev;
        BeachLineEdge rightEdge = (BeachLineEdge)Next;
        BeachLineArc  leftArc   = (BeachLineArc)leftEdge.Prev;
        BeachLineArc  rightArc  = (BeachLineArc)rightEdge.Next;

        // Create new edge
        // To create it, we need the intersection point of the two output edges plus the
        // focuses of the two arcs.
        Vector2 intersection;

        leftEdge.CheckIntersection(rightEdge, out intersection);
        Vector2 focus1           = leftArc.Focus;
        Vector2 focus2           = rightArc.Focus;
        Vector2 perpendicular    = focus2 - focus1;
        Vector2 newEdgeDirection = new Vector2(perpendicular.y, -perpendicular.x);

        if (Vector2.Dot(leftEdge.Direction.normalized + rightEdge.Direction.normalized, -newEdgeDirection) > 0)
        //if (newEdgeDirection.y < 0)
        {
            newEdgeDirection = -newEdgeDirection;
        }

        newEdge = new BeachLineEdge(intersection, newEdgeDirection.normalized);


        BeachLineEdge edgeToReplace = leftEdge == Parent ? rightEdge : leftEdge;

        //newEdge.SetLeftArc(LeftArc);
        //newEdge.SetRightArc(RightArc);
        // Replace other edge with new edge
        edgeToReplace.ReplaceWithSingleNode(newEdge);

        // Replace parent with sibling

        BeachLineElement sibling = Sibling;

        sibling.SetParent(null);
        bool iAmLeftChild = this == Parent.LeftChild;

        if (iAmLeftChild)
        {
            Parent.SetRightChild(null);
        }
        else
        {
            Parent.SetLeftChild(null);
        }

        BeachLineElement parentsParent     = Parent.Parent;
        bool             parentIsLeftChild = Parent == parentsParent.LeftChild;

        if (parentIsLeftChild)
        {
            parentsParent.SetLeftChild(sibling);
        }
        else
        {
            parentsParent.SetRightChild(sibling);
        }
        sibling.SetParent(parentsParent);

        //Parent.ReplaceWith(Sibling);

        //Parent.SetLeftChild(null);
        //Parent.SetRightChild(null);

        // Set next/prev for altered nodes
        leftArc.SetNext(newEdge);
        newEdge.SetPrev(leftArc);
        newEdge.SetNext(rightArc);
        rightArc.SetPrev(newEdge);

        // Remove this node from tree
        SetParent(null);
        SetLeftChild(null);
        SetRightChild(null);
        SetNext(null);
        SetPrev(null);
        leftEdge.SetParent(null);
        leftEdge.SetLeftChild(null);
        leftEdge.SetRightChild(null);
        leftEdge.SetNext(null);
        leftEdge.SetPrev(null);
        rightEdge.SetParent(null);
        rightEdge.SetLeftChild(null);
        rightEdge.SetRightChild(null);
        rightEdge.SetNext(null);
        rightEdge.SetPrev(null);

        leftEdge.SetEndpoint(intersection);
        rightEdge.SetEndpoint(intersection);

        List <BeachLineEdge> outputList = new List <BeachLineEdge>
        {
            leftEdge,
            rightEdge
        };

        return(outputList);
    }