3

Solved, used this code:

if ( !isClockwise(TempVectArray) ) { Collections.reverse(TempVectArray); }

...

private boolean isClockwise(ArrayList<Vec2> arl){
    Iterator<Vec2> it = arl.iterator();
    Vec2 pt1 = (Vec2)it.next();
    Vec2 firstPt = pt1;
    Vec2 lastPt = null;
    double area = 0.0;
    while(it.hasNext()){
        Vec2 pt2 = (Vec2) it.next();
        area += (((pt2.x - pt1.x) * (pt2.y + pt1.y)) / 2);
        pt1 = pt2;
        lastPt = pt1;
    }
    area += (((firstPt.x - lastPt.x) * (firstPt.y + lastPt.y)) / 2);
    return area < 0;
}

Suppose I get a vertex array from the user tapping on the screen, but need it to be clockwise.

Maybe you know of some standard methods to check if it is clockwise and if it's not, then make it clockwise?

enter image description here

Thanks!

4

2 回答 2

2

一种方法是首先计算平均点,然后按角度对其周围的所有内容进行排序。应该是这样的:

public static void sortPointsClockwise(ArrayList<PointF> points) {
    float averageX = 0;
    float averageY = 0;

    for (PointF point : points) {
        averageX += point.x;
        averageY += point.y;
    }

    final float finalAverageX = averageX / points.size();
    final float finalAverageY = averageY / points.size();

    Comparator<PointF> comparator = new Comparator<PointF>() {
        public int compare(PointF lhs, PointF rhs) {
            double lhsAngle = Math.atan2(lhs.y - finalAverageY, lhs.x - finalAverageX);
            double rhsAngle = Math.atan2(rhs.y - finalAverageY, rhs.x - finalAverageX);

            // Depending on the coordinate system, you might need to reverse these two conditions
            if (lhsAngle < rhsAngle) return -1;
            if (lhsAngle > rhsAngle) return 1;

            return 0;
        }
    };

    Collections.sort(points, comparator);
}

public static void sortPointsCounterClockwise(ArrayList<PointF> points) {
    sortPointsClockwise(points);
    Collections.reverse(points);
}
于 2012-05-05T10:08:02.157 回答
0

您有节点的序列号和位置。获取在移动中保持 x 和 y 变化的移动。剩下要做的就是定义一个控制结构,例如:

if(movement_before is "up")
    movement should-be "up" or "up-right"
if(movement_before is "up-left")
    movement should-be "up" or "up-left" or "up-right"
etc..
于 2012-05-05T11:42:10.903 回答