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 static double distance(double x1, double y1, double x2, double y2) 

Source Link

Document

Returns the distance between two points.

Usage

From source file:PathLength.java

/**
 * Flattens the path and determines the path length.
 *//*  w w  w  .j  a  v a2  s  . c  om*/
protected void initialise() {
    pathLength = 0f;

    PathIterator pi = path.getPathIterator(new AffineTransform());
    SingleSegmentPathIterator sspi = new SingleSegmentPathIterator();
    segments = new ArrayList(20);
    List indexes = new ArrayList(20);
    int index = 0;
    int origIndex = -1;
    float lastMoveX = 0f;
    float lastMoveY = 0f;
    float currentX = 0f;
    float currentY = 0f;
    float[] seg = new float[6];
    int segType;

    segments.add(new PathSegment(PathIterator.SEG_MOVETO, 0f, 0f, 0f, origIndex));

    while (!pi.isDone()) {
        origIndex++;
        indexes.add(new Integer(index));
        segType = pi.currentSegment(seg);
        switch (segType) {
        case PathIterator.SEG_MOVETO:
            segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
            currentX = seg[0];
            currentY = seg[1];
            lastMoveX = currentX;
            lastMoveY = currentY;
            index++;
            pi.next();
            break;
        case PathIterator.SEG_LINETO:
            pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
            segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
            currentX = seg[0];
            currentY = seg[1];
            index++;
            pi.next();
            break;
        case PathIterator.SEG_CLOSE:
            pathLength += Point2D.distance(currentX, currentY, lastMoveX, lastMoveY);
            segments.add(new PathSegment(PathIterator.SEG_LINETO, lastMoveX, lastMoveY, pathLength, origIndex));
            currentX = lastMoveX;
            currentY = lastMoveY;
            index++;
            pi.next();
            break;
        default:
            sspi.setPathIterator(pi, currentX, currentY);
            FlatteningPathIterator fpi = new FlatteningPathIterator(sspi, 0.01f);
            while (!fpi.isDone()) {
                segType = fpi.currentSegment(seg);
                if (segType == PathIterator.SEG_LINETO) {
                    pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
                    segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
                    currentX = seg[0];
                    currentY = seg[1];
                    index++;
                }
                fpi.next();
            }
        }
    }
    segmentIndexes = new int[indexes.size()];
    for (int i = 0; i < segmentIndexes.length; i++) {
        segmentIndexes[i] = ((Integer) indexes.get(i)).intValue();
    }
    initialised = true;
}

From source file:ec.util.chart.swing.Charts.java

@Nullable
public static LegendItemEntity getSeriesForPoint(@Nonnull Point pt, @Nonnull ChartPanel cp) {

    final double chartX;
    final double chartY;
    final Rectangle2D plotArea;
    final XYPlot plot;
    {/*  www . j  a v a2s .  co  m*/
        // Let's find the X and Y values of the clicked point
        Point2D p = cp.translateScreenToJava2D(pt);
        chartX = p.getX();
        chartY = p.getY();
        // Let's find plotArea and plot
        XYPlot tmpPlot = cp.getChart().getXYPlot();
        PlotRenderingInfo plotInfo = cp.getChartRenderingInfo().getPlotInfo();
        if (tmpPlot instanceof CombinedDomainXYPlot) {
            int subplotIndex = plotInfo.getSubplotIndex(p);
            if (subplotIndex == -1) {
                return null;
            }
            plotArea = plotInfo.getSubplotInfo(subplotIndex).getDataArea();
            plot = ((CombinedDomainXYPlot) tmpPlot).findSubplot(plotInfo, p);
        } else {
            plotArea = plotInfo.getDataArea();
            plot = tmpPlot;
        }
    }

    // Let's avoid unnecessary computation
    final ValueAxis domainAxis = plot.getDomainAxis();
    final ValueAxis rangeAxis = plot.getRangeAxis();
    final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    final double x = domainAxis.java2DToValue(chartX, plotArea, domainAxisEdge);

    final double sensitivity = TOL;
    double distanceClickSeries = TOL + 1;

    Entry<XYDataset, Comparable> result = null;

    // For each series in each datasets
    for (XYDataset dataset : asDatasetList(plot)) {
        for (int series = 0; series < dataset.getSeriesCount(); series++) {
            // Index of the closest data item of the current series just left to the click
            int lp = getNearestLeftPoint(x, 0, dataset.getItemCount(series) - 1, series, dataset);

            try {
                // X and Y values of data items to the left and to the right
                double leftX = dataset.getXValue(series, lp);
                double leftY = dataset.getYValue(series, lp);
                double rightX = dataset.getXValue(series, lp + 1);
                double rightY = dataset.getYValue(series, lp + 1);

                double lx = domainAxis.valueToJava2D(leftX, plotArea, domainAxisEdge);
                double ly = rangeAxis.valueToJava2D(leftY, plotArea, rangeAxisEdge);
                double rx = domainAxis.valueToJava2D(rightX, plotArea, domainAxisEdge);
                double ry = rangeAxis.valueToJava2D(rightY, plotArea, rangeAxisEdge);

                // Distance to left point
                double distL = Point2D.distance(lx, ly, chartX, chartY);
                // Distance to right point
                double distR = Point2D.distance(rx, ry, chartX, chartY);
                // Average of both distances
                double distLRavg = (distL + distR) / 2d;
                // Distance to the segment between L and R
                //double distSeg = Line2D.ptSegDist(leftX, leftY, rightX, rightY, chartX, chartY);
                double distSeg = ptSegDist(lx, ly, rx, ry, chartX, chartY);

                // With a line renderer, this is probably a bit of overkill as
                // distSeg would be enough, but it becomes more reliable to check all these
                // if using splines
                double tmp = Math.min(Math.min(distSeg, Math.min(distL, distR)), distLRavg);

                // Are we closer than the previous series?
                if (tmp < sensitivity && tmp < distanceClickSeries) {
                    distanceClickSeries = tmp;
                    result = new SimpleEntry<>(dataset, dataset.getSeriesKey(series));
                }
            } catch (Exception ex) {
                /*
                 * An exception might happen when some series have less data
                 * than others, catching the the exception here will simply rule
                 * them out from the detection on this click
                 */
            }
        }
    }

    return result != null ? createFakeLegendItemEntity(result.getKey(), result.getValue()) : null;
}