Extend a given line segment to a given length and holding the first point of the line as fixed. - Android java.lang

Android examples for java.lang:Math Geometry

Description

Extend a given line segment to a given length and holding the first point of the line as fixed.

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//from   ww w.ja  v  a2s  .  c  o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

import android.graphics.PointF;

public class Main {
    public static PointF extendLine(float ax, float ay, float bx, float by,
            float toLength) {
        float oldLength = length(ax, ay, bx, by);
        float lengthFraction = oldLength != 0.0f ? toLength / oldLength
                : 0.0f;
        PointF newEnd = new PointF();
        newEnd.x = ax + (bx - ax) * lengthFraction;
        newEnd.y = ay + (by - ay) * lengthFraction;
        return newEnd;
    }

    /**
     * Extend a given line segment to a given length and holding the first
     * point of the line as fixed.
     * 
     * @param lineStart
     * @param lineEnd
     *            Line segment to extend. lineStart is fixed during extension
     * @param length
     *            Length of new line segment.
     * @param The
     *            new end point of the line.
     */
    public static PointF extendLine(PointF lineStart, PointF lineEnd,
            float toLength) {
        return extendLine(lineStart.x, lineStart.y, lineEnd.x, lineEnd.y,
                toLength);
    }

    public static float length(float ax, float ay) {
        return (float) Math.sqrt(ax * ax + ay * ay);
    }

    public static float length(float ax, float ay, float bx, float by) {
        PointF v = createVector(ax, ay, bx, by);
        return length(v);
    }

    /**
     * Return the length of a vector.
     * 
     * @param v
     *            Vector Point
     * @return Length of vector.
     */
    public static float length(PointF v) {
        return length(v.x, v.y);
    }

    /**
     * Compute distance between two points.
     * 
     * @param p0
     *            starting point
     * @param p1
     *            ending point
     * 
     * @return Distance between points.
     */
    public static float length(PointF p0, PointF p1) {
        PointF v = createVector(p0, p1);
        return length(v);
    }

    public static PointF createVector(float ax, float ay, float bx, float by) {
        PointF point = new PointF();
        point.x = bx - ax;
        point.y = by - ay;
        return point;
    }

    /**
     * Construct the vector specified by two points.
     * 
     * @param lineStart
     *            The coordinate of the line starting point
     * @param lineEnd
     *            The coordinate of the line ending point
     * 
     * @return The Vector point
     */
    public static PointF createVector(PointF lineStart, PointF lineEnd) {
        return createVector(lineStart.x, lineStart.y, lineEnd.x, lineEnd.y);
    }
}

Related Tutorials