Java Geometry Algorithm projectPoint(float x, float y, Line2D ray, float distance)

Here you can find the source of projectPoint(float x, float y, Line2D ray, float distance)

Description

Move a point a given distance along a line parallel to the ray implied by the the given line.

License

Educational Community License

Return

the new point

Declaration


public static Point2D projectPoint(float x, float y, Line2D ray,
        float distance) 

Method Source Code

//package com.java2s;
/*//from  ww  w . j a va  2  s  .c  o m
 * Copyright 2003-2010 Tufts University  Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.osedu.org/licenses/ECL-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import java.awt.geom.Point2D;
import java.awt.geom.Line2D;

public class Main {
    /**
     * Move a point a given distance along a line parallel to the
     * ray implied by the the given line.  The direction of projection
     * is parallel to the ray that begins at the first point in the line,
     * and passes through the second point of the line.  The start point
     * does not need to be on the given line.
     * 
     * @return the new point
     */

    public static Point2D projectPoint(float x, float y, Line2D ray,
            float distance) {

        // todo: this impl could be much simpler

        final Point2D.Float p = new Point2D.Float();

        final double rotation = computeVerticalRotation(ray);

        final java.awt.geom.AffineTransform tx = new java.awt.geom.AffineTransform();

        tx.setToTranslation(x, y);
        tx.rotate(rotation);
        tx.translate(0, distance);
        tx.transform(p, p);

        return p;
    }

    public static Point2D projectPoint(Point2D.Float p, Line2D ray,
            float distance) {
        return projectPoint(p.x, p.y, ray, distance);
    }

    public static double computeVerticalRotation(Line2D l) {
        return computeVerticalRotation(l.getX1(), l.getY1(), l.getX2(),
                l.getY2());
    }

    /**
     * Compute the rotation needed to normalize the line segment to vertical orientation, making it
     * parrallel to the Y axis.  So vertical lines will return either 0 or Math.PI (180 degrees), horizontal lines
     * will return +/- PI/2.  (+/- 90 degrees).  In the rotated space, +y values will move down, +x values will move right.
     */
    public static double computeVerticalRotation(double x1, double y1,
            double x2, double y2) {
        final double xdiff = x1 - x2;
        final double ydiff = y1 - y2;
        final double slope = xdiff / ydiff; // really, inverse slope
        double radians = -Math.atan(slope);

        if (xdiff >= 0 && ydiff >= 0)
            radians += Math.PI;
        else if (xdiff <= 0 && ydiff >= 0)
            radians -= Math.PI;

        return radians;
    }
}

Related

  1. preprocess(Point2D pa, Point2D pb, Point2D pc)
  2. prodEscalar(Point2D p1, Point2D p2)
  3. project(Point2D sourceLocation, double angle, double length)
  4. project(Point2D.Double sourceLocation, double angle, double length)
  5. projectionFactor(Point pt, Point start, Point stop)
  6. projectPointOntoLine(Point2D pt, Line2D ln)
  7. quantisePoint(Point2D.Double dpos, Point gpos)
  8. relTo(final Point2D from, final Point2D to, final Point2D rel)
  9. resample(Vector points, int n, Vector newPoints)