summaryrefslogtreecommitdiff
path: root/hw2/include/vec2d.h
blob: 36e0e5c5666a90d38033b2b7bd243c7030154609 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527     */
/* without consent of either the College of William & Mary or     */
/* Pieter Peers.                                                  */
/******************************************************************/
#ifndef _VEC2D_H_
#define _VEC2D_H_

#include <ostream>
#include <cmath>

class vec2d {
 public:
  /////////////
  // Typedef //
  /////////////
  typedef float             value_type;
  typedef value_type&       reference;
  typedef const value_type& const_reference;
  typedef value_type*       iterator;
  typedef const value_type* const_iterator;

  //////////////////
  // Data Members //
  //////////////////
  union {
    struct { value_type x, y; };
    struct { value_type u, v; };
    value_type data[2];
  };

 public:
  /////////////////
  // Constructor //
  /////////////////
  explicit vec2d(const_reference value=0.0f);
  vec2d(const_reference x, const_reference y);
  vec2d(const vec2d& v);
 
  ////////////////
  // Inspectors //
  ////////////////
  static size_t size(void) { return 2; }

  iterator begin(void);
  const_iterator begin(void) const;
  iterator end(void);
  const_iterator end(void) const;

  const_reference  operator[](size_t index) const;
  reference        operator[](size_t index);

  ///////////////
  // Operators //
  ///////////////
  vec2d& operator=(const vec2d& v);

  bool operator==(const vec2d& v) const;
  bool operator!=(const vec2d& v) const;

  vec2d operator-(void) const;
  
  vec2d operator+(const vec2d& v) const;
  vec2d operator-(const vec2d& v) const;
  vec2d operator*(const vec2d& v) const;
  vec2d operator*(const_reference scale) const;
  vec2d operator/(const vec2d& v) const;
  vec2d operator/(const_reference scale) const;

  vec2d& operator+=(const vec2d& v);
  vec2d& operator-=(const vec2d& v);
  vec2d& operator*=(const vec2d& v);
  vec2d& operator*=(const_reference scale);
  vec2d& operator/=(const vec2d& v);
  vec2d& operator/=(const_reference scale);

  /////////////
  // Methods //
  /////////////
  value_type dot(const vec2d& v) const;
  value_type squared_length(void) const;
  value_type length(void) const;
  value_type squared_distance(const vec2d& v) const;
  value_type distance(const vec2d& v) const;

  ///////////////
  // Modifiers //
  ///////////////
  vec2d& abs(void);
  vec2d& normalize(void);

  /////////////
  // Friends //
  /////////////
  friend void  swap(vec2d& a, vec2d& b) { return a._swap(b); }
  friend vec2d  normalize(const vec2d& v) { return vec2d(v).normalize(); }
  friend vec2d  abs(const vec2d& v) { return vec2d(v).abs(); }  
  friend vec2d operator*(const_reference scale, const vec2d& v) { return (v*scale); }

  friend std::ostream& operator<<(std::ostream& s, const vec2d& v)
  {
    s << "[" << v.x << "," << v.y << "]";
    return s;
  }

 private:
  /////////////////////
  // Private Methods //
  /////////////////////
  void _assign(const vec2d& v);
  void _swap(vec2d& v);
};

#endif /* _VEC2D_H_ */