1 | /**********************************************************************
|
---|
2 | *<
|
---|
3 | FILE: point2.h
|
---|
4 |
|
---|
5 | DESCRIPTION: Class definition for Point2
|
---|
6 |
|
---|
7 | CREATED BY: Dan Silva
|
---|
8 |
|
---|
9 | HISTORY:
|
---|
10 |
|
---|
11 | *> Copyright (c) 1994, All Rights Reserved.
|
---|
12 | **********************************************************************/
|
---|
13 |
|
---|
14 | #ifndef __POINT2__
|
---|
15 |
|
---|
16 | #define __POINT2__
|
---|
17 |
|
---|
18 | class ostream;
|
---|
19 |
|
---|
20 | class Point2 {
|
---|
21 | public:
|
---|
22 | float x,y;
|
---|
23 |
|
---|
24 | // Constructors
|
---|
25 | Point2(){}
|
---|
26 | Point2(float X, float Y) { x = X; y = Y; }
|
---|
27 | Point2(double X, double Y) { x = (float)X; y = (float)Y; }
|
---|
28 | Point2(const Point2& a) { x = a.x; y = a.y; }
|
---|
29 | Point2(float af[2]) { x = af[0]; y = af[1]; }
|
---|
30 |
|
---|
31 | // Access operators
|
---|
32 | float& operator[](int i) { return (&x)[i]; }
|
---|
33 | const float& operator[](int i) const { return (&x)[i]; }
|
---|
34 |
|
---|
35 | // Conversion function
|
---|
36 | operator float*() { return(&x); }
|
---|
37 |
|
---|
38 | // Unary operators
|
---|
39 | Point2 operator-() const { return(Point2(-x,-y)); }
|
---|
40 | Point2 operator+() const { return *this; }
|
---|
41 |
|
---|
42 | // Assignment operators
|
---|
43 | DllExport Point2& operator-=(const Point2&);
|
---|
44 | DllExport Point2& operator+=(const Point2&);
|
---|
45 | DllExport Point2& operator*=(float);
|
---|
46 | DllExport Point2& operator/=(float);
|
---|
47 |
|
---|
48 | // Binary operators
|
---|
49 | DllExport Point2 operator-(const Point2&) const;
|
---|
50 | DllExport Point2 operator+(const Point2&) const;
|
---|
51 | DllExport float DotProd(const Point2&) const; // DOT PRODUCT
|
---|
52 | DllExport float operator*(const Point2&) const; // DOT PRODUCT
|
---|
53 |
|
---|
54 | // Relational operators
|
---|
55 | int operator==(const Point2& p) const { return (x == p.x && y == p.y); }
|
---|
56 | };
|
---|
57 |
|
---|
58 |
|
---|
59 | float DllExport Length(const Point2&);
|
---|
60 | int DllExport MaxComponent(const Point2&); // the component with the maximum abs value
|
---|
61 | int DllExport MinComponent(const Point2&); // the component with the minimum abs value
|
---|
62 | Point2 DllExport Normalize(const Point2&); // Return a unit vector.
|
---|
63 |
|
---|
64 | Point2 DllExport operator*(float, const Point2&); // multiply by scalar
|
---|
65 | Point2 DllExport operator*(const Point2&, float); // multiply by scalar
|
---|
66 | Point2 DllExport operator/(const Point2&, float); // divide by scalar
|
---|
67 |
|
---|
68 | ostream DllExport &operator<<(ostream&, const Point2&);
|
---|
69 |
|
---|
70 | // Inlines:
|
---|
71 |
|
---|
72 | inline float Length(const Point2& v) {
|
---|
73 | return (float)sqrt(v.x*v.x+v.y*v.y);
|
---|
74 | }
|
---|
75 |
|
---|
76 | inline Point2& Point2::operator-=(const Point2& a) {
|
---|
77 | x -= a.x; y -= a.y;
|
---|
78 | return *this;
|
---|
79 | }
|
---|
80 |
|
---|
81 | inline Point2& Point2::operator+=(const Point2& a) {
|
---|
82 | x += a.x; y += a.y;
|
---|
83 | return *this;
|
---|
84 | }
|
---|
85 |
|
---|
86 | inline Point2& Point2::operator*=(float f) {
|
---|
87 | x *= f; y *= f;
|
---|
88 | return *this;
|
---|
89 | }
|
---|
90 |
|
---|
91 | inline Point2& Point2::operator/=(float f) {
|
---|
92 | x /= f; y /= f;
|
---|
93 | return *this;
|
---|
94 | }
|
---|
95 |
|
---|
96 | inline Point2 Point2::operator-(const Point2& b) const{
|
---|
97 | return(Point2(x-b.x,y-b.y));
|
---|
98 | }
|
---|
99 |
|
---|
100 | inline Point2 Point2::operator+(const Point2& b) const {
|
---|
101 | return(Point2(x+b.x,y+b.y));
|
---|
102 | }
|
---|
103 |
|
---|
104 | inline float Point2::DotProd(const Point2& b) const{
|
---|
105 | return(x*b.x+y*b.y);
|
---|
106 | }
|
---|
107 |
|
---|
108 | inline float Point2::operator*(const Point2& b)const {
|
---|
109 | return(x*b.x+y*b.y);
|
---|
110 | }
|
---|
111 |
|
---|
112 | inline Point2 operator*(float f, const Point2& a) {
|
---|
113 | return(Point2(a.x*f, a.y*f));
|
---|
114 | }
|
---|
115 |
|
---|
116 | inline Point2 operator*(const Point2& a, float f) {
|
---|
117 | return(Point2(a.x*f, a.y*f));
|
---|
118 | }
|
---|
119 |
|
---|
120 | inline Point2 operator/(const Point2& a, float f) {
|
---|
121 | return(Point2(a.x/f, a.y/f));
|
---|
122 | }
|
---|
123 |
|
---|
124 | #endif
|
---|
125 |
|
---|