Java Code Examples for com.badlogic.gdx.math.Vector2.crs()

The following are Jave code examples for showing how to use crs() of the com.badlogic.gdx.math.Vector2 class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: feup-lpoo-armadillo   File: PolygonIntersector.java   Source Code and License Vote up 5 votes
/**
   * Finds the point were two Edges intersect.
   *
   * @param firstEdgeStartPoint  The first edge's starting point.
   * @param firstEdgeEndPoint    The first edge's ending point.
   * @param secondEdgeStartPoint The second edge's starting point.
   * @param secondEdgeEndPoint   The second edge's ending point.
   * @return The point were the two Edges intersect.
   */
  private static Vector2 getEdgesIntersection(Vector2 firstEdgeStartPoint,
                                              Vector2 firstEdgeEndPoint, Vector2 secondEdgeStartPoint,
                                              Vector2 secondEdgeEndPoint) {

      Vector2 firstDirectionPoint = new Vector2(firstEdgeStartPoint.x
              - firstEdgeEndPoint.x, firstEdgeStartPoint.y
              - firstEdgeEndPoint.y);
      Vector2 secondDirectionPoint = new Vector2(secondEdgeStartPoint.x
              - secondEdgeEndPoint.x, secondEdgeStartPoint.y
              - secondEdgeEndPoint.y);

/* Cross product of each edge */
      float crossFirstEdge = firstEdgeStartPoint.crs(firstEdgeEndPoint);
      float crossSecondEdge = secondEdgeStartPoint.crs(secondEdgeEndPoint);

      float inversedCrossDirection = 1 / firstDirectionPoint
              .crs(secondDirectionPoint);

      return new Vector2(
              (crossFirstEdge * secondDirectionPoint.x - crossSecondEdge
                      * firstDirectionPoint.x)
                      * inversedCrossDirection, (crossFirstEdge
              * secondDirectionPoint.y - crossSecondEdge
              * firstDirectionPoint.y)
              * inversedCrossDirection);
  }
 
Example 2
Project: feup-lpoo-armadillo   File: PolygonIntersector.java   Source Code and License Vote up 4 votes
/**
 * Computes the Polygon Properties of a given Polygon.
 *
 * @param polygon The polygon to be analyzed.
 * @return The Polygon Properties computed.
 */
public static PolygonProperties computePolygonProperties(Vector2[] polygon) {
    PolygonProperties polygonProperties = null;

    int count = polygon.length;

    if (count >= 3) {
        Vector2 centroid = new Vector2(0, 0);
        float area = 0;

        Vector2 refPoint = new Vector2(0, 0);
        float threeInverse = 1 / 3f;

        for (int i = 0; i < count; i++) {
            /*
 * Create a new vector to represent the reference point for
 * forming triangles. Then use refPoint, polygonVertex and
 * thirdTriangleVertex as vertices of a triangle.
 */
            refPoint.set(0, 0);
            Vector2 polygonVertex = polygon[i];
            Vector2 thirdTriangleVertex = i + 1 < count ? polygon[i + 1]
                    : polygon[0];

            Vector2 firstDirectionVector = polygonVertex.sub(refPoint);
            Vector2 secondDirectionVector = thirdTriangleVertex
                    .sub(refPoint);

            float triangleArea = firstDirectionVector
                    .crs(secondDirectionVector) / 2;
            area += triangleArea;

/* Area weighted centroid */
            centroid.add(refPoint.add(polygonVertex)
                    .add(thirdTriangleVertex)
                    .scl(triangleArea * threeInverse));
        }

        if (area > EPSILON) {
            centroid.scl(1 / area);
        } else {
            area = 0;
        }

        polygonProperties = new PolygonProperties(centroid, area);
    }

    return polygonProperties;
}