Exemple #1
0
    public int CompareTo(object obj)
    {
        IdsTestPoint b = obj as IdsTestPoint;

        if (this.dist > b.dist)
        {
            return(1);
        }
        return(-1);
    }
    public static PolyPart Partition(List <Vector2> pts, List <int> ids)
    {
        int countPt = pts.Count;
        int countId = ids.Count;

        int idxPa;
        int idxPb;
        int idxPc;

        Vector2 pa, pb, pc;
        Vector2 dirCA;
        Vector2 penCA;
        Vector2 vecB;

        for (int i = 0; i < countId; i++)
        {
            idxPa = ids[i];
            idxPb = ids[(i + 1) % countId] % countPt;
            idxPc = ids[(i + 2) % countId] % countPt;

            pa = pts[idxPa];
            pb = pts[idxPb];
            pc = pts[idxPc];

            if (pa.y > pb.y && pc.y > pb.y)
            {
                dirCA = pc - pa;
                penCA = new Vector2(-dirCA.y, dirCA.x);
                vecB  = pb - (pa + pc) / 2f;

                if (Vector2.Dot(penCA, vecB) < 0)
                {
                    Vector2             tmp;
                    List <IdsTestPoint> tests = new List <IdsTestPoint>(countId);
                    IdsTestPoint        testPt;
                    for (int b = 0; b < countId; b++)
                    {
                        tmp = pts[ids[b]];
                        if (tmp.y < pb.y)
                        {
                            testPt = new IdsTestPoint(tmp, b, (tmp - pb).sqrMagnitude);
                            tests.Add(testPt);
                        }
                    }
                    int      idxDiagonal = TestDiagonal(pts, ids, tests, pb);
                    PolyPart partNew     = new PolyPart(ids, (i + 1) % countId, idxDiagonal);
                    return(partNew);
                }
            }
            if (pa.y < pb.y && pc.y < pb.y)
            {
                dirCA = pc - pa;
                penCA = new Vector2(-dirCA.y, dirCA.x);
                vecB  = pb - (pa + pc) / 2f;

                if (Vector2.Dot(penCA, vecB) < 0)
                {
                    Vector2             tmp;
                    List <IdsTestPoint> tests = new List <IdsTestPoint>(countId);
                    IdsTestPoint        testPt;
                    for (int b = 0; b < countId; b++)
                    {
                        tmp = pts[ids[b]];
                        if (tmp.y > pb.y)
                        {
                            testPt = new IdsTestPoint(tmp, b, (tmp - pb).sqrMagnitude);
                            tests.Add(testPt);
                        }
                    }
                    int idxDiagonal = TestDiagonal(pts, ids, tests, pb);

                    PolyPart partNew = new PolyPart(ids, (i + 1) % countId, idxDiagonal);
                    return(partNew);
                }
            }
        }
        return(null);
    }