Java Code Examples for org.apache.commons.math3.geometry.euclidean.twod.Vector2D#subtract()

The following examples show how to use org.apache.commons.math3.geometry.euclidean.twod.Vector2D#subtract() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeometryDataSetGenerator.java    From geowave with Apache License 2.0 6 votes vote down vote up
private static final List<Point> generatePoints(
    final GeometryFactory factory,
    final Vector2D coordinateOne,
    final Vector2D coordinateTwo,
    final double distanceFactor,
    final int points) {
  final List<Point> results = new ArrayList<>();
  final Random rand = new Random();
  final Vector2D originVec = coordinateTwo.subtract(coordinateOne);
  for (int i = 0; i < points; i++) {
    // HP Fortify "Insecure Randomness" false positive
    // This random number is not used for any purpose
    // related to security or cryptography
    final double factor = rand.nextDouble();
    final Vector2D projectionPoint = originVec.scalarMultiply(factor);
    final double direction = rand.nextGaussian() * distanceFactor;
    final Vector2D orthogonal = new Vector2D(originVec.getY(), -originVec.getX());

    results.add(
        factory.createPoint(
            toCoordinate(
                orthogonal.scalarMultiply(direction).add(projectionPoint).add(coordinateOne))));
  }
  return results;
}
 
Example 2
Source File: ConvexHull2D.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks whether the given hull vertices form a convex hull.
 * @param hullVertices the hull vertices
 * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
 */
private boolean isConvex(final Vector2D[] hullVertices) {
    if (hullVertices.length < 3) {
        return true;
    }

    int sign = 0;
    for (int i = 0; i < hullVertices.length; i++) {
        final Vector2D p1 = hullVertices[i == 0 ? hullVertices.length - 1 : i - 1];
        final Vector2D p2 = hullVertices[i];
        final Vector2D p3 = hullVertices[i == hullVertices.length - 1 ? 0 : i + 1];

        final Vector2D d1 = p2.subtract(p1);
        final Vector2D d2 = p3.subtract(p2);

        final double crossProduct = MathArrays.linearCombination(d1.getX(), d2.getY(), -d1.getY(), d2.getX());
        final int cmp = Precision.compareTo(crossProduct, 0.0, tolerance);
        // in case of collinear points the cross product will be zero
        if (cmp != 0.0) {
            if (sign != 0.0 && cmp != sign) {
                return false;
            }
            sign = cmp;
        }
    }

    return true;
}
 
Example 3
Source File: ConvexHullGenerator2DAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected final boolean isConvex(final ConvexHull2D hull, final boolean includesCollinearPoints,
                                 final double tolerance) {

    final Vector2D[] points = hull.getVertices();
    int sign = 0;

    for (int i = 0; i < points.length; i++) {
        Vector2D p1 = points[i == 0 ? points.length - 1 : i - 1];
        Vector2D p2 = points[i];
        Vector2D p3 = points[i == points.length - 1 ? 0 : i + 1];

        Vector2D d1 = p2.subtract(p1);
        Vector2D d2 = p3.subtract(p2);

        Assert.assertTrue(d1.getNorm() > 1e-10);
        Assert.assertTrue(d2.getNorm() > 1e-10);

        final double cross = MathArrays.linearCombination(d1.getX(), d2.getY(), -d1.getY(), d2.getX());
        final int cmp = Precision.compareTo(cross, 0.0, tolerance);

        if (sign != 0 && cmp != sign) {
            if (includesCollinearPoints && cmp == 0) {
                // in case of collinear points the cross product will be zero
            } else {
                return false;
            }
        }
        
        sign = cmp;
    }
    
    return true;
}
 
Example 4
Source File: ConvexHull2D.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks whether the given hull vertices form a convex hull.
 * @param hullVertices the hull vertices
 * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
 */
private boolean isConvex(final Vector2D[] hullVertices) {
    if (hullVertices.length < 3) {
        return true;
    }

    int sign = 0;
    for (int i = 0; i < hullVertices.length; i++) {
        final Vector2D p1 = hullVertices[i == 0 ? hullVertices.length - 1 : i - 1];
        final Vector2D p2 = hullVertices[i];
        final Vector2D p3 = hullVertices[i == hullVertices.length - 1 ? 0 : i + 1];

        final Vector2D d1 = p2.subtract(p1);
        final Vector2D d2 = p3.subtract(p2);

        final double crossProduct = MathArrays.linearCombination(d1.getX(), d2.getY(), -d1.getY(), d2.getX());
        final int cmp = Precision.compareTo(crossProduct, 0.0, tolerance);
        // in case of collinear points the cross product will be zero
        if (cmp != 0.0) {
            if (sign != 0.0 && cmp != sign) {
                return false;
            }
            sign = cmp;
        }
    }

    return true;
}
 
Example 5
Source File: ConvexHullGenerator2DAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected final boolean isConvex(final ConvexHull2D hull, final boolean includesCollinearPoints,
                                 final double tolerance) {

    final Vector2D[] points = hull.getVertices();
    int sign = 0;

    for (int i = 0; i < points.length; i++) {
        Vector2D p1 = points[i == 0 ? points.length - 1 : i - 1];
        Vector2D p2 = points[i];
        Vector2D p3 = points[i == points.length - 1 ? 0 : i + 1];

        Vector2D d1 = p2.subtract(p1);
        Vector2D d2 = p3.subtract(p2);

        Assert.assertTrue(d1.getNorm() > 1e-10);
        Assert.assertTrue(d2.getNorm() > 1e-10);

        final double cross = MathArrays.linearCombination(d1.getX(), d2.getY(), -d1.getY(), d2.getX());
        final int cmp = Precision.compareTo(cross, 0.0, tolerance);

        if (sign != 0 && cmp != sign) {
            if (includesCollinearPoints && cmp == 0) {
                // in case of collinear points the cross product will be zero
            } else {
                return false;
            }
        }
        
        sign = cmp;
    }
    
    return true;
}