Java Array Cross Product crossEndForLineVectorAlgebra(double x1, double y1, double x2, double y2, double margin, double width)

Here you can find the source of crossEndForLineVectorAlgebra(double x1, double y1, double x2, double y2, double margin, double width)

Description

Calculate cross end for given line with vector algebra:
---------------x--
Cross points:
      C D
A---------------x--B
      E F
Usage code:
 

License

Apache License

Parameter

Parameter Description
x1 line start A X
y1 line start A Y
x2 line end B X
y2 line end B Y
margin cross from B
width of cross

Return

arrays of calculated cross points: cross's points: [C x, D x, E x, F x] [C y, D y, E y, F y]

Declaration

public static double[][] crossEndForLineVectorAlgebra(double x1, double y1, double x2, double y2, double margin,
        double width) 

Method Source Code

//package com.java2s;
/*/*www. j a  v a 2 s.c  o  m*/
 * Beigesoft ?
 * 
 * Licensed under the Apache License, Version 2.0
 * 
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 */

public class Main {
    /**
     * Calculate cross end for given line with vector algebra:<br>
     * ---------------x--<br>
     * Cross points:<br>
     * &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;C&emsp;D<br>
     * A---------------x--B<br>
     * &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;E&emsp;F<br>
     * 
     * Usage code:
     * <pre>
     * </pre>
     * @param x1 line start A X
     * @param y1 line start A Y
     * @param x2 line end B X
     * @param y2 line end B Y
     * @param margin cross from B
     * @param width of cross
     * @return arrays of calculated cross points:
     * cross's points:  
     * [C x, D x, E x, F x]
     * [C y, D y, E y, F y]</li>
     * </ul>
     */
    public static double[][] crossEndForLineVectorAlgebra(double x1, double y1, double x2, double y2, double margin,
            double width) {
        double[][] result = new double[2][4];
        double[] baseVector = { x2 - x1, y2 - y1 };
        double baseVectorLenght = Math.sqrt(baseVector[0] * baseVector[0] + baseVector[1] * baseVector[1]);
        double[] baseUnitVector = { baseVector[0] / baseVectorLenght, baseVector[1] / baseVectorLenght };
        double[] normal = { -baseUnitVector[1], baseUnitVector[0] };
        //Point C:
        result[0][0] = x2 - baseUnitVector[0] * (margin + width / 2) - normal[0] * width / 2;
        result[1][0] = y2 - baseUnitVector[1] * (margin + width / 2) - normal[1] * width / 2;
        //Point D:
        result[0][1] = x2 - baseUnitVector[0] * (margin - width / 2) - normal[0] * width / 2;
        result[1][1] = y2 - baseUnitVector[1] * (margin - width / 2) - normal[1] * width / 2;
        //Point E:
        result[0][2] = x2 - baseUnitVector[0] * (margin + width / 2) + normal[0] * width / 2;
        result[1][2] = y2 - baseUnitVector[1] * (margin + width / 2) + normal[1] * width / 2;
        //Point F:
        result[0][3] = x2 - baseUnitVector[0] * (margin - width / 2) + normal[0] * width / 2;
        result[1][3] = y2 - baseUnitVector[1] * (margin - width / 2) + normal[1] * width / 2;
        return result;
    }
}

Related

  1. cross(int x1, int y1, int x2, int y2)
  2. cross2D(final float[] v1, final float[] v2)
  3. cross2D(int[] v, int[] u)
  4. cross3(double x1, double y1, double z1, double x2, double y2, double z2)
  5. cross33(float u[], float v[])
  6. crossMuliply(double a, double b, double c)
  7. crossMult(int value, int maximum, int coefficient)
  8. crossProduct(double x1, double y1, double x2, double y2, double x3, double y3)
  9. crossProduct(double[] output, double[] a, double[] b)