public void CheckCircle(BeachArc mid)
    {
        BeachEdge leftParent  = BeachNode.GetLeftParent(mid);
        BeachEdge rightParent = BeachNode.GetRightParent(mid);

        BeachArc left  = BeachNode.GetLeftChild(leftParent);
        BeachArc right = BeachNode.GetRightChild(rightParent);

        if (left == null || right == null || left.site == right.site)
        {
            return;
        }

        Vector2 s = GetEdgeIntersection(leftParent.edge, rightParent.edge);

        if (float.IsNaN(s.x) || float.IsNaN(s.y))
        {
            return;
        }

        float dx = left.site.pos.x - s.x;
        float dy = left.site.pos.y - s.y;

        float d = Mathf.Sqrt((dx * dx) + (dy * dy));

        if (s.y - d >= sweep)
        {
            return;
        }

        VertexEvent ev = new VertexEvent(left, mid, right);

        mid.ev = ev;
        events.Add(ev);
    }
    public float GetXOfEdge(BeachEdge n, float y)
    {
        BeachArc left  = BeachNode.GetLeftChild(n);
        BeachArc right = BeachNode.GetRightChild(n);

        Vector2 p = left.site.pos;
        Vector2 r = right.site.pos;

        float dp = 2f * (p.y - y);
        float a1 = 1f / dp;
        float b1 = -2f * p.x / dp;
        float c1 = y + dp / 4 + p.x * p.x / dp;

        dp = 2f * (r.y - y);
        float a2 = 1f / dp;
        float b2 = -2f * r.x / dp;
        float c2 = sweep + dp / 4 + r.x * r.x / dp;

        float a = a1 - a2;
        float b = b1 - b2;
        float c = c1 - c2;

        float disc = b * b - 4 * a * c;
        float x1   = (-b + Mathf.Sqrt(disc)) / (2 * a);
        float x2   = (-b - Mathf.Sqrt(disc)) / (2 * a);

        float xl = Mathf.Min(x1, x2);
        float xr = Mathf.Max(x1, x2);


        float ry;

        if (p.y < r.y)
        {
            ry = Mathf.Max(x1, x2);
        }
        else
        {
            ry = Mathf.Min(x1, x2);
        }
        return(ry);

        /*
         * if(p.x < r.x) //going L to R
         * {
         *  if (r.y < p.y)
         *      return xl;
         *  else
         *      return xr;
         * }
         * else
         * {
         *  if (p.y < r.y)
         *      return xr;
         *  else
         *      return xl;
         * }*/
    }
    public void Remove(VertexEvent e)
    {
        BeachArc mid = e.arcs[1];

        BeachEdge leftEdge  = BeachNode.GetLeftParent(mid);
        BeachEdge rightEdge = BeachNode.GetRightParent(mid);

        BeachArc left  = BeachNode.GetLeftChild(leftEdge);
        BeachArc right = BeachNode.GetRightChild(rightEdge);

        if (left.ev != null)
        {
            events.Remove(left.ev);
            left.ev = null;
        }
        if (right.ev != null)
        {
            events.Remove(right.ev);
            right.ev = null;
        }

        Vertex end = new Vertex(e.center);

        diagram.vertices.Add(end);
        leftEdge.edge.end  = end;
        rightEdge.edge.end = end;

        diagram.edges.Add(leftEdge.edge);
        diagram.edges.Add(rightEdge.edge);

        BeachEdge higher = null;
        BeachNode par    = mid;

        while (par != root)
        {
            par = par.parent;
            if (par == leftEdge)
            {
                higher = leftEdge;
            }
            if (par == rightEdge)
            {
                higher = rightEdge;
            }
        }
        higher.edge = new Edge(end, left.site, right.site);
        diagram.edges.Add(higher.edge);

        BeachEdge gparent = (BeachEdge)mid.parent.parent;

        if (mid.parent.left == mid)
        {
            if (gparent.left == mid.parent)
            {
                gparent.left = mid.parent.right;
            }
            if (gparent.right == mid.parent)
            {
                gparent.right = mid.parent.right;
            }
        }
        else
        {
            if (gparent.left == mid.parent)
            {
                gparent.left = mid.parent.left;
            }
            if (gparent.right == mid.parent)
            {
                gparent.right = mid.parent.left;
            }
        }

        CheckCircle(left);
        CheckCircle(right);
    }