Example usage for java.awt.geom Point2D distanceSq

List of usage examples for java.awt.geom Point2D distanceSq

Introduction

In this page you can find the example usage for java.awt.geom Point2D distanceSq.

Prototype

public static double distanceSq(double x1, double y1, double x2, double y2) 

Source Link

Document

Returns the square of the distance between two points.

Usage

From source file:org.jax.maanova.plot.PlotUtil.java

/**
 * Get the index of the data point nearest to graphX, graphY.
 * @param xyData/*ww w  . j  a va 2  s .  c om*/
 *          the data points to search through
 * @param graphX
 *          the reference X point
 * @param graphY
 *          the reference Y point
 * @return
 *          the index of the nearest point or -1 if there is no such point
 */
public static int getNearestDataIndex(double[][] xyData, double graphX, double graphY) {
    int nearestIndex = -1;

    final double[] xData = xyData[0];
    final double[] yData = xyData[1];

    double nearestDistSq = Double.POSITIVE_INFINITY;
    for (int i = 0; i < xData.length; i++) {
        double currDistSq = Point2D.distanceSq(graphX, graphY, xData[i], yData[i]);
        if (currDistSq < nearestDistSq) {
            nearestDistSq = currDistSq;
            nearestIndex = i;
        }
    }

    return nearestIndex;
}

From source file:at.tuwien.ifs.somtoolbox.apps.viewer.MapPNode.java

/**
 * Computes the locations of each input vector within a specific unit
 *//*from w  ww .  j a v  a2  s  .c  o  m*/
private Point[][] initInputLocations(Unit unit) {

    int size = unit.getNumberOfMappedInputs();
    // Store XY displacements in a temporary array
    double[][] locdxy = new double[size][2];
    double[][] locdxyShifted = new double[size][2];

    // Process all mapped inputs in the unit
    for (int index = 0; index < size; index++) {
        // Find vector position for this mapped input
        String label = unit.getMappedInputName(index);

        int vindex;
        try {
            vindex = inputObjects.getDataWinnerMapping().getVectPos(label);
        } catch (SOMToolboxException e1) {
            Logger.getLogger("at.tuwien.ifs.somtoolbox")
                    .warning(e1.getMessage() + " Could not draw exact point on unit!");
            // e1.printStackTrace();
            continue;
        }

        // Get winners information
        int[] xpos = inputObjects.getDataWinnerMapping().getXPos(vindex);
        int[] ypos = inputObjects.getDataWinnerMapping().getYPos(vindex);
        double[] dist = inputObjects.getDataWinnerMapping().getDists(vindex);

        // Computes displacement of the input vector against the next best winning unit.
        for (int uindex = 1; uindex <= 3; uindex++) {
            double vx = xpos[uindex] - xpos[0];
            double vy = ypos[uindex] - ypos[0];

            // Find x-axis and y-axis pull force of the winner. The pull force of 2nd unit is higher than the 3rd
            // and so on
            double force = dist[0] / (dist[uindex] * uindex);

            // Now calculate displacement. The key is: farther the unit, lesser must be the displacement.
            // Additionally the displacement is zero if units are not pulling in different directions.
            // Consider unit U1 being positioned at [2, 4] and unit U2's position being [3, 4].
            // Now clearly both units lie on the same position on y-axis and thus there is no need to compute
            // displacement for y-axis
            locdxy[index][0] += vx == 0 ? 0 : UNIT_WIDTH / 2 * force / vx;
            locdxy[index][1] += vy == 0 ? 0 : UNIT_WIDTH / 2 * force / vy;
        }

        locdxyShifted[index][0] = locdxy[index][0];
        locdxyShifted[index][1] = locdxy[index][1];

        // Compute force of replusion if the current input is ovlapping with any of the previous inputs
        // store locations into separate field to allow easy switching
        for (int rindex = 0; rindex < index; rindex++) {
            double distance = Point2D.distanceSq(locdxy[rindex][0], locdxy[rindex][1], locdxy[index][0],
                    locdxy[index][1]);
            if (distance < InputPNode.MIN_DISTANCE_SQ) {
                // Its like other input pushing the this input towards boundary if its trying to overlap
                locdxyShifted[index][0] *= 1.03;
                locdxyShifted[index][1] *= 1.03;
                // And on the other sie this input is forcing that input to leave a free space for him by moving
                // towards center.
                // double factor = distance/InputPNode.MIN_DISTANCE_SQ;
                double factor = 0.8;
                locdxyShifted[rindex][0] *= factor;
                locdxyShifted[rindex][1] *= factor;
            }
        }
    }
    // Now calculate the actual XY position for display
    Point[][] locations = new Point[2][size];
    for (int i = 0; i < size; i++) {
        locations[0][i] = new Point((int) (UNIT_WIDTH / 2 + locdxy[i][0]),
                (int) (UNIT_WIDTH / 2 + locdxy[i][1]));
        locations[1][i] = new Point((int) (UNIT_WIDTH / 2 + locdxyShifted[i][0]),
                (int) (UNIT_WIDTH / 2 + locdxyShifted[i][1]));
    }
    return locations;
}