2009-06-17から1日間の記事一覧

なぜ標準でπがないんだろう

C++

<cmath>にはstd::cosやstd::sinといった関数はありますが、 πを表す定数が用意されていません。 M_PIがそうかなーとも思ったのですが、これは非標準のようです。 仕方がないのでこんなの用意してますが namespace math { inline double pi() { return 3.14…

ベクトルの長さと正規化

C++

#include <iostream> #include <cmath> // sqrt template <class T> struct vector2 { T x, y; vector2(const T& x, const T& y) : x(x), y(y) {} // ベクトルの長さ T norm2() const { return std::sqrt(x * x + y * y); } // 正規化(ベクトルの長さを1にする) void normalize() { flo</class></cmath></iostream>…