Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package util;
  6.  
  7. /**
  8. *
  9. * @author michel
  10. */
  11. public class VectorMath {
  12.  
  13. // assign coefficients c0..c2 to vector v
  14. public static void setVector(double[] v, double c0, double c1, double c2) {
  15. v[0] = c0;
  16. v[1] = c1;
  17. v[2] = c2;
  18. }
  19.  
  20. public static double[] inverseVector(double[] v){
  21. v[0] = -1d * v[0];
  22. v[1] = -1d * v[1];
  23. v[2] = -1d * v[2];
  24.  
  25. return v;
  26. }
  27.  
  28. public static double[] normalizeVector(double[] v){
  29. double sum = v[0] + v[1] + v[2];
  30. v[0] = v[0]/sum;
  31. v[1] = v[1]/sum;
  32. v[2] = v[2]/sum;
  33. return v;
  34.  
  35. }
  36.  
  37. // compute dotproduct of vectors v and w
  38. public static double dotproduct(double[] v, double[] w) {
  39. double r = 0;
  40. for (int i=0; i<3; i++) {
  41. r += v[i] * w[i];
  42. }
  43. return r;
  44. }
  45.  
  46. // compute distance between vectors v and w
  47. public static double distance(double[] v, double[] w) {
  48. double[] tmp = new double[3];
  49. VectorMath.setVector(tmp, v[0]-w[0], v[1]-w[1], v[2]-w[2]);
  50. return Math.sqrt(VectorMath.dotproduct(tmp, tmp));
  51. }
  52.  
  53. // compute dotproduct of v and w
  54. public static double[] crossproduct(double[] v, double[] w, double[] r) {
  55. r[0] = v[1] * w[2] - v[2] * w[1];
  56. r[1] = v[2] * w[0] - v[0] * w[2];
  57. r[2] = v[0] * w[1] - v[1] * w[0];
  58. return r;
  59. }
  60.  
  61. public static double[] addVectors(double[] v, double[] w){
  62. double[] a = new double[3];
  63. a[0] = v[0] + w[0];
  64. a[1] = v[1] + w[1];
  65. a[2] = v[2] + w[2];
  66. return a;
  67. }
  68.  
  69. public static double[] divideVector(double[] v, double division){
  70. v[0] = v[0]/division;
  71. v[1] = v[1]/division;
  72. v[2] = v[2]/division;
  73. return v;
  74. }
  75.  
  76. // compute length of vector v
  77. public static double length(double[] v) {
  78. return Math.sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  79. }
  80.  
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement