Java Code Examples for javafx.geometry.Point2D#distance()

The following examples show how to use javafx.geometry.Point2D#distance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FXColorPicker.java    From gef with Eclipse Public License 2.0 7 votes vote down vote up
/**
 * Draws a color wheel into the given {@link WritableImage}, starting at the
 * given offsets, in the given size (in pixel).
 *
 * @param image
 *            The {@link WritableImage} in which the color wheel is drawn.
 * @param offsetX
 *            The horizontal offset (in pixel).
 * @param offsetY
 *            The vertical offset (in pixel).
 * @param size
 *            The size (in pixel).
 */
private void renderColorWheel(WritableImage image, int offsetX, int offsetY,
		int size) {
	PixelWriter px = image.getPixelWriter();
	double radius = size / 2;
	Point2D mid = new Point2D(radius, radius);
	for (int y = 0; y < size; y++) {
		for (int x = 0; x < size; x++) {
			double d = mid.distance(x, y);
			if (d <= radius) {
				// compute hue angle
				double angleRad = d == 0 ? 0
						: Math.atan2(y - mid.getY(), x - mid.getX());
				// compute saturation depending on distance to
				// middle
				// ([0;1])
				double sat = d / radius;
				Color color = Color.hsb(angleRad * 180 / Math.PI, sat, 1);
				px.setColor(offsetX + x, offsetY + y, color);
			} else {
				px.setColor(offsetX + x, offsetY + y, Color.TRANSPARENT);
			}
		}
	}
}
 
Example 2
Source File: DataPointTooltip.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
private DataPoint findNearestDataPointWithinPickingDistance(final Chart chart, final Point2D mouseLocation) {
    DataPoint nearestDataPoint = null;
    if (!(chart instanceof XYChart)) {
        return null;
    }
    final XYChart xyChart = (XYChart) chart;
    // final double xValue = toDataPoint(xyChart.getYAxis(),
    // mouseLocation).getXValue().doubleValue();
    // TODO: iterate through all axes, renderer and datasets
    final double xValue = xyChart.getXAxis().getValueForDisplay(mouseLocation.getX());

    for (final DataPoint dataPoint : findNeighborPoints(xyChart, xValue)) {
        // Point2D displayPoint = toDisplayPoint(chart.getYAxis(),
        // (X)dataPoint.x , dataPoint.y);
        if (getChart().getFirstAxis(Orientation.HORIZONTAL) instanceof Axis) {
            final double x = xyChart.getXAxis().getDisplayPosition(dataPoint.x);
            final double y = xyChart.getYAxis().getDisplayPosition(dataPoint.y);
            final Point2D displayPoint = new Point2D(x, y);
            dataPoint.distanceFromMouse = displayPoint.distance(mouseLocation);
            if (displayPoint.distance(mouseLocation) <= getPickingDistance() && (nearestDataPoint == null || dataPoint.distanceFromMouse < nearestDataPoint.distanceFromMouse)) {
                nearestDataPoint = dataPoint;
            }
        }
    }
    return nearestDataPoint;
}
 
Example 3
Source File: GridComponent.java    From FXGLGames with MIT License 6 votes vote down vote up
public void applyExplosiveForce(double force, Point2D position, double radius) {
    Vec2 tmpVec = Pools.obtain(Vec2.class);

    for (int x = 0; x < points.length; x++) {
        for (int y = 0; y < points[0].length; y++) {
            double dist = position.distance(points[x][y].getPosition().x, points[x][y].getPosition().y);
            dist *= dist;

            if (dist < radius * radius) {
                tmpVec.set((float) position.getX(), (float) position.getY());
                tmpVec.subLocal(points[x][y].getPosition()).mulLocal((float) (-10f * force / (10000 + dist)));

                points[x][y].applyForce(tmpVec);
                points[x][y].increaseDamping(0.6f);
            }
        }
    }

    Pools.free(tmpVec);
}
 
Example 4
Source File: GridComponent.java    From FXGLGames with MIT License 6 votes vote down vote up
public void applyExplosiveForce(double force, Point2D position, double radius) {
    Vec2 tmpVec = Pools.obtain(Vec2.class);

    for (int x = 0; x < points.length; x++) {
        for (int y = 0; y < points[0].length; y++) {
            double dist = position.distance(points[x][y].getPosition().x, points[x][y].getPosition().y);
            dist *= dist;

            if (dist < radius * radius) {
                tmpVec.set((float) position.getX(), (float) position.getY());
                tmpVec.subLocal(points[x][y].getPosition()).mulLocal((float) (-10f * force / (10000 + dist)));

                points[x][y].applyForce(tmpVec);
                points[x][y].increaseDamping(0.6f);
            }
        }
    }

    Pools.free(tmpVec);
}
 
Example 5
Source File: SmartGraphEdgeCurve.java    From JavaFXSmartGraph with MIT License 4 votes vote down vote up
private void update() {                
    if (inbound == outbound) {
        /* Make a loop using the control points proportional to the vertex radius */
        
        //TODO: take into account several "self-loops" with randomAngleFactor
        double midpointX1 = outbound.getCenterX() - inbound.getRadius() * 5;
        double midpointY1 = outbound.getCenterY() - inbound.getRadius() * 2;
        
        double midpointX2 = outbound.getCenterX() + inbound.getRadius() * 5;
        double midpointY2 = outbound.getCenterY() - inbound.getRadius() * 2;
        
        setControlX1(midpointX1);
        setControlY1(midpointY1);
        setControlX2(midpointX2);
        setControlY2(midpointY2);
        
    } else {          
        /* Make a curved edge. The curve is proportional to the distance  */
        double midpointX = (outbound.getCenterX() + inbound.getCenterX()) / 2;
        double midpointY = (outbound.getCenterY() + inbound.getCenterY()) / 2;

        Point2D midpoint = new Point2D(midpointX, midpointY);

        Point2D startpoint = new Point2D(inbound.getCenterX(), inbound.getCenterY());
        Point2D endpoint = new Point2D(outbound.getCenterX(), outbound.getCenterY());

        //TODO: improvement lower max_angle_placement according to distance between vertices
        double angle = MAX_EDGE_CURVE_ANGLE;

        double distance = startpoint.distance(endpoint);

        //TODO: remove "magic number" 1500 and provide a distance function for the 
        //decreasing angle with distance
        angle = angle - (distance / 1500 * angle);

        midpoint = UtilitiesPoint2D.rotate(midpoint,
                startpoint,
                (-angle) + randomAngleFactor * (angle - (-angle)));

        setControlX1(midpoint.getX());
        setControlY1(midpoint.getY());
        setControlX2(midpoint.getX());
        setControlY2(midpoint.getY());
    }

}
 
Example 6
Source File: UtilitiesPoint2D.java    From JavaFXSmartGraph with MIT License 3 votes vote down vote up
/**
 * Computes the vector of the repelling force between two node.
 *
 * @param from              Coordinates of the first node
 * @param to                Coordinates of the second node
 * @param scale             Scale factor to be used
 * @return                  Computed force vector
 */
public static Point2D repellingForce(Point2D from, Point2D to, double scale) {
    double distance = from.distance(to); 

    Point2D vec = to.subtract(from).normalize();
    
    double factor = -repellingFunction(distance, scale);
    
    return vec.multiply(factor);       
}
 
Example 7
Source File: UtilitiesPoint2D.java    From JavaFXSmartGraph with MIT License 1 votes vote down vote up
/**
 * Computes the vector of the attractive force between two nodes.
 *
 * @param from              Coordinates of the first node
 * @param to                Coordinates of the second node
 * @param globalCount       Global number of nodes
 * @param force             Force factor to be used
 * @param scale             Scale factor to be used
 * 
 * @return                  Computed force vector
 */
public static Point2D attractiveForce(Point2D from, Point2D to, int globalCount, double force, double scale) {

    double distance = from.distance(to); 

    Point2D vec = to.subtract(from).normalize();
    
    double factor = attractiveFunction(distance, globalCount, force, scale);
    return vec.multiply(factor);              
}