C++ (Qt)#include <QtWidgets> inline float RandF( void ){ return float(qrand()) / RAND_MAX - 0.5f;} int main(int argc, char *argv[]){ qsrand(0); const int num = 1000 * 1024; double sum[5] = { 0.0 }; for (int i = 0; i < num; ++i) { QVector3D vec(RandF(), RandF(), RandF()); vec.normalize(); double x2 = vec.x() * vec.x(); double y2 = vec.y() * vec.y(); double z2 = vec.z() * vec.z(); sum[0] += x2; sum[1] += x2 * x2; sum[2] += x2 * y2; sum[3] += x2 * z2; } for (int i = 0; i < 4; ++i) qDebug() << (sum[i] / num) << "/" << (num / sum[i]); return 0;}
C++ (Qt)QVector3D vec(RandF(), RandF(), RandF());vec.normalize();
C++ (Qt)QVector3D RandomVec( void ){ float theta = float(qrand()) / RAND_MAX * M_PI; // случайный "угол возвышения" float phi = float(qrand()) / RAND_MAX * M_PI * 2; // случайный "азимут" return QVector3D(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)); }
C++ (Qt)inline float RandF( void ){ return float(qrand()) / RAND_MAX * 2 - 1;} QVector3D RandVec( void ){ while (true) { QVector3D vec(RandF(), RandF(), RandF()); float len = vec.length(); if (len <= 1.0f) return vec / len; }}
C++ (Qt)static const double pi = 4.0*atan(1.0); inline double sign(const double & x) { return (x >= 0.0) ? 1 : -1; } int main(){ random_device rd; mt19937 gen(rd()); uniform_real_distribution<double> phi_dist(0.0, 2.0*pi); // распределение для угла phi uniform_real_distribution<double> z_dist(-1.0, 1.0); // распределение для проекции вектора на ось z const int num = 1000 * 1024; double sum[5] = { 0.0 }; for (int i = 0; i < num; ++i) { double z = z_dist(gen); double phi = phi_dist(gen); double x = sign(z)*sqrt(1.0 - z*z)*cos(phi); double y = sign(z)*sqrt(1.0 - z*z)*sin(phi); double x2 = x*x; double y2 = y*y; double z2 = z*z; sum[0] += x2; sum[1] += x2 * x2; sum[2] += x2 * y2; sum[3] += x2 * z2; } for (int i = 0; i < 4; ++i) cout << (sum[i] / num) << endl; return 0;}
C++ (Qt)static constexpr double pi = 4.0*atan(1.0);
C++ (Qt)struct Ball { QVector3D m_position; QVector3D m_speed; static float radius;};
C++ (Qt)bool BallsCollided( Ball & ball1, Ball & ball2 ){// Если шарики столкнулись то вернуть true// и рассчитать новые скорости шариков}
C++ (Qt)bool BallsCollided( Ball & ball1, Ball & ball2 ){ QVector3D r0 = ball1.pos - ball2.pos; QVector3D u = ball1.speed - ball2.speed; float u2 = u.lengthSquared(); float r0u = QVector3D::dotProduct(r0, u); float rho2 = r0.lengthSquared() - r0u*r0u/u2; return (rho2 < 4*Ball::radius * Ball::radius);}