Example usage for java.awt.geom Point2D getX

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

Introduction

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

Prototype

public abstract double getX();

Source Link

Document

Returns the X coordinate of this Point2D in double precision.

Usage

From source file:smanilov.mandelbrot.compute.Computer.java

/**
 * Converts a pixel coordinate into a complex number.
 * @param pixelCenter The center of the pixel coordinate system.
 * @param scale log2(pixels) per unit in the complex plane. 
 * @param center The center of the view on the complex plane.
 *///from ww w .  ja v a2 s . com
private static Complex toComplex(double x, double y, Point pixelCenter, int scale, Point2D center) {
    double dx = (double) (x - pixelCenter.getX());
    double dy = (double) (-y + pixelCenter.getY());
    double real = (double) dx / (1 << scale) + center.getX();
    double imaginary = (double) dy / (1 << scale) + center.getY();
    return new Complex(real, imaginary);
}

From source file:controller.VisLP.java

/**
 * Draw the linear constraints of an {@code LP} and color
 * it's feasible region in a given {@code CCSystem}.
 * //from w  w w .ja va 2  s. c o  m
 * @param cs
 *        a {@code CCSystem}.
 * @param lp
 *        a {@code LP}.
 */
static void drawLP(CCSystem cs, LP lp) {
    cs.clear();

    /* Don't draw the LP if it is not in two variables */
    if (lp == null || lp.getNoBasic() != 2) {
        cs.setVisibleAxes(false);
        return;
    }
    cs.setVisibleAxes(true);

    CCSLine line;
    FieldMatrix<BigFraction> cons = lp.getConstraints();
    cons = checkForBounds(cons);

    /* Draw all constraints as lines, except hidden bounded constraint */
    for (int i = 0; i < cons.getRowDimension() - 1; i++) {
        line = new CCSLine(cons.getEntry(i, 0).doubleValue(), cons.getEntry(i, 1).doubleValue(),
                cons.getEntry(i, 2).doubleValue(), Color.gray);
        cs.addLine(line);
    }

    Point2D[] fpoints = getFeasibleIntersections(cons);

    /* 
     * Move the center of the coordinate system
     * to the center of the feasible region.
     */
    if (readScope) {
        scopeArea(cs, fpoints, true);
        readScope = false;
    }
    if (feasScope && lp.feasible(false)) {
        scopeArea(cs, fpoints, false);
        feasScope = false;
    }

    /* If there is no feasible region there is no need to try to color it */
    if (fpoints.length == 0)
        return;

    /* Draw all feasible solutions as points */
    Point2D[] pconv = convex(fpoints);
    for (Point2D p2d : pconv) {
        CCSPoint ccsp = new CCSPoint(p2d.getX(), p2d.getY());
        if (!unb.contains(p2d))
            cs.addPoint(ccsp);
    }

    /* Color the region depending on whether it is unbounded or not. */
    if (unb.size() == 0) {
        cs.addPolygon(new CCSPolygon(pconv, Color.pink, true));
    } else if (unb.size() == 1) {
        GradientPaint gp = new GradientPaint(pconv[0], Color.pink, unb.get(0), cs.getBackground());
        cs.addPolygon(new CCSPolygon(pconv, gp, true));
    } else {
        Point2D p1 = unb.get(0);
        Point2D p2 = unb.get(1);
        double xavg = (p1.getX() + p2.getX()) / 2.0;
        double yavg = (p1.getY() + p2.getY()) / 2.0;
        /* 
         * Move the end point of the gradient further away from the
         * polygon edge to make the end of the gradient look less sudden.
         */
        xavg *= 0.9;
        yavg *= 0.9;

        Point2D pavg = new Point2D.Double(xavg, yavg);

        /* Fade into the background color */
        GradientPaint gp = new GradientPaint(pconv[0], Color.pink, pavg, cs.getBackground());

        cs.addPolygon(new CCSPolygon(pconv, gp, true));
    }

    /* Draw the current objective function */
    FieldVector<BigFraction> obj = lp.getObjFunction();
    line = new CCSLine(obj.getEntry(0).doubleValue(), obj.getEntry(1).doubleValue(), lp.objVal().doubleValue(),
            Color.red);
    cs.addLine(line);

    /* Draw the current basic solution as a point. */
    BigFraction[] point = lp.point();
    cs.addPoint(new CCSPoint(point[0].doubleValue(), point[1].doubleValue(), Color.red, true));
}

From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java

/**
 *
 * @param entity/*from  www.jav  a 2s. c  om*/
 * @param chartPanel
 * @param center
 * @return
 */
public static Shape toViewXY(Shape entity, ChartPanel chartPanel, Point2D center) {
    AffineTransform toPosition = getModelToViewTransformXY(chartPanel, center.getX(), center.getY());
    toPosition.concatenate(getTranslateInstance(-center.getX(), -center.getY()));
    return toPosition.createTransformedShape(entity);
}

From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java

/**
 *
 * @param entity/*from  w  w  w.j a  va  2  s  .co m*/
 * @param chartPanel
 * @return
 */
public static Point2D toViewXY(Point2D entity, ChartPanel chartPanel) {
    AffineTransform toPosition = getModelToViewTransformXY(chartPanel, entity.getX(), entity.getY());
    toPosition.concatenate(getTranslateInstance(-entity.getX(), -entity.getY()));
    return toPosition.transform(entity, null);
}

From source file:net.sqs2.omr.session.logic.PageImageRenderer.java

private static void drawFormAreas(int pageIndex, float densityThreshold, FormMaster master,
        PageTaskResult pageTaskResult, Graphics2D g, MarkRecognitionConfig markRecognizationConfig,
        DeskewedImageSource pageSource, int focusedColumnIndex, Rectangle scope) {
    int formAreaIndexInPage = 0;

    int minX = Integer.MAX_VALUE;
    int minY = Integer.MAX_VALUE;
    int maxX = Integer.MIN_VALUE;
    int maxY = Integer.MIN_VALUE;

    for (FormArea formArea : master.getFormAreaListByPageIndex(pageIndex)) {
        FormAreaResult result = (FormAreaResult) pageTaskResult.getPageAreaResultList()
                .get(formAreaIndexInPage);
        if (formArea.isMarkArea()) {
            if (focusedColumnIndex == formArea.getQuestionIndex()) {

                Rectangle rect = formArea.getRect();

                Point2D p1 = pageSource.getPoint((int) rect.getX(), (int) rect.getY());
                Point2D p2 = pageSource.getPoint((int) (rect.getX() + rect.getWidth()),
                        (int) (rect.getY() + rect.getHeight()));

                minX = Math.min(minX, (int) p1.getX());
                minY = Math.min(minY, (int) p1.getY());
                maxX = Math.max(maxX, (int) p2.getX());
                maxY = Math.max(maxY, (int) p2.getY());

                if (result.getDensity() < densityThreshold) {
                    g.setColor(FOCUSED_MARKED_COLOR);
                } else {
                    g.setColor(FOCUSED_NO_MARKED_COLOR);
                }//from   w  w  w.j  av  a 2 s .  c  o m

            } else {
                if (result.getDensity() < densityThreshold) {
                    g.setColor(MARKED_COLOR);
                } else {
                    g.setColor(NO_MARKED_COLOR);
                }
            }

            g.fillPolygon(pageSource.createRectPolygon(
                    getExtendedRectangle(formArea.getRect(), markRecognizationConfig.getHorizontalMargin(),
                            markRecognizationConfig.getVerticalMargin())));
            g.drawPolygon(pageSource.createRectPolygon(
                    getExtendedRectangle(formArea.getRect(), markRecognizationConfig.getHorizontalMargin() + 3,
                            markRecognizationConfig.getVerticalMargin() + 3)));

        } else {
            g.setColor(TEXTAREA_COLOR);
            g.fillPolygon(pageSource.createRectPolygon(formArea.getRect()));
        }
        formAreaIndexInPage++;
    }

    if (scope != null) {
        int borderMarginX = 20;
        int borderMarginY = 3;
        int margin = 40;

        int x = minX - borderMarginX;
        int y = minY - borderMarginY;
        int width = maxX - minX + borderMarginX * 2;
        int height = maxY - minY + borderMarginY * 2;

        scope.x = minX - margin;
        scope.y = minY - margin;
        scope.width = maxX - minX + margin * 2;
        scope.height = maxY - minY + margin * 2;

        Stroke stroke = new BasicStroke(4.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 2.0f,
                new float[] { 4.0f, 8.0f }, 0.0f);
        g.setStroke(stroke);
        g.setColor(FOCUSED_SCOPE_COLOR);
        g.drawRoundRect(x, y, width, height, 20, 20);
    }

}

From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java

/**
 *
 * @param s/* w  w  w .  j  a  v  a  2 s . co  m*/
 * @param baseX
 * @param baseY
 * @param scalex
 * @param scaley
 * @return
 */
public static AffineTransform scaleAtOrigin(Shape s, double baseX, double baseY, float scalex, float scaley) {
    Point2D center = new Point2D.Double(baseX, baseY);
    //Affine transforms are applied from right to left
    AffineTransform at = getTranslateInstance(center.getX(), center.getY());
    at.concatenate(getScaleInstance(scalex, scaley));
    return at;
}

From source file:com.t_oster.visicut.misc.Helper.java

public static Point toPoint(Point2D p) {
    return new Point((int) p.getX(), (int) p.getY());
}

From source file:ShapeTransform.java

/**
 * Normalize the line; the point with the lowest X is the primary point, if
 * both points have the same X, that point with the lowest Y value wins.
 * /*from  w w  w .  j a  v a  2s  .com*/
 * @param line
 *          the original line
 * @return the normalized line
 */
private static Line2D getNormalizedLine(final Line2D line) {
    final Line2D lineClone = (Line2D) line.clone();

    final Point2D p1 = line.getP1();
    final Point2D p2 = line.getP2();
    if (p1.getX() < p2.getX()) {
        return lineClone;
    }
    if (p1.getX() > p2.getX()) {
        lineClone.setLine(p2, p1);
        return lineClone;
    }
    if (p1.getY() < p2.getY()) {
        return lineClone;
    }
    lineClone.setLine(p2, p1);
    return lineClone;
}

From source file:ShapeTransform.java

/**
 * Resizes a line. Instead of creating a GeneralPath (as AffineTransform's
 * scale would do) we modify the line itself.
 * //from   ww  w  . j  a  v a2 s.c om
 * @param line
 *          the line that should be scaled
 * @param width
 *          the new width of the line bounds
 * @param height
 *          the new height of the line bounds
 * @return the scale Line2D object.
 */
private static Line2D resizeLine(final Line2D line, final double width, final double height) {
    final Line2D newLine = getNormalizedLine(line);
    final Point2D p1 = newLine.getP1();
    final Point2D p2 = newLine.getP2();
    final double normPointX = (p1.getX() - p2.getX());
    final double normPointY = (p1.getY() - p2.getY());
    final double scaleX = (normPointX == 0) ? 1 : width / Math.abs(normPointX);
    final double scaleY = (normPointY == 0) ? 1 : height / Math.abs(normPointY);
    p2.setLocation((p2.getX() - p1.getX()) * scaleX + p1.getX(), (p2.getY() - p1.getY()) * scaleY + p1.getY());
    newLine.setLine(p1, p2);
    return newLine;
}

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;
    {/* ww  w.j  ava 2  s.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;
}