Example usage for com.badlogic.gdx.math Vector2 crs

List of usage examples for com.badlogic.gdx.math Vector2 crs

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector2 crs.

Prototype

public float crs(Vector2 v) 

Source Link

Document

Calculates the 2D cross product between this and the given vector.

Usage

From source file:com.kotcrab.vis.editor.module.physicseditor.util.PolygonUtils.java

License:Apache License

/** Checks whether polygon vertices will make degenerated box2d polygon or not */
public static boolean isDegenerate(Vector2[] vertices) {
    // https://github.com/libgdx/libgdx/blob/master/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp#L120-L226
    // it's not like I understand what is going on here

    int count = vertices.length;
    int n = Math.min(count, B2_MAX_VERTICES);

    // Perform welding and copy vertices into local buffer.
    Vector2[] ps = new Vector2[B2_MAX_VERTICES];
    int tempCount = 0;
    for (int i = 0; i < n; ++i) {
        Vector2 v = vertices[i];//from w  ww  .  j a v  a 2s.c  o m

        boolean unique = true;
        for (int j = 0; j < tempCount; ++j) {
            if (distanceSquared(v, ps[j]) < 0.5f * B2_LINEAR_SLOP) {
                unique = false;
                break;
            }
        }

        if (unique) {
            ps[tempCount++] = v;
        }
    }

    n = tempCount;
    if (n < 3) {
        // Polygon is degenerate.
        return true;
    }

    // Create the convex hull using the Gift wrapping algorithm
    // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm

    // Find the right most point on the hull
    int i0 = 0;
    float x0 = ps[0].x;
    for (int i = 1; i < n; ++i) {
        float x = ps[i].x;
        if (x > x0 || (x == x0 && ps[i].y < ps[i0].y)) {
            i0 = i;
            x0 = x;
        }
    }

    int hull[] = new int[B2_MAX_VERTICES];
    int m = 0;
    int ih = i0;

    while (true) {
        hull[m] = ih;

        int ie = 0;
        for (int j = 1; j < n; ++j) {
            if (ie == ih) {
                ie = j;
                continue;
            }

            Vector2 r = ps[ie].cpy().sub(ps[hull[m]]);
            Vector2 v = ps[j].cpy().sub(ps[hull[m]]);

            float c = r.crs(v);
            if (c < 0.0f) {
                ie = j;
            }

            // Collinearity check
            if (c == 0.0f && v.len2() > r.len2()) {
                ie = j;
            }
        }

        ++m;
        ih = ie;

        if (ie == i0) {
            break;
        }
    }

    if (m < 3) {
        // Polygon is degenerate.
        return true;
    }

    return false;
}

From source file:com.kotcrab.vis.editor.util.polygon.PolygonUtils.java

License:Apache License

/** Checks whether polygon faces will make degenerated box2d polygon or not */
public static boolean isDegenerate(Vector2[] vertices) {
    // https://github.com/libgdx/libgdx/blob/master/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp#L120-L226
    // it's not like I understand what is going on here

    int count = vertices.length;
    int n = Math.min(count, B2_MAX_VERTICES);

    // Perform welding and copy vertices into local buffer.
    Vector2[] ps = new Vector2[B2_MAX_VERTICES];
    int tempCount = 0;
    for (int i = 0; i < n; ++i) {
        Vector2 v = vertices[i];//from w  w w .  j  a v a 2s . c  o  m

        boolean unique = true;
        for (int j = 0; j < tempCount; ++j) {
            if (distanceSquared(v, ps[j]) < ((0.5f * B2_LINEAR_SLOP) * (0.5f * B2_LINEAR_SLOP))) {
                unique = false;
                break;
            }
        }

        if (unique) {
            ps[tempCount++] = v;
        }
    }

    n = tempCount;
    if (n < 3) {
        // Polygon is degenerate.
        return true;
    }

    // Create the convex hull using the Gift wrapping algorithm
    // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm

    // Find the right most point on the hull
    int i0 = 0;
    float x0 = ps[0].x;
    for (int i = 1; i < n; ++i) {
        float x = ps[i].x;
        if (x > x0 || (x == x0 && ps[i].y < ps[i0].y)) {
            i0 = i;
            x0 = x;
        }
    }

    int hull[] = new int[B2_MAX_VERTICES];
    int m = 0;
    int ih = i0;

    while (true) {
        hull[m] = ih;

        int ie = 0;
        for (int j = 1; j < n; ++j) {
            if (ie == ih) {
                ie = j;
                continue;
            }

            Vector2 r = ps[ie].cpy().sub(ps[hull[m]]);
            Vector2 v = ps[j].cpy().sub(ps[hull[m]]);

            float c = r.crs(v);
            if (c < 0.0f) {
                ie = j;
            }

            // Collinearity check
            if (c == 0.0f && v.len2() > r.len2()) {
                ie = j;
            }
        }

        ++m;
        ih = ie;

        if (ie == i0) {
            break;
        }
    }

    if (m < 3) {
        // Polygon is degenerate.
        return true;
    }

    return false;
}