Extend a given line segment to a specified length. - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

Extend a given line segment to a specified length.

Demo Code



public class Main{
    /**/* w  ww.j a  v  a  2s  .c  om*/
     * Extend a given line segment to a specified length.
     *
     * @param p0, p1 Line segment to extend [x,y,z].
     * @param toLength Length of new line segment.
     * @param anchor Specifies the fixed point during extension. If anchor is
     * 0.0, p0 is fixed and p1 is adjusted. If anchor is 1.0, p1 is fixed and p0
     * is adjusted. If anchor is 0.5, the line is adjusted equally in each
     * direction and so on.
     */
    public static void extendLine(double[] p0, double[] p1,
            double toLength, double anchor) {
        double[] p = GeometryUtils.computePointOnLine(p0, p1, anchor);

        double length0 = toLength * anchor;
        double length1 = toLength * (1.0 - anchor);

        GeometryUtils.extendLine(p, p0, length0);
        GeometryUtils.extendLine(p, p1, length1);
    }
    /**
     * Extend a given line segment to a given length and holding the first point
     * of the line as fixed.
     *
     * @param p0, p1 Line segment to extend. p0 is fixed during extension
     * @param length Length of new line segment.
     */
    public static void extendLine(double[] p0, double[] p1, double toLength) {
        double oldLength = GeometryUtils.length(p0, p1);
        double lengthFraction = oldLength != 0.0 ? toLength / oldLength
                : 0.0;

        p1[0] = p0[0] + (p1[0] - p0[0]) * lengthFraction;
        p1[1] = p0[1] + (p1[1] - p0[1]) * lengthFraction;
        p1[2] = p0[2] + (p1[2] - p0[2]) * lengthFraction;
    }
    /**
     * Find the point on the line p0,p1 [x,y,z] a given fraction from p0.
     * Fraction of 0.0 whould give back p0, 1.0 give back p1, 0.5 returns
     * midpoint of line p0,p1 and so on. F raction can be >1 and it can be
     * negative to return any point on the line specified by p0,p1.
     *
     * @param p0 First coordinale of line [x,y,z].
     * @param p0 Second coordinale of line [x,y,z].
     * @param fractionFromP0 Point we are looking for coordinates of
     * @param p Coordinate of point we are looking for
     */
    public static double[] computePointOnLine(double[] p0, double[] p1,
            double fractionFromP0) {
        double[] p = new double[3];

        p[0] = p0[0] + fractionFromP0 * (p1[0] - p0[0]);
        p[1] = p0[1] + fractionFromP0 * (p1[1] - p0[1]);
        p[2] = p0[2] + fractionFromP0 * (p1[2] - p0[2]);

        return p;
    }
    /**
     * Find the point on the line defined by x0,y0,x1,y1 a given fraction from
     * x0,y0. 2D version of method above..
     *
     * @param x0, y0 First point defining the line
     * @param x1, y1 Second point defining the line
     * @param fractionFrom0 Distance from (x0,y0)
     * @return x, y Coordinate of point we are looking for
     */
    public static double[] computePointOnLine(double x0, double y0,
            double x1, double y1, double fractionFrom0) {
        double[] p0 = { x0, y0, 0.0 };
        double[] p1 = { x1, y1, 0.0 };

        double[] p = GeometryUtils
                .computePointOnLine(p0, p1, fractionFrom0);

        double[] r = { p[0], p[1] };
        return r;
    }
    /**
     * Return the length of a vector.
     *
     * @param v Vector to compute length of [x,y,z].
     * @return Length of vector.
     */
    public static double length(double[] v) {
        return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
    }
    /**
     * Compute distance between two points.
     *
     * @param p0, p1 Points to compute distance between [x,y,z].
     * @return Distance between points.
     */
    public static double length(double[] p0, double[] p1) {
        double[] v = GeometryUtils.createVector(p0, p1);
        return length(v);
    }
    /**
     * Compute the length of the line from (x0,y0) to (x1,y1)
     *
     * @param x0, y0 First line end point.
     * @param x1, y1 Second line end point.
     * @return Length of line from (x0,y0) to (x1,y1).
     */
    public static double length(int x0, int y0, int x1, int y1) {
        return GeometryUtils.length((double) x0, (double) y0, (double) x1,
                (double) y1);
    }
    /**
     * Compute the length of the line from (x0,y0) to (x1,y1)
     *
     * @param x0, y0 First line end point.
     * @param x1, y1 Second line end point.
     * @return Length of line from (x0,y0) to (x1,y1).
     */
    public static double length(double x0, double y0, double x1, double y1) {
        double dx = x1 - x0;
        double dy = y1 - y0;

        return Math.sqrt(dx * dx + dy * dy);
    }
    /**
     * Compute the length of a polyline.
     *
     * @param x, y Arrays of x,y coordinates
     * @param nPoints Number of elements in the above.
     * @param isClosed True if this is a closed polygon, false otherwise
     * @return Length of polyline defined by x, y and nPoints.
     */
    public static double length(int[] x, int[] y, boolean isClosed) {
        double length = 0.0;

        int nPoints = x.length;
        for (int i = 0; i < nPoints - 1; i++) {
            length += GeometryUtils.length(x[i], y[i], x[i + 1], y[i + 1]);
        }

        // Add last leg if this is a polygon
        if (isClosed && nPoints > 1) {
            length += GeometryUtils.length(x[nPoints - 1], y[nPoints - 1],
                    x[0], y[0]);
        }

        return length;
    }
    /**
     * Construct the vector specified by two points.
     *
     * @param p0, p1 Points the construct vector between [x,y,z].
     * @return v Vector from p0 to p1 [x,y,z].
     */
    public static double[] createVector(double[] p0, double[] p1) {
        double v[] = { p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2] };
        return v;
    }
}

Related Tutorials