~ivantis/armagetronad/sty+ct+ivantis

« back to all changes in this revision

Viewing changes to src/engine/eCoord.h

  • Committer: ivantis
  • Date: 2008-09-09 21:33:18 UTC
  • Revision ID: ivantis@ivantis.net-20080909213318-k43y6yuq0zd6wbsa
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
*************************************************************************
 
4
 
 
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
 
6
Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
 
7
 
 
8
**************************************************************************
 
9
 
 
10
This program is free software; you can redistribute it and/or
 
11
modify it under the terms of the GNU General Public License
 
12
as published by the Free Software Foundation; either version 2
 
13
of the License, or (at your option) any later version.
 
14
 
 
15
This program is distributed in the hope that it will be useful,
 
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
GNU General Public License for more details.
 
19
 
 
20
You should have received a copy of the GNU General Public License
 
21
along with this program; if not, write to the Free Software
 
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
23
  
 
24
***************************************************************************
 
25
 
 
26
*/
 
27
 
 
28
#ifndef ArmageTron_COORD_H
 
29
#define ArmageTron_COORD_H
 
30
 
 
31
//#include <iostream>
 
32
#include "defs.h"
 
33
 
 
34
class eCoord{
 
35
public:
 
36
    REAL x,y;
 
37
    explicit eCoord(REAL X=0,REAL Y=0):x(X),y(Y){};
 
38
 
 
39
    // Calculations:
 
40
    inline bool operator==(const eCoord &a) const;
 
41
    bool operator!=(const eCoord &a) const{return !operator==(a);}
 
42
    eCoord operator-(const eCoord &a) const{return eCoord(x-a.x,y-a.y);}
 
43
    eCoord operator-() const{return eCoord(-x,-y);}
 
44
    eCoord operator+(const eCoord &a) const{return eCoord(x+a.x,y+a.y);}
 
45
    eCoord operator*(REAL a) const              {return eCoord(x*a,y*a);}
 
46
    const eCoord& operator*=(REAL a)    { x*=a; y*=a; return *this;}
 
47
    REAL NormSquared()                  const   {return x*x+y*y;}
 
48
    REAL Norm()                                 const   {return sqrt(NormSquared());}
 
49
    void Normalize()                                    { *this *= 1/Norm(); }
 
50
    const eCoord &operator=(const eCoord &a){ x=a.x;y=a.y; return *this;  }
 
51
 
 
52
    //scalar product:
 
53
    static REAL F(const eCoord &a,const eCoord &b){
 
54
        return(a.x*b.x+a.y*b.y);
 
55
    }
 
56
 
 
57
    // change coordinates, so that a is at zero and c is at one.
 
58
    // gives the coordinate of b.
 
59
    static REAL V(const eCoord &a,const eCoord &b,const eCoord &c){
 
60
        eCoord ab=b-a;
 
61
        eCoord ac=c-a;
 
62
        return(F(ab,ac)/F(ac,ac));
 
63
    }
 
64
 
 
65
 
 
66
    // returns a positive number if (a,*this) form a right-handed
 
67
    // coordinate system, a negative number, if ...
 
68
    // and 0, if a and *this are parallel.
 
69
    REAL   operator*(const eCoord &a) const{return -x*a.y+y*a.x;}
 
70
 
 
71
 
 
72
    // complex multiplication: turns this by angle given in a
 
73
    eCoord Turn(const eCoord &a) const{return eCoord(x*a.x-y*a.y,y*a.x+x*a.y);}
 
74
    eCoord Turn(REAL a,REAL b) const{return Turn(eCoord(a,b));}
 
75
    // complex conjugation
 
76
    eCoord Conj() const{return eCoord(x,-y);}
 
77
 
 
78
    // I/O:
 
79
    void Print(std::ostream &s) const;
 
80
    void Read(std::istream &s);
 
81
};
 
82
 
 
83
// handy function used to decide whether a*b is small enough to
 
84
// consider a and b lineary dependant (criterium: |a*b|<EPS*estim....)
 
85
REAL se_EstimatedRangeOfMult(const eCoord &a,const eCoord &b);
 
86
 
 
87
// returns a measure for the equality of two coords
 
88
REAL st_GetDifference( const eCoord & a, const eCoord & b );
 
89
 
 
90
inline bool eCoord::operator==(const eCoord &a) const{
 
91
    return ((*this-a).NormSquared()<=(EPS*EPS)*se_EstimatedRangeOfMult(*this,a));
 
92
}
 
93
 
 
94
inline std::ostream &operator<< (std::ostream &s,const eCoord &c){
 
95
    c.Print(s);
 
96
    return s;
 
97
}
 
98
 
 
99
inline std::istream &operator>> (std::istream &s,eCoord &c){
 
100
    c.Read(s);
 
101
    return s;
 
102
}
 
103
 
 
104
extern const eCoord se_zeroCoord;
 
105
#endif