@sztom
2020-12-07T09:14:12.000000Z
字数 1815
阅读 85
未分类
source:
// TODO:
// Direction类中概率相加可能会不等于100%(精度损失)
#include <cstdio>
#include <algorithm>
#include <stdexcept>
#include <cstdlib>
namespace random {
double random_float() {
const double rand_number{static_cast<double>(rand())};
return rand_number / RAND_MAX;
}
}
const double SpeedRado{1e4 + 7};
const double pi{std::acos(-1)};
double covert_degree(const double x) {
return x * pi / 180;
}
class Position {
public:
double x;
double y;
void MakeMove(const double angle, const double len = SpeedRado) {
this->x += len * std::cos(covert_degree(angle));
this->y += len * std::sin(covert_degree(angle));
}
};
template <std::size_t level = 4>
class Direction {
public:
double& operator[] (const std::size_t index) {
return this->At(index);
}
double operator[] (const std::size_t index) const {
return this->At(index);
}
double& At(const std::size_t index) {
if (index > level) {
throw std::out_of_range("Index too large");
}
return this->_d[index - 1];
}
double At(const std::size_t index) const {
if (index > level) {
throw std::out_of_range("Index too large");
}
return this->_d[index - 1];
}
std::size_t size() const {
return level;
}
static Direction<level> Add(const Direction<level>& x, const Direction<level>& y) {
Direction<level> res;
for (std::size_t i{1}; i <= level; ++i) {
res[i] = (x[i] + y[i]) / 2.0;
}
return std::move(res);
}
void multiplus(double value) {
}
double CalcAngle() const {
double target{random::random_float()};
double sum{0};
for (std::size_t i{1}; i <= this->size(); ++i) {
sum += this->at(i);
if (sum >= target) {
return (360 / this->size()) * (i - 1);
}
}
return 0;
}
private:
std::array<double, level> _d;
};
struct Wave {
Position pos;
Direction<> direction;
double enery;
void MakeMove() {
const double angle{this->direction->CalcAngle()};
this->pos->MakeMove(angle);
}
static void Crash(Wave& a, Wave& b) {
// transfer enery
const double enery_distance{a.enery - b.enery};
const double enery_transfer{enery_distance * random::random_float()};
a.enery -= enery_transfer;
b.enery += enery_transfer;
// direction modification
}
};
int main() {
return 0;
}