ipoint2.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**********************************************************************
  2. *<
  3. FILE: ipoint2.h
  4. DESCRIPTION: Class definintion for IPoint2: Integer 2D point.
  5. CREATED BY: Dan Silva
  6. HISTORY:
  7. *> Copyright (c) 1994, All Rights Reserved.
  8. **********************************************************************/
  9. #ifndef __IPOINT2__
  10. #define __IPOINT2__
  11. class ostream;
  12. class IPoint2 {
  13. public:
  14. int x,y;
  15. // Constructors
  16. IPoint2(){}
  17. IPoint2(int X, int Y) { x = X; y = Y; }
  18. IPoint2(const IPoint2& a) { x = a.x; y = a.y; }
  19. IPoint2(int af[2]) { x = af[0]; y = af[1]; }
  20. // Access operators
  21. int& operator[](int i) { return (&x)[i]; }
  22. const int& operator[](int i) const { return (&x)[i]; }
  23. // Conversion function
  24. operator int*() { return(&x); }
  25. // Unary operators
  26. IPoint2 operator-() const { return(IPoint2(-x,-y)); }
  27. IPoint2 operator+() const { return *this; }
  28. // Assignment operators
  29. IPoint2& operator-=(const IPoint2&);
  30. IPoint2& operator+=(const IPoint2&);
  31. DllExport IPoint2& operator*=(int);
  32. DllExport IPoint2& operator/=(int);
  33. // Binary operators
  34. DllExport IPoint2 IPoint2::operator-(const IPoint2&) const;
  35. DllExport IPoint2 IPoint2::operator+(const IPoint2&) const;
  36. DllExport int DotProd(const IPoint2&) const; // DOT PRODUCT
  37. DllExport int operator*(const IPoint2&) const; // DOT PRODUCT
  38. // Relational operators
  39. int operator==(const IPoint2& p) const { return (x == p.x && y == p.y); }
  40. };
  41. int DllExport Length(const IPoint2&);
  42. IPoint2 DllExport Normalize(const IPoint2&); // Return a unit vector.
  43. IPoint2 DllExport operator*(int, const IPoint2&); // multiply by scalar
  44. IPoint2 DllExport operator*(const IPoint2&, int); // multiply by scalar
  45. IPoint2 DllExport operator/(const IPoint2&, int); // divide by scalar
  46. ostream DllExport &operator<<(ostream&, const IPoint2&);
  47. // Inlines:
  48. inline int MaxComponent(const IPoint2& p) { return(p.x>p.y?0:1); }
  49. inline int MinComponent(const IPoint2& p) { return(p.x<p.y?0:1); }
  50. inline int Length(const IPoint2& v) {
  51. return (int)sqrt(v.x*v.x+v.y*v.y);
  52. }
  53. inline IPoint2& IPoint2::operator-=(const IPoint2& a) {
  54. x -= a.x; y -= a.y;
  55. return *this;
  56. }
  57. inline IPoint2& IPoint2::operator+=(const IPoint2& a) {
  58. x += a.x; y += a.y;
  59. return *this;
  60. }
  61. inline IPoint2& IPoint2::operator*=(int f) {
  62. x *= f; y *= f;
  63. return *this;
  64. }
  65. inline IPoint2& IPoint2::operator/=(int f) {
  66. x /= f; y /= f;
  67. return *this;
  68. }
  69. inline IPoint2 IPoint2::operator-(const IPoint2& b) const{
  70. return(IPoint2(x-b.x,y-b.y));
  71. }
  72. inline IPoint2 IPoint2::operator+(const IPoint2& b) const {
  73. return(IPoint2(x+b.x,y+b.y));
  74. }
  75. inline int IPoint2::DotProd(const IPoint2& b) const{
  76. return(x*b.x+y*b.y);
  77. }
  78. inline int IPoint2::operator*(const IPoint2& b)const {
  79. return(x*b.x+y*b.y);
  80. }
  81. inline IPoint2 operator*(int f, const IPoint2& a) {
  82. return(IPoint2(a.x*f, a.y*f));
  83. }
  84. inline IPoint2 operator*(const IPoint2& a, int f) {
  85. return(IPoint2(a.x*f, a.y*f));
  86. }
  87. inline IPoint2 operator/(const IPoint2& a, int f) {
  88. return(IPoint2(a.x/f, a.y/f));
  89. }
  90. #endif