point2.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003     FILE: point2.h
00004 
00005     DESCRIPTION: Class definition for Point2
00006 
00007     CREATED BY: Dan Silva
00008 
00009     HISTORY:
00010 
00011  *> Copyright (c) 1994, All Rights Reserved.
00012  **********************************************************************/
00013 
00014 #pragma once
00015 
00016 #include "maxheap.h"
00017 #include "GeomExport.h"
00018 #include <iosfwd>
00019 #include <math.h>
00020 
00039 class GEOMEXPORT Point2: public MaxHeapOperators {
00040 public:
00041     float x,y;
00042 
00043     // Constructors
00045     Point2(){}
00047     Point2(float X, float Y)  { 
00048         x = X; y = Y;  
00049     }
00051     Point2(double X, double Y)  { 
00052         x = (float)X; y = (float)Y;  
00053     }
00055     Point2(int X, int Y)  { 
00056         x = (float)X; y = (float)Y;  
00057     }
00059     Point2(const Point2& a) { 
00060         x = a.x; y = a.y; 
00061     } 
00063     Point2(float af[2]) { 
00064         x = af[0]; y = af[1]; 
00065     }
00066 
00067     // Data members
00068     static const Point2 Origin;
00069     static const Point2 XAxis;
00070     static const Point2 YAxis;
00071 
00074     float& operator[](int i) { 
00075         return (&x)[i]; 
00076     }     
00079     const float& operator[](int i) const { 
00080         return (&x)[i]; 
00081     }  
00082     
00083     // Conversion function
00085     operator float*() { 
00086         return(&x); 
00087     }
00088 
00089     // Unary operators
00092     Point2 operator-() const { 
00093         return(Point2(-x,-y)); 
00094     } 
00097     Point2 operator+() const { 
00098         return *this; 
00099     }
00100     
00101     // Property functions
00103     float Length() const;
00106     int MaxComponent() const;
00109     int MinComponent() const;
00113     Point2 Normalize() const; // more accurate than *this/Length();
00114 
00115     // Assignment operators
00118     Point2& operator-=(const Point2&);
00121     Point2& operator+=(const Point2&);
00124     Point2& operator*=(float);
00127     Point2& operator/=(float);
00128 
00137     Point2& Set(float X, float Y);
00138 
00139     // Binary operators
00142     Point2 operator-(const Point2&) const;
00145     Point2 operator+(const Point2&) const;
00148     float DotProd(const Point2&) const;     // DOT PRODUCT
00151     float operator*(const Point2&) const;   // DOT PRODUCT
00152 
00153     // Relational operators
00156     int operator==(const Point2& p) const { return (x == p.x && y == p.y); }
00157     int operator!=(const Point2& p) const { return ( (x != p.x) || (y != p.y) ); }
00166   int Equals(const Point2& p, float epsilon = 1E-6f);
00167     
00168     // In-place normalize
00171     Point2& Unify();
00175     float LengthUnify();          // returns old Length
00176     };
00177 
00179 inline Point2 operator+(const Point2& a, float f) {
00180    return(Point2(a.x+f, a.y+f));
00181    }
00182 
00183 
00186 GEOMEXPORT float Length(const Point2&); 
00189 GEOMEXPORT int MaxComponent(const Point2&);  // the component with the maximum abs value
00192 GEOMEXPORT int MinComponent(const Point2&);  // the component with the minimum abs value
00195 GEOMEXPORT Point2 Normalize(const Point2&);  // more accurate than v/Length(v)
00196 
00198 GEOMEXPORT Point2 operator*(float, const Point2&);  // multiply by scalar
00200 GEOMEXPORT Point2 operator*(const Point2&, float);  // multiply by scalar
00202 GEOMEXPORT Point2 operator/(const Point2&, float);  // divide by scalar
00203 
00204 GEOMEXPORT std::ostream &operator<<(std::ostream&, const Point2&);
00205      
00206 // Inlines:
00207 
00208 inline float Length(const Point2& v) {  
00209     return (float)sqrt(v.x*v.x+v.y*v.y);
00210     }
00211 
00212 inline float Point2::Length() const {   
00213     return (float)sqrt(x*x+y*y);
00214     }
00215 
00218 inline float LengthSquared(const Point2& v) {   
00219     return (float)(v.x*v.x+v.y*v.y);
00220     }
00221 
00222 inline Point2& Point2::operator-=(const Point2& a) {    
00223     x -= a.x;   y -= a.y;  
00224     return *this;
00225     }
00226 
00227 inline Point2& Point2::operator+=(const Point2& a) {
00228     x += a.x;   y += a.y;  
00229     return *this;
00230     }
00231 
00232 inline Point2& Point2::operator*=(float f) {
00233     x *= f;   y *= f;   
00234     return *this;
00235     }
00236 
00237 inline Point2& Point2::operator/=(float f) { 
00238     x /= f; y /= f;     
00239     return *this; 
00240     }
00241 
00242 inline Point2& Point2::Set(float X, float Y) {
00243     x = X; y = Y;
00244     return *this;
00245     }
00246 
00247 inline Point2 Point2::operator-(const Point2& b) const{
00248     return(Point2(x-b.x,y-b.y));
00249     }
00250 
00251 inline Point2 Point2::operator+(const Point2& b) const {
00252     return(Point2(x+b.x,y+b.y));
00253     }
00254 
00255 inline float Point2::DotProd(const Point2& b) const{
00256     return(x*b.x+y*b.y);
00257     }
00258 
00259 inline float Point2::operator*(const Point2& b)const {
00260     return(x*b.x+y*b.y);
00261     }
00262 
00263 inline Point2 operator*(float f, const Point2& a) {
00264     return(Point2(a.x*f, a.y*f));
00265     }
00266 
00267 inline Point2 operator*(const Point2& a, float f) {
00268     return(Point2(a.x*f, a.y*f));
00269     }
00270 
00271 inline Point2 operator/(const Point2& a, float f) {
00272     return(Point2(a.x/f, a.y/f));
00273     }
00274 
00275