moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kig / misc / coordinate.h
blobe33aca21aa226c8067cb162b12162034b1856e5f
1 /**
2 This file is part of Kig, a KDE program for Interactive Geometry...
3 Copyright (C) 2002 Dominique Devriese <devriese@kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA
19 **/
22 #ifndef KIG_MISC_COORDINATE_H
23 #define KIG_MISC_COORDINATE_H
25 class QPoint;
26 class kdbgstream;
28 /**
29 * The Coordinate class is the basic class representing a 2D location
30 * by its x and y components. It has all relevant arithmetic
31 * operators properly defined, and should be straightforward to use..
33 class Coordinate
35 public:
36 static Coordinate fromQPoint( const QPoint& p );
38 /** Constructor. Construct a new Coordinate, with a given x and y
39 * value.
41 Coordinate( double x, double y );
42 /** Copy Constructor. Construct a new Coordinate, and give it the
43 * same value as p.
45 Coordinate( const Coordinate& p );
46 /**
47 * \ifnot creating-python-scripting-doc
48 * \brief Default Constructor
50 * Constructs a new Coordinate, with x and y initialized to 0.
51 * \endif
53 Coordinate();
54 ~Coordinate() {};
56 /** Create an invalid Coordinate. This is a special value of a
57 * Coordinate that signals that something went wrong..
59 * \see Coordinate::valid
61 * \internal We represent an invalid coordinate by setting x or y to
62 * positive or negative infinity. This is handy, since it doesn't
63 * require us to adapt most of the functions, it doesn't need extra
64 * space, and most of the times that we should get an invalid coord,
65 * we get one automatically..
67 static Coordinate invalidCoord();
68 /** Return whether this is a valid Coordinate.
69 * \see Coordinate::invalidCoord
71 bool valid() const;
73 /** Distance to another Coordinate.
75 double distance ( const Coordinate& p ) const;
76 /** Length. Returns the length or norm of this coordinate.
77 * I.e. return the distance from this Coordinate to the origin.
78 * \see squareLength
80 double length () const;
81 /** Square length. Equivalent to the square of \ref length, but a
82 * bit more efficient because no square root has to be calculated.
83 * \see length
85 inline double squareLength() const;
86 /** Inverse. Returns the inverse of this Coordinate.
88 const Coordinate operator- () const;
89 /** Orthogonal. Returns a vector which is orthogonal on this vector.
90 * This relation always holds:
91 * <pre>
92 * Coordinate a = ...;
93 * assert( a*a.orthogonal() ) == 0;
94 * </pre>
96 const Coordinate orthogonal() const;
97 /** Round. Returns this coordinate, rounded to the nearest integral
98 * values.
100 const Coordinate round() const;
101 /** Normalize. This sets the length to length, while keeping the
102 * x/y ratio untouched...
104 const Coordinate normalize( double length = 1 ) const;
105 QPoint toQPoint() const;
107 Coordinate& operator= ( const Coordinate& c );
108 /** Add. Add c to this Coordinate
110 Coordinate& operator+= ( const Coordinate& c );
111 /** Subtract. Subtract c from this Coordinate
113 Coordinate& operator-= ( const Coordinate& c );
114 /** Scale. Scales this Coordinate by a factor r
116 Coordinate& operator*= ( double r );
117 /** Scale. Scales this Coordinate by a factor r
119 Coordinate& operator*= ( int r );
120 /** Scale. Scales this Coordinate by a factor 1/r
122 Coordinate& operator/= ( double r );
123 public:
124 /** X Component. The X Component of this Coordinate.
126 double x;
127 /** Y Component. The Y Component of this Coordinate.
129 double y;
131 friend kdbgstream& operator<<( kdbgstream& s, const Coordinate& t );
132 /** Add. Returns the sum of a and b.
134 friend const Coordinate operator+ ( const Coordinate& a, const Coordinate& b );
135 /** Subtract. Returns the difference between a and b.
137 friend const Coordinate operator- ( const Coordinate& a, const Coordinate& b );
138 /** Scale. Returns this a, scaled by a factor of r.
140 friend const Coordinate operator* ( const Coordinate& a, double r );
141 /** Scale. Returns a, scaled by a factor of 1/r.
143 friend const Coordinate operator/ ( const Coordinate& a, double r );
144 /** Scalar Product. Returns the scalar product of a and b.
146 friend double operator*( const Coordinate& a, const Coordinate& b );
147 /** Equal. Tests two Coordinates for equality.
149 friend bool operator==( const Coordinate&, const Coordinate& );
150 /** Not Equal. Tests two Coordinates for inequality.
152 friend bool operator!=( const Coordinate&, const Coordinate& );
155 const Coordinate operator/ ( const Coordinate& a, double r );
156 kdbgstream& operator<<( kdbgstream& s, const Coordinate& t );
157 const Coordinate operator+ ( const Coordinate& a, const Coordinate& b );
158 const Coordinate operator- ( const Coordinate& a, const Coordinate& b );
159 const Coordinate operator* ( const Coordinate& a, double r );
160 const Coordinate operator* ( double r, const Coordinate& a );
161 double operator*( const Coordinate& a, const Coordinate& b );
163 double Coordinate::squareLength() const
165 return x*x+y*y;
168 #endif