impressive_i

Vectors and angles

Apr 18th, 2021
626
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. struct Vect {
  7.   double vx;
  8.   double vy;
  9.   double vz;
  10. };
  11.  
  12. void print_v(Vect v){
  13.     cout << v.vx << "i + " << v.vy << "j + " << v.vz << "k" << endl;
  14. }
  15.  
  16. double len(Vect v){ return pow(v.vx * v.vx + v.vy * v.vy + v.vz * v.vz, 0.5); }
  17.  
  18. double scalar_mult(Vect v1, Vect v2){
  19.     return v1.vx * v2.vx + v1.vy * v2.vy + v1.vz * v2.vz;  
  20. }
  21.  
  22. Vect vector_mult(Vect v1, Vect v2){
  23.     Vect res;
  24.     res.vx = v1.vy * v2.vz - v1.vz * v2.vy;
  25.     res.vy = v1.vz * v2.vx - v1.vx * v2.vz;
  26.     res.vz = v1.vx * v2.vy - v1.vy * v2.vx;
  27.     return res;  
  28. }
  29.  
  30. int main() {
  31.  
  32.     Vect a;
  33.     Vect b;
  34.    
  35.     a.vx = 1; a.vy = 2; a.vz = 0;
  36.     b.vx = 0; b.vy = 1; b.vz = 0;
  37.    
  38.     cout << "Длина вектора a: " << len(a) << endl;
  39.     cout << "Длина вектора b: " << len(b) << endl;
  40.     cout << "Скалярное произведение (a, b): " << scalar_mult(a, b) << endl;
  41.     cout << "Векторное произведение [a × b]: "; print_v(vector_mult(a, b));
  42.     cout << "cos(a, b): " << scalar_mult(a, b) / (len(a) * len(b)) << endl;
  43.     cout << "sin(a, b): " << len(vector_mult(a, b)) / (len(a) * len(b)) << endl;
  44.     return 0;
  45. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×