src/lemon/xy.h
author marci
Thu, 07 Oct 2004 17:21:27 +0000
changeset 940 50a153b08f07
parent 906 17f31d280385
child 964 2c0c20e90116
permissions -rw-r--r--
Coding style.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_XY_H
alpar@921
    18
#define LEMON_XY_H
athos@201
    19
athos@201
    20
#include <iostream>
athos@201
    21
klao@491
    22
///\ingroup misc
alpar@249
    23
///\file
alpar@249
    24
///\brief A simple two dimensional vector and a bounding box implementation 
alpar@249
    25
///
alpar@921
    26
/// The class \ref lemon::xy "xy" implements
alpar@249
    27
///a two dimensional vector with the usual
alpar@249
    28
/// operations.
alpar@249
    29
///
alpar@921
    30
/// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
alpar@921
    31
/// the rectangular bounding box a set of \ref lemon::xy "xy"'s.
alpar@458
    32
///
alpar@458
    33
///\author Attila Bernath
alpar@249
    34
alpar@249
    35
alpar@921
    36
namespace lemon {
alpar@431
    37
alpar@431
    38
  /// \addtogroup misc
alpar@431
    39
  /// @{
alpar@431
    40
alpar@458
    41
  /// A two dimensional vector (plainvector) implementation
alpar@242
    42
alpar@458
    43
  /// A two dimensional vector (plainvector) implementation
alpar@458
    44
  ///with the usual vector
alpar@458
    45
  /// operators.
alpar@458
    46
  ///
alpar@458
    47
  ///\author Attila Bernath
athos@207
    48
  template<typename T>
athos@207
    49
    class xy {
athos@201
    50
athos@207
    51
    public:
athos@240
    52
athos@240
    53
      T x,y;     
athos@207
    54
      
athos@207
    55
      ///Default constructor: both coordinates become 0
athos@240
    56
      xy() : x(0), y(0) {}
athos@201
    57
athos@240
    58
      ///Constructing the instance from coordinates
athos@514
    59
      xy(T a, T b) : x(a), y(b) { }
athos@201
    60
athos@201
    61
athos@207
    62
      ///Gives back the square of the norm of the vector
athos@207
    63
      T normSquare(){
athos@240
    64
	return x*x+y*y;
athos@207
    65
      };
athos@201
    66
  
athos@207
    67
      ///Increments the left hand side by u
athos@207
    68
      xy<T>& operator +=(const xy<T>& u){
athos@240
    69
	x += u.x;
athos@240
    70
	y += u.y;
athos@207
    71
	return *this;
athos@207
    72
      };
athos@201
    73
  
athos@207
    74
      ///Decrements the left hand side by u
athos@207
    75
      xy<T>& operator -=(const xy<T>& u){
athos@240
    76
	x -= u.x;
athos@240
    77
	y -= u.y;
athos@207
    78
	return *this;
athos@207
    79
      };
athos@201
    80
athos@207
    81
      ///Multiplying the left hand side with a scalar
athos@207
    82
      xy<T>& operator *=(const T &u){
athos@240
    83
	x *= u;
athos@240
    84
	y *= u;
athos@207
    85
	return *this;
athos@207
    86
      };
athos@207
    87
athos@207
    88
      ///Dividing the left hand side by a scalar
athos@207
    89
      xy<T>& operator /=(const T &u){
athos@240
    90
	x /= u;
athos@240
    91
	y /= u;
athos@207
    92
	return *this;
athos@207
    93
      };
athos@201
    94
  
athos@207
    95
      ///Returns the scalar product of two vectors
athos@207
    96
      T operator *(const xy<T>& u){
athos@240
    97
	return x*u.x+y*u.y;
athos@207
    98
      };
athos@201
    99
  
athos@207
   100
      ///Returns the sum of two vectors
athos@207
   101
      xy<T> operator+(const xy<T> &u) const {
athos@207
   102
	xy<T> b=*this;
athos@207
   103
	return b+=u;
athos@207
   104
      };
athos@201
   105
athos@207
   106
      ///Returns the difference of two vectors
athos@207
   107
      xy<T> operator-(const xy<T> &u) const {
athos@207
   108
	xy<T> b=*this;
athos@207
   109
	return b-=u;
athos@207
   110
      };
athos@201
   111
athos@207
   112
      ///Returns a vector multiplied by a scalar
athos@207
   113
      xy<T> operator*(const T &u) const {
athos@207
   114
	xy<T> b=*this;
athos@207
   115
	return b*=u;
athos@207
   116
      };
athos@201
   117
athos@207
   118
      ///Returns a vector divided by a scalar
athos@207
   119
      xy<T> operator/(const T &u) const {
athos@207
   120
	xy<T> b=*this;
athos@207
   121
	return b/=u;
athos@207
   122
      };
athos@201
   123
athos@207
   124
      ///Testing equality
athos@207
   125
      bool operator==(const xy<T> &u){
athos@240
   126
	return (x==u.x) && (y==u.y);
athos@207
   127
      };
athos@201
   128
athos@207
   129
      ///Testing inequality
athos@207
   130
      bool operator!=(xy u){
athos@240
   131
	return  (x!=u.x) || (y!=u.y);
athos@207
   132
      };
athos@201
   133
athos@207
   134
    };
athos@201
   135
alpar@814
   136
  ///Read a plainvector from a stream
alpar@814
   137
alpar@814
   138
  ///\relates xy
alpar@814
   139
  ///
athos@207
   140
  template<typename T>
athos@207
   141
  inline
athos@207
   142
  std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207
   143
  {
athos@240
   144
athos@240
   145
    is >> z.x >> z.y;
athos@207
   146
    return is;
athos@207
   147
  }
athos@201
   148
alpar@814
   149
  ///Write a plainvector to a stream
alpar@814
   150
alpar@814
   151
  ///\relates xy
alpar@814
   152
  ///
athos@207
   153
  template<typename T>
athos@207
   154
  inline
athos@207
   155
  std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207
   156
  {
athos@240
   157
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   158
    return os;
athos@207
   159
  }
athos@207
   160
athos@244
   161
alpar@458
   162
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   163
alpar@458
   164
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   165
  ///
alpar@458
   166
  ///\author Attila Bernath
athos@244
   167
  template<typename T>
athos@244
   168
    class BoundingBox {
athos@244
   169
      xy<T> bottom_left, top_right;
athos@244
   170
      bool _empty;
athos@244
   171
    public:
athos@244
   172
      
athos@244
   173
      ///Default constructor: an empty bounding box
athos@244
   174
      BoundingBox() { _empty = true; }
athos@244
   175
athos@244
   176
      ///Constructing the instance from one point
athos@244
   177
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   178
athos@244
   179
      ///Is there any point added
athos@244
   180
      bool empty() const {
athos@244
   181
	return _empty;
athos@244
   182
      }
athos@244
   183
athos@244
   184
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   185
      xy<T> bottomLeft() const {
athos@244
   186
	return bottom_left;
athos@244
   187
      };
athos@244
   188
athos@244
   189
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   190
      xy<T> topRight() const {
athos@244
   191
	return top_right;
athos@244
   192
      };
athos@244
   193
athos@244
   194
      ///Checks whether a point is inside a bounding box
athos@244
   195
      bool inside(const xy<T>& u){
athos@244
   196
	if (_empty)
athos@244
   197
	  return false;
athos@244
   198
	else{
athos@244
   199
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   200
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   201
	}
athos@244
   202
      }
athos@244
   203
  
athos@244
   204
      ///Increments a bounding box with a point
athos@244
   205
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   206
	if (_empty){
athos@244
   207
	  bottom_left=top_right=u;
athos@244
   208
	  _empty = false;
athos@244
   209
	}
athos@244
   210
	else{
athos@244
   211
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   212
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   213
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   214
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   215
	}
athos@244
   216
	return *this;
athos@244
   217
      };
athos@244
   218
  
athos@244
   219
      ///Sums a bounding box and a point
athos@244
   220
      BoundingBox operator +(const xy<T>& u){
athos@244
   221
	BoundingBox b = *this;
athos@244
   222
	return b += u;
athos@244
   223
      };
athos@244
   224
athos@244
   225
      ///Increments a bounding box with an other bounding box
athos@244
   226
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   227
	if ( !u.empty() ){
athos@244
   228
	  *this += u.bottomLeft();
athos@244
   229
	  *this += u.topRight();
athos@244
   230
	}
athos@244
   231
	return *this;
athos@244
   232
      };
athos@244
   233
  
athos@244
   234
      ///Sums two bounding boxes
athos@244
   235
      BoundingBox operator +(const BoundingBox& u){
athos@244
   236
	BoundingBox b = *this;
athos@244
   237
	return b += u;
athos@244
   238
      };
athos@244
   239
athos@244
   240
    };//class Boundingbox
athos@244
   241
athos@244
   242
alpar@431
   243
  /// @}
athos@244
   244
athos@244
   245
alpar@921
   246
} //namespace lemon
athos@201
   247
alpar@921
   248
#endif //LEMON_XY_H