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 double distanceSq(double px, double py) 

Source Link

Document

Returns the square of the distance from this Point2D to a specified point.

Usage

From source file:ch.epfl.leb.sass.models.emitters.internal.AbstractEmitter.java

/**
 * Returns a list of pixels within a certain radius from a point.
 * /*from w  ww  .  j  a va2  s . co  m*/
 * This method locates all the pixels within a circular area surrounding a
 * given two-dimensional point whose center lies at (x, y). The coordinate
 * of a pixel is assumed to lie at the pixel's center, and a pixel is within
 * a given radius of another if the pixel's center lies within this circle.
 * 
 * @param point
 * @param radius radius value [pixels]
 * @return list of Pixels with pre-calculated signatures
 */
public static final ArrayList<Pixel> getPixelsWithinRadius(Point2D point, double radius) {
    ArrayList<Pixel> result = new ArrayList<Pixel>();
    // If radius is less than one, return the pixel containing the point
    if (radius < 1) {
        int x = (int) point.getX();
        int y = (int) point.getY();
        result.add(new Pixel(x, y, 0));
        return result;
    }

    // Upper and lower bounds for the region.
    final int bot_x = (int) floor(point.getX() - radius);
    final int top_x = (int) ceil(point.getX() + radius);
    final int bot_y = (int) floor(point.getY() - radius);
    final int top_y = (int) ceil(point.getY() + radius);

    // Squared radius so we dont have to do the sqrt()
    final double radius2 = radius * radius;

    // Iterate over all pixels in the square defined by the bounds and
    // filter out those which are too far, otherwise generate signature and
    // add to list.
    for (int i = bot_x; i <= top_x; i++) {
        for (int j = bot_y; j <= top_y; j++) {
            if (point.distanceSq((double) i, (double) j) <= radius2) {
                result.add(new Pixel(i, j, 0));
            }
        }
    }
    return result;
}

From source file:org.jax.maanova.madata.gui.ArrayScatterPlotPanel.java

private void mouseMoved(MouseEvent e) {
    if (this.showTooltip) {
        Point2D chartPoint = this.chartPanel.toChartPoint(e.getPoint());

        // find the nearest probe
        XYProbeData xyProbeData = this.getXYData();
        double nearestDistance = Double.POSITIVE_INFINITY;
        int nearestDotIndex = -1;
        double[] xData = xyProbeData.getXData();
        double[] yData = xyProbeData.getYData();
        for (int dotIndex = 0; dotIndex < xData.length; dotIndex++) {
            double currDist = chartPoint.distanceSq(xData[dotIndex], yData[dotIndex]);
            if (currDist < nearestDistance) {
                nearestDistance = currDist;
                nearestDotIndex = dotIndex;
            }//  w w  w .ja  v a  2 s  .c o m
        }

        if (nearestDotIndex == -1) {
            this.clearProbePopup();
        } else {
            Point2D probeJava2DCoord = this.getJava2DCoordinates(xData[nearestDotIndex],
                    yData[nearestDotIndex]);
            double java2DDist = probeJava2DCoord.distance(e.getX(), e.getY());

            // is the probe close enough to be worth showing (in pixel distance)
            if (java2DDist <= PlotUtil.SCATTER_PLOT_DOT_SIZE_PIXELS * 2) {
                this.showProbePopup(xyProbeData.getProbeIndices()[nearestDotIndex], xData[nearestDotIndex],
                        yData[nearestDotIndex], e.getX(), e.getY());
            } else {
                this.clearProbePopup();
            }
        }
    }
}

From source file:org.jax.maanova.fit.gui.ResidualPlotPanel.java

private void mouseMoved(MouseEvent e) {
    if (this.showTooltip) {
        Point2D chartPoint = this.chartPanel.toChartPoint(e.getPoint());

        // find the nearest probe
        XYProbeData[] xyProbeData = this.getXYData();
        double nearestDistance = Double.POSITIVE_INFINITY;
        int nearestArrayIndex = -1;
        int nearestDotIndex = -1;
        for (int arrayIndex = 0; arrayIndex < xyProbeData.length; arrayIndex++) {
            double[] currXData = xyProbeData[arrayIndex].getXData();
            double[] currYData = xyProbeData[arrayIndex].getYData();
            for (int dotIndex = 0; dotIndex < currXData.length; dotIndex++) {
                double currDist = chartPoint.distanceSq(currXData[dotIndex], currYData[dotIndex]);
                if (currDist < nearestDistance) {
                    nearestDistance = currDist;
                    nearestArrayIndex = arrayIndex;
                    nearestDotIndex = dotIndex;
                }/* w w  w  .j  a v  a  2 s  . c  o  m*/
            }
        }

        if (nearestArrayIndex == -1) {
            this.clearProbePopup();
        } else {
            XYProbeData nearestArrayData = xyProbeData[nearestArrayIndex];
            Point2D probeJava2DCoord = this.getJava2DCoordinates(nearestArrayData.getXData()[nearestDotIndex],
                    nearestArrayData.getYData()[nearestDotIndex]);
            double java2DDist = probeJava2DCoord.distance(e.getX(), e.getY());

            // is the probe close enough to be worth showing (in pixel distance)
            if (java2DDist <= PlotUtil.SCATTER_PLOT_DOT_SIZE_PIXELS * 2) {
                this.showProbePopup(nearestArrayIndex, nearestArrayData.getProbeIndices()[nearestDotIndex],
                        nearestArrayData.getXData()[nearestDotIndex],
                        nearestArrayData.getYData()[nearestDotIndex], e.getX(), e.getY());
            } else {
                this.clearProbePopup();
            }
        }
    }
}

From source file:org.jcurl.core.base.CurveTransformedTest.java

public void testAffineTransformRotate() {
    final Rock v0 = new RockDouble(1, 1.5, 0.3);
    final double[] d = { v0.getY(), -v0.getX(), v0.getX(), v0.getY(), 0, 0 };
    final AffineTransform at = new AffineTransform(d);
    final double v = v0.distance(0, 0);
    at.scale(1 / v, 1 / v);/* w  w w  . j a  v  a2s . com*/
    assertEquals(AffineTransform.TYPE_GENERAL_ROTATION, at.getType());
    assertEquals("", 1.0, at.getDeterminant(), 1e-9);
    assertEquals("", 0.8320502943378437, at.getScaleX(), 1e-9);
    assertEquals("", at.getScaleX(), at.getScaleY(), 1e-9);
    assertEquals("", 0.5547001962252291, at.getShearX(), 1e-9);
    assertEquals("", -at.getShearX(), at.getShearY(), 1e-9);
    assertEquals("", 0.0, at.getTranslateX(), 1e-9);
    assertEquals("", 0.0, at.getTranslateY(), 1e-9);
    Point2D p = null;
    p = at.transform(new Point2D.Double(1, 0), null);
    assertEquals("Point2D.Double[0.8320502943378437, -0.5547001962252291]", p.toString());
    assertEquals("", 1.0, p.distanceSq(0, 0), 1e-9);
    p = at.transform(new Point2D.Double(0, 1), null);
    assertEquals("Point2D.Double[0.5547001962252291, 0.8320502943378437]", p.toString());
    assertEquals("", 1.0, p.distanceSq(0, 0), 1e-9);
    p = at.transform(new Point2D.Double(0.75, 1.5), null);
    assertEquals("Point2D.Double[1.4560880150912265, 0.8320502943378438]", p.toString());
    p = at.transform(new Point2D.Double(1.5, 3.0), null);
    assertEquals("Point2D.Double[2.912176030182453, 1.6641005886756877]", p.toString());
}