public void Split(BeachLineArc newArc) { float splitX = newArc.Focus.x; float sweepLine = newArc.Focus.y; BeachLineArc newLeftArc = new BeachLineArc(this); BeachLineArc newRightArc = new BeachLineArc(this); Vector2 splitPoint = new Vector2(splitX, y(splitX)); Vector2 edgeDirection = Tangent(splitX); // Make edgeDirection always point in the positive x direction edgeDirection = edgeDirection.x < 0 ? -edgeDirection : edgeDirection; BeachLineEdge newLeftEdge = new BeachLineEdge(splitPoint, -edgeDirection); BeachLineEdge newRightEdge = new BeachLineEdge(splitPoint, edgeDirection); newLeftEdge.SetLeftChild(newLeftArc); newLeftEdge.SetRightChild(newRightEdge); newLeftEdge.SetParent(Parent); newRightEdge.SetLeftChild(newArc); newRightEdge.SetRightChild(newRightArc); newRightEdge.SetParent(newLeftEdge); newLeftArc.SetParent(newLeftEdge); newRightArc.SetParent(newRightEdge); newArc.SetParent(newRightEdge); // Change next and prev links BeachLineEdge prevEdge = (BeachLineEdge)Prev; BeachLineEdge nextEdge = (BeachLineEdge)Next; LinkRemove(); if (prevEdge != null) { prevEdge.LinkInsertAfter(newLeftArc); } else { // newLeftArc is going to be the new start of the list if (nextEdge != null) { newLeftArc.SetNext(nextEdge); nextEdge.SetPrev(newLeftArc); } } newLeftArc.LinkInsertAfter(newLeftEdge) .LinkInsertAfter(newArc) .LinkInsertAfter(newRightEdge) .LinkInsertAfter(newRightArc); //if (nextEdge != null) //{ // newRightArc.SetNext(nextEdge); // nextEdge.SetPrev(newRightArc); //} BeachLineElement parent = Parent; if (parent == null) { // This is root. Replace this with left edge. BeachLineRoot.SetRoot(newLeftEdge); } else { // Set parent to point to new construct if (parent.LeftChild == this) { parent.SetLeftChild(newLeftEdge); } else if (parent.RightChild == this) { parent.SetRightChild(newLeftEdge); } else { Debug.LogError("Error in Split - couldn't find child in parent."); } } // We can now remove this node //SetLeftArc(null); //SetRightArc(null); //SetLeftEdge(null); //SetRightEdge(null); SetLeftChild(null); SetRightChild(null); SetParent(null); }
// Use this for initialization void Start() { leftArc.LinkInsertAfter(leftEdge).LinkInsertAfter(newArc).LinkInsertAfter(rightEdge).LinkInsertAfter(rightArc); }