RicardasSim

vectors cross product 3d

Mar 21st, 2019
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct{
  5.     double x;
  6.     double y;
  7.     double z;
  8. }Vector3;
  9.  
  10. void printVector(Vector3 v)
  11. {
  12.     printf("%f %f %f\n", v.x, v.y, v.z);
  13. }
  14.  
  15. Vector3 vectCrossProduct(Vector3 a, Vector3 b)
  16. {
  17.     Vector3 retV = { a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x };
  18.    
  19.     return retV;
  20. }
  21.  
  22. int main (int argc, char **argv)
  23. {
  24.  
  25.     Vector3 tstV1 = {1.0,0.0,0.0};
  26.     Vector3 tstV2 = {0.0,1.0,0.0};
  27.     Vector3 tstV3 = {1.0,1.0,1.0};
  28.  
  29.    
  30.     printVector(vectCrossProduct(tstV1,tstV2));
  31.     printVector(vectCrossProduct(tstV1,tstV3));
  32.     printVector(vectCrossProduct(tstV2,tstV3));
  33.     printf("\n");
  34.  
  35.    
  36. return 0;
  37. }
  38.  
  39. /*
  40. output:
  41.  
  42. 0.000000 0.000000 1.000000
  43. 0.000000 -1.000000 1.000000
  44. 1.000000 0.000000 -1.000000
  45.  
  46. */
Advertisement
Add Comment
Please, Sign In to add comment