Example usage for java.awt.geom Point2D distance

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

Introduction

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

Prototype

public double distance(double px, double py) 

Source Link

Document

Returns the distance from this Point2D to a specified point.

Usage

From source file:org.jcurl.model.PathSegmentTest.java

public void testTrafo() throws NoninvertibleTransformException {
    final Point2D x0 = new Point2D.Double(1.5, 2.5);
    // final Point2D x0 = new Point2D.Double(0, 0);
    final Point2D v0 = new Point2D.Double(2, 1);
    // build the trafo
    final AffineTransform rc2wc = new AffineTransform();
    {//from   w w w . j  av a 2 s  .  co m
        rc2wc.rotate(-Math.acos((v0.getX() * 0 + v0.getY() * 1) / v0.distance(0, 0)), x0.getX(), x0.getY());
        rc2wc.translate(x0.getX(), x0.getY());
    }
    // check some points.
    // wc(x0) -> rc(0,0)
    Point2D tmp = rc2wc.inverseTransform(x0, null);
    assertEquals("", 0, tmp.getX(), 1e-9);
    assertEquals("", 0, tmp.getY(), 1e-9);

    // rc(0,1) -> wc(x0)
    tmp = rc2wc.transform(new Point2D.Double(0, 1), null);
    assertEquals("", x0.getX() + 0.8944271909999, tmp.getX(), 1e-6);
    assertEquals("", x0.getY() + 0.4472135954999, tmp.getY(), 1e-6);
}

From source file:org.jcurl.model.PathSegment.java

protected PathSegment(boolean isRockCoordinates, double t0, Point2D x0, Point2D v0, final R1R1Function[] c) {
    super(c);/*from  w w  w  .  j  a v  a 2 s. c  o m*/
    if (c.length != 3)
        throw new IllegalArgumentException("rock path curve must have 3 dimensions, but had " + c.length);
    this.isRockCoordinates = isRockCoordinates;
    this.rc2wc = new AffineTransform();
    final double vabs = v0.distance(0, 0);
    if (vabs != 0.0)
        rc2wc.rotate(-Math.acos((v0.getX() * 0 + v0.getY() * 1) / vabs), x0.getX(), x0.getY());
    rc2wc.translate(x0.getX(), x0.getY());
    this.t0 = t0;
}

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  .co 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;
                }/*from   w w  w . j a  v  a  2 s . c  om*/
            }
        }

        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.jax.maanova.test.gui.VolcanoPlotPanel.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[][] xyData = new double[][] { xyProbeData.getXData(), xyProbeData.getYData() };
        int nearestDotIndex = PlotUtil.getNearestDataIndex(xyData, chartPoint.getX(), chartPoint.getY());

        if (nearestDotIndex == -1) {
            this.clearProbePopup();
        } else {/*from w w  w. j  a v  a 2 s  .  co  m*/
            Point2D probeJava2DCoord = this.getJava2DCoordinates(xyData, 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], e.getX(), e.getY());
            } else {
                this.clearProbePopup();
            }
        }
    }
}

From source file:spinworld.gui.RadarPlot.java

private void drawTickLabel(Graphics2D g2, Rectangle2D radarArea, Point2D middlePoint, double _axisAngle,
        int cat, double tick) {
    double axisAngle = normalize(_axisAngle);
    double _origin = getOrigin(cat).doubleValue();
    double max = getMaxValue(cat).doubleValue();
    double tickValue = ((max - _origin) * tick) + _origin;
    String label = "" + Math.round(tickValue * 1000) / 1000d;
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    int labelW = (int) labelBounds.getWidth();
    int labelH = (int) labelBounds.getHeight();

    double centerX = radarArea.getCenterX();
    double centerY = radarArea.getCenterY();

    double adj = middlePoint.distance(centerX, centerY);
    double opp = TICK_MARK_LENGTH / 2 + 4;
    double hyp = Math.sqrt(Math.pow(opp, 2) + Math.pow(adj, 2));
    double angle = Math.toDegrees(Math.atan(opp / adj));
    int charHeight = g2.getFontMetrics().getHeight();
    int charWidth = g2.getFontMetrics().charWidth('M');

    double alphaRad = Math.toRadians(axisAngle - angle);
    double labelX = centerX + (hyp * Math.cos(alphaRad));
    double labelY = centerY - (hyp * Math.sin(alphaRad)) + labelH;

    //        g2.draw(new Line2D.Double(centerX, centerY, labelX, labelY - labelH)); // test line

    double sinGap = Math.pow(Math.sin(Math.toRadians(axisAngle)), 2);
    if (axisAngle > 90 && axisAngle < 270) {
        labelY -= labelH;//from  w w w .ja  v a  2  s. c  o  m
        labelY += (charHeight * sinGap / 2);
    } else {
        labelY -= (charHeight * sinGap / 2);
    }
    double cosGap = Math.pow(Math.cos(Math.toRadians(axisAngle)), 2);
    if (axisAngle > 180) {
        labelX -= labelW;
        labelX += (charWidth * cosGap / 2);
    } else {
        labelX -= (charWidth * cosGap / 2);
    }
    //        g2.drawRect((int) labelX, (int) labelY - labelH, labelW, labelH); // test rectangle
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelX, (float) labelY);
}