include/vector.h
branchpyrit
changeset 25 b8232edee786
parent 22 76b7bd51d64a
child 28 ffe83ca074f3
equal deleted inserted replaced
24:d0d76e8a5203 25:b8232edee786
    30 	// constructors
    30 	// constructors
    31 	Vector3(): x(0.0f), y(0.0f), z(0.0f) {};
    31 	Vector3(): x(0.0f), y(0.0f), z(0.0f) {};
    32 	Vector3(Float ax, Float ay, Float az): x(ax), y(ay), z(az) {};
    32 	Vector3(Float ax, Float ay, Float az): x(ax), y(ay), z(az) {};
    33 
    33 
    34 	// index operator
    34 	// index operator
    35 	Float &operator[](int index)	{ return cell[index]; };
    35 	const Float &operator[](int index) const { return cell[index]; };
    36 
    36 
    37 	bool operator==(Vector3 &v) { return x==v.x && y==v.y && z==v.z; };
    37 	bool operator==(Vector3 &v) const { return x==v.x && y==v.y && z==v.z; };
    38 
    38 
    39 	// normalize
    39 	// normalize
    40 	Vector3 normalize()
    40 	Vector3 normalize()
    41 	{
    41 	{
    42 		Float f = 1.0f / mag();
    42 		Float f = 1.0f / mag();
    45 		z *= f;
    45 		z *= f;
    46 		return *this;
    46 		return *this;
    47 	}
    47 	}
    48 
    48 
    49 	// get normalized copy
    49 	// get normalized copy
    50 	Vector3 unit()
    50 	Vector3 unit() const
    51 	{
    51 	{
    52 		Vector3 u(*this);
    52 		Vector3 u(*this);
    53 		return u.normalize();;
    53 		return u.normalize();;
    54 	}
    54 	}
    55 
    55 
    56 	// square magnitude, magnitude
    56 	// square magnitude, magnitude
    57 	Float mag2()	{ return x * x + y * y + z * z; }
    57 	Float mag2() const	{ return x * x + y * y + z * z; }
    58 	Float mag()	{ return sqrtf(mag2()); }
    58 	Float mag() const	{ return sqrtf(mag2()); }
    59 
    59 
    60 	// negative
    60 	// negative
    61 	Vector3 operator-()	{ return Vector3(-x, -y, -z); }
    61 	Vector3 operator-() const { return Vector3(-x, -y, -z); }
    62 
    62 
    63 	// accumulate
    63 	// accumulate
    64 	Vector3 operator+=(const Vector3 &v)
    64 	Vector3 operator+=(const Vector3 &v)
    65 	{
    65 	{
    66 		x += v.x;
    66 		x += v.x;