1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/hugo/xy.h Thu May 06 13:21:24 2004 +0000
1.3 @@ -0,0 +1,227 @@
1.4 +// -*- c++ -*-
1.5 +#ifndef HUGO_XY_H
1.6 +#define HUGO_XY_H
1.7 +
1.8 +#include <iostream>
1.9 +
1.10 +///\ingroup misc
1.11 +///\file
1.12 +///\brief A simple two dimensional vector and a bounding box implementation
1.13 +///
1.14 +/// The class \ref hugo::xy "xy" implements
1.15 +///a two dimensional vector with the usual
1.16 +/// operations.
1.17 +///
1.18 +/// The class \ref hugo::BoundingBox "BoundingBox" can be used to determine
1.19 +/// the rectangular bounding box a set of \ref hugo::xy "xy"'s.
1.20 +///
1.21 +///\author Attila Bernath
1.22 +
1.23 +
1.24 +namespace hugo {
1.25 +
1.26 + /// \addtogroup misc
1.27 + /// @{
1.28 +
1.29 + /// A two dimensional vector (plainvector) implementation
1.30 +
1.31 + /// A two dimensional vector (plainvector) implementation
1.32 + ///with the usual vector
1.33 + /// operators.
1.34 + ///
1.35 + ///\author Attila Bernath
1.36 + template<typename T>
1.37 + class xy {
1.38 +
1.39 + public:
1.40 +
1.41 + T x,y;
1.42 +
1.43 + ///Default constructor: both coordinates become 0
1.44 + xy() : x(0), y(0) {}
1.45 +
1.46 + ///Constructing the instance from coordinates
1.47 + xy(T a, T b) : x(a), y(b) { }
1.48 +
1.49 +
1.50 + ///Gives back the square of the norm of the vector
1.51 + T normSquare(){
1.52 + return x*x+y*y;
1.53 + };
1.54 +
1.55 + ///Increments the left hand side by u
1.56 + xy<T>& operator +=(const xy<T>& u){
1.57 + x += u.x;
1.58 + y += u.y;
1.59 + return *this;
1.60 + };
1.61 +
1.62 + ///Decrements the left hand side by u
1.63 + xy<T>& operator -=(const xy<T>& u){
1.64 + x -= u.x;
1.65 + y -= u.y;
1.66 + return *this;
1.67 + };
1.68 +
1.69 + ///Multiplying the left hand side with a scalar
1.70 + xy<T>& operator *=(const T &u){
1.71 + x *= u;
1.72 + y *= u;
1.73 + return *this;
1.74 + };
1.75 +
1.76 + ///Dividing the left hand side by a scalar
1.77 + xy<T>& operator /=(const T &u){
1.78 + x /= u;
1.79 + y /= u;
1.80 + return *this;
1.81 + };
1.82 +
1.83 + ///Returns the scalar product of two vectors
1.84 + T operator *(const xy<T>& u){
1.85 + return x*u.x+y*u.y;
1.86 + };
1.87 +
1.88 + ///Returns the sum of two vectors
1.89 + xy<T> operator+(const xy<T> &u) const {
1.90 + xy<T> b=*this;
1.91 + return b+=u;
1.92 + };
1.93 +
1.94 + ///Returns the difference of two vectors
1.95 + xy<T> operator-(const xy<T> &u) const {
1.96 + xy<T> b=*this;
1.97 + return b-=u;
1.98 + };
1.99 +
1.100 + ///Returns a vector multiplied by a scalar
1.101 + xy<T> operator*(const T &u) const {
1.102 + xy<T> b=*this;
1.103 + return b*=u;
1.104 + };
1.105 +
1.106 + ///Returns a vector divided by a scalar
1.107 + xy<T> operator/(const T &u) const {
1.108 + xy<T> b=*this;
1.109 + return b/=u;
1.110 + };
1.111 +
1.112 + ///Testing equality
1.113 + bool operator==(const xy<T> &u){
1.114 + return (x==u.x) && (y==u.y);
1.115 + };
1.116 +
1.117 + ///Testing inequality
1.118 + bool operator!=(xy u){
1.119 + return (x!=u.x) || (y!=u.y);
1.120 + };
1.121 +
1.122 + };
1.123 +
1.124 + ///Reading a plainvector from a stream
1.125 + template<typename T>
1.126 + inline
1.127 + std::istream& operator>>(std::istream &is, xy<T> &z)
1.128 + {
1.129 +
1.130 + is >> z.x >> z.y;
1.131 + return is;
1.132 + }
1.133 +
1.134 + ///Outputting a plainvector to a stream
1.135 + template<typename T>
1.136 + inline
1.137 + std::ostream& operator<<(std::ostream &os, xy<T> z)
1.138 + {
1.139 + os << "(" << z.x << ", " << z.y << ")";
1.140 + return os;
1.141 + }
1.142 +
1.143 +
1.144 + /// A class to calculate or store the bounding box of plainvectors.
1.145 +
1.146 + /// A class to calculate or store the bounding box of plainvectors.
1.147 + ///
1.148 + ///\author Attila Bernath
1.149 + template<typename T>
1.150 + class BoundingBox {
1.151 + xy<T> bottom_left, top_right;
1.152 + bool _empty;
1.153 + public:
1.154 +
1.155 + ///Default constructor: an empty bounding box
1.156 + BoundingBox() { _empty = true; }
1.157 +
1.158 + ///Constructing the instance from one point
1.159 + BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
1.160 +
1.161 + ///Is there any point added
1.162 + bool empty() const {
1.163 + return _empty;
1.164 + }
1.165 +
1.166 + ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
1.167 + xy<T> bottomLeft() const {
1.168 + return bottom_left;
1.169 + };
1.170 +
1.171 + ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
1.172 + xy<T> topRight() const {
1.173 + return top_right;
1.174 + };
1.175 +
1.176 + ///Checks whether a point is inside a bounding box
1.177 + bool inside(const xy<T>& u){
1.178 + if (_empty)
1.179 + return false;
1.180 + else{
1.181 + return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
1.182 + (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
1.183 + }
1.184 + }
1.185 +
1.186 + ///Increments a bounding box with a point
1.187 + BoundingBox& operator +=(const xy<T>& u){
1.188 + if (_empty){
1.189 + bottom_left=top_right=u;
1.190 + _empty = false;
1.191 + }
1.192 + else{
1.193 + if (bottom_left.x > u.x) bottom_left.x = u.x;
1.194 + if (bottom_left.y > u.y) bottom_left.y = u.y;
1.195 + if (top_right.x < u.x) top_right.x = u.x;
1.196 + if (top_right.y < u.y) top_right.y = u.y;
1.197 + }
1.198 + return *this;
1.199 + };
1.200 +
1.201 + ///Sums a bounding box and a point
1.202 + BoundingBox operator +(const xy<T>& u){
1.203 + BoundingBox b = *this;
1.204 + return b += u;
1.205 + };
1.206 +
1.207 + ///Increments a bounding box with an other bounding box
1.208 + BoundingBox& operator +=(const BoundingBox &u){
1.209 + if ( !u.empty() ){
1.210 + *this += u.bottomLeft();
1.211 + *this += u.topRight();
1.212 + }
1.213 + return *this;
1.214 + };
1.215 +
1.216 + ///Sums two bounding boxes
1.217 + BoundingBox operator +(const BoundingBox& u){
1.218 + BoundingBox b = *this;
1.219 + return b += u;
1.220 + };
1.221 +
1.222 + };//class Boundingbox
1.223 +
1.224 +
1.225 + /// @}
1.226 +
1.227 +
1.228 +} //namespace hugo
1.229 +
1.230 +#endif //HUGO_XY_H