Java Franction Convert fractionAlongRay(double px, double py, double qx, double qy, double rx, double ry)

Here you can find the source of fractionAlongRay(double px, double py, double qx, double qy, double rx, double ry)

Description

Calculate the scalar such that when the length of the ray is multiplied by this scalar, it will provide the closest point along the ray to the reference point.

License

Apache License

Parameter

Parameter Description
px X coord of reference point
py Y coord of reference point
qx X coord of ray origin
qy Y coord of ray origin
rx X length of ray
ry Y length of ray

Return

Scalar of ray

Declaration

public static double fractionAlongRay(double px, double py, double qx, double qy, double rx, double ry) 

Method Source Code

//package com.java2s;
/*// w  ww. j  av a  2s . c  o m
 * Copyright 2011 Mark McKay
 * 
 * Licensed under the Apache 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.apache.org/licenses/LICENSE-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.
 */

public class Main {
    /**
     * Calculate the scalar such that when the length of the ray is
     * multiplied by this scalar, it will provide the closest point
     * along the ray to the reference point.
     * 
     * @param px X coord of reference point
     * @param py Y coord of reference point
     * @param qx X coord of ray origin
     * @param qy Y coord of ray origin
     * @param rx X length of ray
     * @param ry Y length of ray
     * @return Scalar of ray
     */
    public static double fractionAlongRay(double px, double py, double qx, double qy, double rx, double ry) {
        double sx = px - qx;
        double sy = py - qy;

        return dot(sx, sy, rx, ry) / dot(rx, ry, rx, ry);
    }

    public static double dot(double ax, double ay, double bx, double by) {
        return ax * bx + ay * by;
    }

    public static int dot(int ax, int ay, int bx, int by) {
        return ax * bx + ay * by;
    }
}

Related

  1. fraction(StringBuilder buf, int scale, long ms)
  2. fractionalPart(float fnum)
  3. fractionalTime(int hour, int minute, int second)
  4. fractionDigits(double number)
  5. fractionOfStringUppercase(String input)