Advertisement
575

3 - functions

575
Apr 22nd, 2023
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.37 KB | None | 0 0
  1. #pragma once
  2. #ifndef FUNCTIONS_H
  3. #define FUNCTIONS_H
  4.  
  5. #include "file_structs.h"
  6. #include "data_making.h"
  7. #include "missile.h"
  8. #include "target.h"
  9.  
  10. const double EPS = 1e-9;
  11.  
  12. coordinates MakeRotation(auto T, double phi, double theta) {
  13.     coordinates res, res_temp;
  14.  
  15.     phi = phi * std::numbers::pi_v<double> / 180.0;
  16.     theta = theta * std::numbers::pi_v<double> / 180.0;
  17.  
  18.     res_temp.x = std::cos(theta) * T.x - std::sin(theta) * T.y;
  19.     res_temp.y = std::sin(theta) * T.x + std::cos(theta) * T.y;
  20.     res_temp.z = T.z;
  21.  
  22.     res.z = std::cos(phi) * res_temp.z + std::sin(phi) * res_temp.x;
  23.     res.x = -(std::sin(phi)) * res_temp.z + std::cos(phi) * res_temp.x;
  24.     res.y = res_temp.y;
  25.  
  26.     return res;
  27. }
  28.  
  29. velocity MakeRotationVelo(auto T, double phi, double theta) {
  30.     velocity res, res_temp;
  31.  
  32.     phi = phi * std::numbers::pi_v<double> / 180.0;
  33.     theta = theta * std::numbers::pi_v<double> / 180.0;
  34.  
  35.     res_temp.x = std::cos(theta) * T.x - std::sin(theta) * T.y;
  36.     res_temp.y = std::sin(theta) * T.x + std::cos(theta) * T.y;
  37.     res_temp.z = T.z;
  38.  
  39.     res.z = std::cos(phi) * res_temp.z + std::sin(phi) * res_temp.x;
  40.     res.x = -(std::sin(phi)) * res_temp.z + std::cos(phi) * res_temp.x;
  41.     res.y = res_temp.y;
  42.  
  43.  
  44.     return res;
  45. }
  46.  
  47. //returns vector length
  48. double VectorMod(auto T) {
  49.     return std::sqrt(std::pow(T.x, 2) + std::pow(T.y, 2) + std::pow(T.z, 2));
  50. }
  51.  
  52. double VectorModZX(auto T) {
  53.     return std::sqrt(std::pow(T.x, 2) + std::pow(T.z, 2));
  54. }
  55.  
  56. double VectorModXY(auto T) {
  57.     return std::sqrt(std::pow(T.x, 2) + std::pow(T.y, 2));
  58. }
  59.  
  60. //returns cos() of angle
  61. double AngleBetweenVectors(auto T1, auto T2) {
  62.     double num = T1.x * T2.x + T1.y * T2.y + T1.z * T2.z;
  63.     double denom = VectorMod(T1) * VectorMod(T2);
  64.     return num / denom;
  65. }
  66.  
  67. double AngleBetweenVectorsZX(auto T1, auto T2) {
  68.     double num = T1.x * T2.x + T1.z * T2.z;
  69.     double denom = VectorModZX(T1) * VectorModZX(T2);
  70.     return num / denom;
  71. }
  72.  
  73. double AngleBetweenVectorsXY(auto T1, auto T2) {
  74.     double num = T1.x * T2.x + T1.y * T2.y;
  75.     double denom = VectorModXY(T1) * VectorModXY(T2);
  76.     return num / denom;
  77. }
  78.  
  79. double PointsDistance(coordinates T1, coordinates T2) {
  80.     double res;
  81.  
  82.     res = std::sqrt(std::pow(T1.x - T2.x, 2) + std::pow(T1.y - T2.y, 2) + std::pow(T1.z - T2.z, 2));
  83.  
  84.     return res;
  85. }
  86.  
  87. //returns coefficients of surface equation
  88. surface MakeTargetSurface(coordinates norm, coordinates point) {
  89.     double A, B, C, D;
  90.     surface res;
  91.  
  92.     A = norm.x;
  93.     B = norm.y;
  94.     C = norm.z;
  95.     D = -(norm.x * point.x) - (norm.y * point.y) - (norm.z * point.z);
  96.  
  97.     res.A = A;
  98.     res.B = B;
  99.     res.C = C;
  100.     res.D = D;
  101.  
  102.     return res;
  103. }
  104.  
  105. line MakeLine(coordinates point1, coordinates point2) {
  106.     line res;
  107.    
  108.     res.A = point1.x - point2.x;
  109.     res.B = point2.z - point1.z;
  110.     res.C = point1.z * point2.x - point2.z * point1.x;
  111.  
  112.     return res;
  113. }
  114.  
  115. line MakeLineXY(coordinates point1, coordinates point2) {
  116.     line res;
  117.  
  118.     res.A = point1.y - point2.y;
  119.     res.B = point2.x - point1.x;
  120.     res.C = point1.x * point2.y - point2.x * point1.y;
  121.  
  122.     return res;
  123. }
  124.  
  125. template <typename T>
  126. inline void swap(T& arg1, T& arg2)
  127. {
  128.     T temp = arg1;
  129.     arg1 = arg2;
  130.     arg2 = temp;
  131. };
  132.  
  133. double det(double a, double b, double c, double d) {
  134.     return a * d - b * c;
  135. }
  136.  
  137. bool PointIsGood(coordinates border1, coordinates border2, coordinates point) {
  138.     coordinates b1 = border1;
  139.     coordinates b2 = border2;
  140.     bool res;
  141.  
  142.     if (b1.z > b2.z)
  143.         swap(b1, b2);
  144.  
  145.     if (std::abs(b1.z - b2.z) < EPS) {
  146.         if (b1.x > b2.x)
  147.             swap(b1, b2);
  148.         if (b1.x < point.x && point.x < b2.x)
  149.             res = true;
  150.         else
  151.             res = false;
  152.     }
  153.     else {
  154.         if (std::abs(b1.x - b2.x) < EPS) {
  155.             if (b1.z < point.z && point.z < b2.z)
  156.                 res = true;
  157.             else
  158.                 res = false;
  159.         }
  160.         else {
  161.             if (b1.z < point.z && point.z < b2.z && std::min(b1.x, b2.x) < point.x
  162.                 && point.x < std::max(b1.x, b2.x))
  163.                 res = true;
  164.             else
  165.                 res = false;
  166.         }
  167.     }
  168.     return res;
  169. }
  170.  
  171. bool DirectionIsGood(coordinates start, coordinates direction_vector, coordinates point) {
  172.     coordinates vector1, vector2;
  173.     double cos_alpha;
  174.  
  175.     vector1.x = direction_vector.x - start.x;
  176.     vector1.z = direction_vector.z - start.z;
  177.  
  178.     vector2.x = point.x - start.x;
  179.     vector2.z = point.z - start.z;
  180.  
  181.     cos_alpha = AngleBetweenVectorsZX(vector1, vector2);
  182.    
  183.     if (cos_alpha > 0.0)
  184.         return true;
  185.     else
  186.         return false;
  187. }
  188.  
  189. bool PointIsGoodXY(coordinates border1, coordinates border2, coordinates point) {
  190.     coordinates b1 = border1;
  191.     coordinates b2 = border2;
  192.     bool res;
  193.  
  194.     if (b1.x > b2.x)
  195.         swap(b1, b2);
  196.  
  197.     if (std::abs(b1.x - b2.x) < EPS) {
  198.         if (b1.y > b2.y)
  199.             swap(b1, b2);
  200.         if (b1.y < point.y && point.y < b2.y)
  201.             res = true;
  202.         else
  203.             res = false;
  204.     }
  205.     else {
  206.         if (std::abs(b1.y - b2.y) < EPS) {
  207.             if (b1.x < point.x && point.x < b2.x)
  208.                 res = true;
  209.             else
  210.                 res = false;
  211.         }
  212.         else {
  213.             if (b1.x < point.x && point.x < b2.x && std::min(b1.y, b2.y) < point.y
  214.                 && point.y < std::max(b1.y, b2.y))
  215.                 res = true;
  216.             else
  217.                 res = false;
  218.         }
  219.     }
  220.     return res;
  221. }
  222.  
  223. bool DirectionIsGoodXY(coordinates start, coordinates direction_vector, coordinates point) {
  224.     coordinates vector1, vector2;
  225.     double cos_alpha;
  226.  
  227.     vector1.y = direction_vector.y - start.y;
  228.     vector1.x = direction_vector.x - start.x;
  229.  
  230.     vector2.y = point.y - start.y;
  231.     vector2.x = point.x - start.x;
  232.  
  233.     cos_alpha = AngleBetweenVectorsXY(vector1, vector2);
  234.  
  235.     if (cos_alpha > 0.0)
  236.         return true;
  237.     else
  238.         return false;
  239. }
  240.  
  241. bool CrossingNumberAlgo(std::vector <coordinates> polygon_vertices, coordinates point) {
  242.     bool res;
  243.     int crossings = 0;
  244.     double zn = 0.0;
  245.     line L1, L2;
  246.     coordinates direction, intersection_point;
  247.  
  248.     direction = point;
  249.     direction.x += 1.0;
  250.  
  251.     L1 = MakeLine(point, direction);
  252.  
  253.     for (unsigned int i = 0; i < polygon_vertices.size(); ++i) {
  254.         L2 = MakeLine(polygon_vertices[i], polygon_vertices[(i + 1) % polygon_vertices.size()]);
  255.  
  256.         zn = det(L1.A, L1.B, L2.A, L2.B);
  257.         if (std::abs(zn) > EPS) {
  258.             intersection_point.z = -det(L1.C, L1.B, L2.C, L2.B) / zn;
  259.             intersection_point.x = -det(L1.A, L1.C, L2.A, L2.C) / zn;
  260.             if (PointIsGood(polygon_vertices[i], polygon_vertices[(i + 1) % polygon_vertices.size()], intersection_point)
  261.                 && DirectionIsGood(point, direction, intersection_point))
  262.                 crossings++;
  263.         }
  264.     }
  265.  
  266.     if (crossings % 2 == 1)
  267.         return true;
  268.     else
  269.         return false;
  270. }
  271.  
  272. bool CrossingNumberAlgoXY(std::vector <coordinates> polygon_vertices, coordinates point) {
  273.     bool res;
  274.     int crossings = 0;
  275.     double zn = 0.0;
  276.     line L1, L2;
  277.     coordinates direction, intersection_point;
  278.  
  279.     direction = point;
  280.     direction.x += 1.0;
  281.  
  282.     L1 = MakeLineXY(point, direction);
  283.  
  284.     for (unsigned int i = 0; i < polygon_vertices.size(); ++i) {
  285.         L2 = MakeLineXY(polygon_vertices[i], polygon_vertices[(i + 1) % polygon_vertices.size()]);
  286.  
  287.         zn = det(L1.A, L1.B, L2.A, L2.B);
  288.         if (std::abs(zn) > EPS) {
  289.             intersection_point.x = -det(L1.C, L1.B, L2.C, L2.B) / zn;
  290.             intersection_point.y = -det(L1.A, L1.C, L2.A, L2.C) / zn;
  291.             if (PointIsGoodXY(polygon_vertices[i], polygon_vertices[(i + 1) % polygon_vertices.size()], intersection_point)
  292.                 && DirectionIsGoodXY(point, direction, intersection_point))
  293.                 crossings++;
  294.         }
  295.     }
  296.  
  297.     if (crossings % 2 == 1)
  298.         return true;
  299.     else
  300.         return false;
  301. }
  302.  
  303. coordinates MoveTarget(target_data T, velocity velo, double time) {
  304.     coordinates res;
  305.  
  306.     res.x = T.coord_n_obj.x + time * velo.x;
  307.     res.y = T.coord_n_obj.y + time * velo.y;
  308.     res.z = T.coord_n_obj.z + time * velo.z;
  309.  
  310.     return res;
  311. }
  312.  
  313. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement