Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package util;
- /**
- *
- * @author michel
- */
- public class VectorMath {
- // assign coefficients c0..c2 to vector v
- public static void setVector(double[] v, double c0, double c1, double c2) {
- v[0] = c0;
- v[1] = c1;
- v[2] = c2;
- }
- public static double[] inverseVector(double[] v){
- v[0] = -1d * v[0];
- v[1] = -1d * v[1];
- v[2] = -1d * v[2];
- return v;
- }
- public static double[] normalizeVector(double[] v){
- double sum = v[0] + v[1] + v[2];
- v[0] = v[0]/sum;
- v[1] = v[1]/sum;
- v[2] = v[2]/sum;
- return v;
- }
- // compute dotproduct of vectors v and w
- public static double dotproduct(double[] v, double[] w) {
- double r = 0;
- for (int i=0; i<3; i++) {
- r += v[i] * w[i];
- }
- return r;
- }
- // compute distance between vectors v and w
- public static double distance(double[] v, double[] w) {
- double[] tmp = new double[3];
- VectorMath.setVector(tmp, v[0]-w[0], v[1]-w[1], v[2]-w[2]);
- return Math.sqrt(VectorMath.dotproduct(tmp, tmp));
- }
- // compute dotproduct of v and w
- public static double[] crossproduct(double[] v, double[] w, double[] r) {
- r[0] = v[1] * w[2] - v[2] * w[1];
- r[1] = v[2] * w[0] - v[0] * w[2];
- r[2] = v[0] * w[1] - v[1] * w[0];
- return r;
- }
- public static double[] addVectors(double[] v, double[] w){
- double[] a = new double[3];
- a[0] = v[0] + w[0];
- a[1] = v[1] + w[1];
- a[2] = v[2] + w[2];
- return a;
- }
- public static double[] divideVector(double[] v, double division){
- v[0] = v[0]/division;
- v[1] = v[1]/division;
- v[2] = v[2]/division;
- return v;
- }
- // compute length of vector v
- public static double length(double[] v) {
- return Math.sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement