diff options
author | 53hornet <53hornet@gmail.com> | 2019-02-02 23:33:15 -0500 |
---|---|---|
committer | 53hornet <53hornet@gmail.com> | 2019-02-02 23:33:15 -0500 |
commit | db072ad4dc181eca5a1458656b130beb43f475bf (patch) | |
tree | a3c03c7f5497cb70503e2486662fa85cfb53415a /hw2/include/vec2d.h | |
download | csci427-db072ad4dc181eca5a1458656b130beb43f475bf.tar.xz csci427-db072ad4dc181eca5a1458656b130beb43f475bf.zip |
Diffstat (limited to 'hw2/include/vec2d.h')
-rw-r--r-- | hw2/include/vec2d.h | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/hw2/include/vec2d.h b/hw2/include/vec2d.h new file mode 100644 index 0000000..36e0e5c --- /dev/null +++ b/hw2/include/vec2d.h @@ -0,0 +1,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_ */ |