Example usage for org.apache.commons.math3.geometry.euclidean.twod Vector2D distance

List of usage examples for org.apache.commons.math3.geometry.euclidean.twod Vector2D distance

Introduction

In this page you can find the example usage for org.apache.commons.math3.geometry.euclidean.twod Vector2D distance.

Prototype

public double distance(Vector<Euclidean2D> p) 

Source Link

Usage

From source file:haxball.networking.ServerMainLoop.java

@Override
public void run() {

    resetField();//from   w  w  w .  j av a  2s  .  c  o  m
    byte score0 = 0, score1 = 0;

    while (!stopped) {
        // Move players
        for (Player player : players) {

            byte input = player.getLastInput();

            float vx = 0;
            float vy = 0;

            if ((input & 0b00_00_00_01) != 0) {
                vy += speed;
            }
            if ((input & 0b00_00_00_10) != 0) {
                vx -= speed;
            }
            if ((input & 0b00_00_01_00) != 0) {
                vy -= speed;
            }
            if ((input & 0b00_00_10_00) != 0) {
                vx += speed;
            }
            if ((input & 0b00_01_00_00) != 0) {
                player.setShooting(true);
            } else {
                player.setShooting(false);
            }

            Vector2D v = new Vector2D(vx, vy);
            if (v.distance(Vector2D.ZERO) > 0) {
                v = v.normalize().scalarMultiply(speed);
            }

            player.velocity = player.velocity.add(v.subtract(player.velocity).scalarMultiply(acceleration));
            player.position = player.position.add(player.velocity);
        }

        // Move ball
        ball.velocity = ball.velocity.scalarMultiply(1 - friction);
        ball.position = ball.position.add(ball.velocity);

        // Check for collisions
        for (Player p : players) {
            // collisions between players
            for (Player p0 : players) {
                if (!p.equals(p0)) {
                    p.uncollide(p0);
                }
            }
            p.uncollide(ball);
            p.setInsideMap(true);

            if (p.isShooting()
                    && p.position.distance(ball.position) < (p.getRadius() + ball.getRadius()) * 1.4f) {
                Vector2D norm = ball.position.subtract(p.position).normalize();
                ball.velocity = ball.velocity.add(norm.scalarMultiply(shootingPower));
            }

        }
        int result;
        if ((result = ball.setInsideMap(false)) >= 0) {
            if (result == 0) {
                score0++;
            } else {
                score1++;
            }
            resetField();
        }

        // send position to every connection
        for (ConnectionHandler handler : connectionHandlers) {
            handler.writeState(ball, score0, score1, players);
        }
        // sleep
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    } // while !stopped
}

From source file:edu.stanford.cfuller.imageanalysistools.util.VoronoiDiagram.java

protected Integer findClosestRegion(Vector2D lookup) {

    Vector2D best = null;/* w  ww.  j  a  v  a2s.  co  m*/

    double bestDist = Double.MAX_VALUE;

    for (Vector2D seedPoint : this.regionLookup.keySet()) {

        if (seedPoint.distance(lookup) < bestDist) {
            bestDist = seedPoint.distance(lookup);
            best = seedPoint;
        }

    }

    return this.regionLookup.get(best);

}

From source file:org.evors.core.TestUtils.java

public static Matcher<Vector2D> vIsIn(final Collection<Vector2D> vc) {
    return new BaseMatcher<Vector2D>() {

        @Override/*from   w ww.  j a  v  a 2s.  com*/
        public boolean matches(Object item) {
            Vector2D v2 = (Vector2D) item;
            boolean matchFound = false;
            for (Vector2D v : vc) {
                if (v2.distance(v) < EPS)
                    matchFound = true;
            }
            return matchFound;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Vector not found in collection :" + vc.toString());
        }

    };
}

From source file:org.evors.core.TestUtils.java

public static void assertTwoVector2DsEqual(Vector2D v1, Vector2D v2, boolean assertion) {
    boolean result;
    if (assertion)
        result = v1.distance(v2) < 0.0001;
    else/*from  w  ww . j a  v a 2s  .c  om*/
        result = v1.distance(v2) > 0.0001;
    assertTrue(String.format("Expected: %s, actual: %s", v1.toString(), v2.toString()), result);
}

From source file:org.micromanager.plugins.magellan.surfacesandregions.SurfaceInterpolatorSimple.java

@Override
public float getExtrapolatedValue(double x, double y) {
    //duplicate points for thread safety
    final LinkedList<Point3d> points = new LinkedList<Point3d>(points_);

    //find 3 closest points and calculate value
    //find closest convex hull vertex
    final LinkedList<Integer> closestIndices = new LinkedList<Integer>();
    final LinkedList<Double> closestDistances = new LinkedList<Double>();
    for (int i = 0; i < points.size(); i++) {
        //get current distance
        Vector2D vertex = new Vector2D(points.get(i).x, points.get(i).y);
        double distance = vertex.distance(new Vector2D(x, y));
        if (closestDistances.size() < 3) {
            closestIndices.add(i);/*from  www. j  a  va 2s .c o m*/
            closestDistances.add(distance);
        } else if (distance < closestDistances.get(2)) {
            closestIndices.removeLast();
            closestDistances.removeLast();
            closestIndices.add(i);
            closestDistances.add(distance);
        }
        //sort
        Collections.sort(closestIndices, new Comparator<Integer>() {
            public int compare(Integer left, Integer right) {
                return (new Double(closestDistances.get(closestIndices.indexOf(left))))
                        .compareTo(closestDistances.get(closestIndices.indexOf(right)));
            }
        });
        Collections.sort(closestDistances);
    }
    Point3d point1 = points.get(closestIndices.get(0));
    Point3d point2 = points.get(closestIndices.get(1));
    Point3d point3 = points.get(closestIndices.get(2));
    Vector3D v1 = new Vector3D(point1.x, point1.y, point1.z);
    Vector3D v2 = new Vector3D(point2.x, point2.y, point2.z);
    Vector3D v3 = new Vector3D(point3.x, point3.y, point3.z);
    Plane plane = new Plane(v1, v2, v3, TOLERANCE);
    //intersetion of vertical line at these x+y values with plane gives point in plane
    Vector3D pointInPlane = plane
            .intersection(new Line(new Vector3D(x, y, 0), new Vector3D(x, y, 1), TOLERANCE));
    float zVal = (float) pointInPlane.getZ();
    return zVal;
}

From source file:uk.ac.ebi.cysbgn.methods.SegmentMethods.java

public static Vector2D pointOutNodeBoundary(Rectangle2D.Double nodeRectangle, Vector2D start, Vector2D end,
        double distance) {
    // C = A - k ( A - B )
    // k = distance / distance_From_A_to_B

    Line arcLine = new Line(start, end);

    // Calculate the point here the arc intersects the node boundary rectangle
    Vector2D boundaryPoint = nodeArcIntersectionPoint(nodeRectangle, arcLine);

    Double k = distance / boundaryPoint.distance(end);

    Double Xc = boundaryPoint.getX() - k * (boundaryPoint.getX() - end.getX());
    Double Xy = boundaryPoint.getY() - k * (boundaryPoint.getY() - end.getY());

    return new Vector2D(Xc, Xy);
}

From source file:uk.ac.ebi.cysbgn.methods.SegmentMethods.java

public static Vector2D calculateLinePointByDistanceToStart(Vector2D A, Vector2D B, double distance) {
    // C = A - k ( A - B )
    // k = distance / distance_From_A_to_B

    Double k = distance / A.distance(B);

    Double Cx = A.getX() - k * (A.getX() - B.getX());
    Double Cy = A.getY() - k * (A.getY() - B.getY());

    Vector2D C = new Vector2D(Cx, Cy);

    return C;/*  w  w  w  . j  av  a2s  . com*/
}

From source file:uk.ac.ebi.cysbgn.methods.SegmentMethods.java

public static Vector2D calculateOutSidePointByDistanceToStart(Vector2D C, Vector2D B, double distance) {
    // A = ( C - k B ) / ( 1 - k )
    // k = distance / distance_From_A_to_B
    // distance_From_A_to_B = distance + distance_From_C_to_B

    Double k = distance / (C.distance(B) + distance);

    Double Ax = (C.getX() - k * B.getX()) / (1 - distance);
    Double Ay = (C.getY() - k * B.getY()) / (1 - distance);

    Vector2D A = new Vector2D(Ax, Ay);

    return A;/*  www . j  ava2s .co  m*/
}