class SplittyTriangles extends Triangles {
    SplittyTriangles(int N) {
        super(N, N/2);
    }
    void split(Triangles tris) {
        for(int i = 0; i<tris.nv_; ++i)
            vertices_[i] = tris.vertices_[i];
        nv_ = tris.nv_;
        for(int i = 0; i<tris.nf_; ++i) {
            PointyPoint a = vertices_[tris.faces_[i*3]],
                b = vertices_[tris.faces_[i*3+1]],
                c = vertices_[tris.faces_[i*3+2]];
            int ay = (int)a.pt.y(),
                by = (int)b.pt.y(),
                cy = (int)c.pt.y(),
                unlike=-1;
            if(ay==by) 
                unlike = 2;
            else if(by==cy)
                unlike = 0;
            else if(cy==ay)
                unlike = 1;
            if(unlike>=0) {
                faces_[nf_*3  ] = tris.faces_[i*3+unlike];
                faces_[nf_*3+1] = tris.faces_[i*3+(unlike+1)%3];
                faces_[nf_*3+2] = tris.faces_[i*3+(unlike+2)%3];
                ++nf_;
            }
            else {
                PointyPoint d = extras_[nx_++];
                int dn = nv_++;
                vertices_[dn] = d;
                boolean alb = a.pt.y()<b.pt.y(),
                    blc = b.pt.y()<c.pt.y(),
                    alc = a.pt.y()<c.pt.y();
                int top,mid,bot;
                if(alb)
                    if(alc) {
                        top = 0;
                        if(blc) {
                            mid = 1;
                            bot = 2;
                        }
                        else {
                            mid = 2;
                            bot = 1;
                        }
                    }
                    else {
                        top = 2;
                        mid = 0;
                        bot = 1;
                    }
                else if(blc) {
                        top = 1;
                        if(alc) {
                            mid = 0;
                            bot = 2;
                        }
                        else {
                            mid = 2;
                            bot = 0;
                        }
                    }
                    else {
                        top = 2;
                        mid = 1;
                        bot = 0;
                    }
                a = vertices_[tris.faces_[i*3+top]];
                b = vertices_[tris.faces_[i*3+mid]];
                c = vertices_[tris.faces_[i*3+bot]];
                //System.out.println("wise "+a.pt.y()+" "+b.pt.y()+" "+c.pt.y());
                double t = (b.pt.y()-a.pt.y())/(c.pt.y()-a.pt.y());
                a.pt.lerp(c.pt,t,d.pt);
                //System.out.println("t "+t+" thus "+d.pt);
                a.v.lerp(c.v,t,d.v);
                // we don't care about clockwisedness anymore, right?  
                // trapezoider won't care whether x/y in/decreases
                faces_[nf_*3] = tris.faces_[i*3+top];
                faces_[nf_*3+1] = tris.faces_[i*3+mid];
                faces_[nf_*3+2] = dn;
                //System.out.println("split into: "+vertices_[faces_[nf_*3]].pt+", "+vertices_[faces_[nf_*3+1]].pt+", "+vertices_[faces_[nf_*3+2]].pt);
                ++nf_;
                faces_[nf_*3] = tris.faces_[i*3+bot];
                faces_[nf_*3+1] = tris.faces_[i*3+mid];
                faces_[nf_*3+2] = dn;
                //System.out.println("and: "+vertices_[faces_[nf_*3]].pt+", "+vertices_[faces_[nf_*3+1]].pt+", "+vertices_[faces_[nf_*3+2]].pt);
                ++nf_;
            }
        }
    }
}
