CMSC23700 Common Code Library
Support code for CS23700 programming projects
cs237-vec2.hxx
Go to the documentation of this file.
1 
8 /*
9  * COPYRIGHT (c) 2013 John Reppy (http://cs.uchicago.edu/~jhr)
10  * All rights reserved.
11  */
12 
13 #ifndef _CS237_VEC2_HXX_
14 #define _CS237_VEC2_HXX_
15 
16 #ifndef _CS237_HXX_
17 #error "c237-vec2.hxx should not be included directly"
18 #endif
19 
20 namespace cs237 {
21 
22  namespace __detail {
23 
25  template <typename T>
26  struct vec2 {
27  T x, y;
28 
29  vec2 () : x(T(0)), y(T(0)) { }
30  vec2 (vec2 const &v) : x(v.x), y(v.y) { }
31  explicit vec2 (T xx) : x(xx), y(T(0)) { }
32  explicit vec2 (T xx, T yy) : x(xx), y(yy) { }
33  explicit vec2 (vec3<T> const &v); // first two components of 3-vector
34  explicit vec2 (vec4<T> const &v); // first two components of 4-vector
35 
38  T & operator[] (unsigned int const &i);
39  T const & operator[] (unsigned int const &i) const;
41 
42  vec2 & operator= (vec2 const &v);
43 
45  vec2 & operator+= (T const &s);
47  vec2 & operator+= (vec2 const &v);
48 
50  vec2 & operator-= (T const &s);
52  vec2 & operator-= (vec2 const &v);
53 
55  vec2 & operator*= (T const &s);
57  vec2 & operator*= (vec2 const &v);
58 
60  vec2 & operator/= (T const &s);
61 
63  T length () const;
65  vec2 & normalize ();
66  };
67 
71  template <typename T>
72  std::ostream& operator<< (std::ostream& s, vec2<T> const &v);
73 
74  } /* _namespace __detail */
75 
80 
81 } /* namespace cs237 */
82 
83 #endif /* !_CS237_VEC2_HXX_ */
vec2 & operator/=(T const &s)
divide this vector by a scalar
Definition: cs237-vec2.inl:107
vec2()
Definition: cs237-vec2.hxx:29
vec2(T xx)
Definition: cs237-vec2.hxx:31
vec2 & normalize()
normalize the vector
Definition: cs237-vec2.inl:122
vec2 & operator*=(T const &s)
multiply this vector by a scalar
Definition: cs237-vec2.inl:91
T y
Definition: cs237-vec2.hxx:27
T x
Definition: cs237-vec2.hxx:27
vec2(T xx, T yy)
Definition: cs237-vec2.hxx:32
T & operator[](unsigned int const &i)
Definition: cs237-vec2.inl:36
__detail::vec2< double > vec2d
Two-element, double-precision floating-point vectors.
Definition: cs237-vec2.hxx:79
vec2 & operator=(vec2 const &v)
Definition: cs237-vec2.inl:50
template class for four-element vectors
Definition: cs237-types.hxx:27
vec2(vec2 const &v)
Definition: cs237-vec2.hxx:30
__detail::vec2< float > vec2f
Two-element, single-precision floating-point vectors.
Definition: cs237-vec2.hxx:77
vec2 & operator+=(T const &s)
add a scalar to this vector
Definition: cs237-vec2.inl:59
Definition: cs237-aabb.hxx:18
template class for three-element vectors
Definition: cs237-types.hxx:26
template class for two-element vectors
Definition: cs237-types.hxx:25
vec2 & operator-=(T const &s)
subtract a scalar from this vector
Definition: cs237-vec2.inl:75
T length() const
the length (magnitude) of the vector
Definition: cs237-vec2.inl:116