[关闭]
@sztom 2020-12-07T09:14:12.000000Z 字数 1815 阅读 85

FISHER World

未分类


source:

  1. // TODO:
  2. // Direction类中概率相加可能会不等于100%(精度损失)
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <stdexcept>
  6. #include <cstdlib>
  7. namespace random {
  8. double random_float() {
  9. const double rand_number{static_cast<double>(rand())};
  10. return rand_number / RAND_MAX;
  11. }
  12. }
  13. const double SpeedRado{1e4 + 7};
  14. const double pi{std::acos(-1)};
  15. double covert_degree(const double x) {
  16. return x * pi / 180;
  17. }
  18. class Position {
  19. public:
  20. double x;
  21. double y;
  22. void MakeMove(const double angle, const double len = SpeedRado) {
  23. this->x += len * std::cos(covert_degree(angle));
  24. this->y += len * std::sin(covert_degree(angle));
  25. }
  26. };
  27. template <std::size_t level = 4>
  28. class Direction {
  29. public:
  30. double& operator[] (const std::size_t index) {
  31. return this->At(index);
  32. }
  33. double operator[] (const std::size_t index) const {
  34. return this->At(index);
  35. }
  36. double& At(const std::size_t index) {
  37. if (index > level) {
  38. throw std::out_of_range("Index too large");
  39. }
  40. return this->_d[index - 1];
  41. }
  42. double At(const std::size_t index) const {
  43. if (index > level) {
  44. throw std::out_of_range("Index too large");
  45. }
  46. return this->_d[index - 1];
  47. }
  48. std::size_t size() const {
  49. return level;
  50. }
  51. static Direction<level> Add(const Direction<level>& x, const Direction<level>& y) {
  52. Direction<level> res;
  53. for (std::size_t i{1}; i <= level; ++i) {
  54. res[i] = (x[i] + y[i]) / 2.0;
  55. }
  56. return std::move(res);
  57. }
  58. void multiplus(double value) {
  59. }
  60. double CalcAngle() const {
  61. double target{random::random_float()};
  62. double sum{0};
  63. for (std::size_t i{1}; i <= this->size(); ++i) {
  64. sum += this->at(i);
  65. if (sum >= target) {
  66. return (360 / this->size()) * (i - 1);
  67. }
  68. }
  69. return 0;
  70. }
  71. private:
  72. std::array<double, level> _d;
  73. };
  74. struct Wave {
  75. Position pos;
  76. Direction<> direction;
  77. double enery;
  78. void MakeMove() {
  79. const double angle{this->direction->CalcAngle()};
  80. this->pos->MakeMove(angle);
  81. }
  82. static void Crash(Wave& a, Wave& b) {
  83. // transfer enery
  84. const double enery_distance{a.enery - b.enery};
  85. const double enery_transfer{enery_distance * random::random_float()};
  86. a.enery -= enery_transfer;
  87. b.enery += enery_transfer;
  88. // direction modification
  89. }
  90. };
  91. int main() {
  92. return 0;
  93. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注