moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kig / misc / common.h
blob3baea8fcb0fe9408bf24af430dbf9f573b9b782a
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_COMMON_H
23 #define KIG_MISC_COMMON_H
25 #include "coordinate.h"
26 #include "rect.h"
28 #include <qptrlist.h>
29 #include <qrect.h>
30 #include <kdeversion.h>
32 #include <vector>
33 #include <assert.h>
35 #ifdef KDE_IS_VERSION
36 #if KDE_IS_VERSION( 3, 1, 0 )
37 #define KIG_USE_KDOUBLEVALIDATOR
38 #else
39 #undef KIG_USE_KDOUBLEVALIDATOR
40 #endif
41 #else
42 #undef KIG_USE_KDOUBLEVALIDATOR
43 #endif
45 class ObjectImp;
46 class KigWidget;
48 extern const double double_inf;
50 /**
51 * Here, we define some algorithms which we need in
52 * various places...
55 double getDoubleFromUser( const QString& caption, const QString& label, double value,
56 QWidget* parent, bool* ok, double min, double max, int decimals );
58 /**
59 * Simple class representing a line. Used by various functions in Kig.
61 class LineData {
62 public:
63 /**
64 * \ifnot creating-python-scripting-doc
65 * Default constructor. Sets a and b to the origin.
66 * \endif
68 LineData() : a(), b() {};
69 /**
70 * Constructor. Sets a and b to the given Coordinates.
72 LineData( const Coordinate& na, const Coordinate& nb ) : a( na ), b( nb ) {};
73 /**
74 * One point on the line.
76 Coordinate a;
77 /**
78 * Another point on the line.
80 Coordinate b;
81 /**
82 * The direction of the line. Equivalent to b - a.
84 const Coordinate dir() const { return b - a; };
85 /**
86 * The length from a to b.
88 double length() const { return ( b - a ).length(); };
90 /**
91 * Return true if this line is parallel to l.
93 bool isParallelTo( const LineData& l ) const;
95 /**
96 * Return true if this line is orthogonal to l.
98 bool isOrthogonalTo( const LineData& l ) const;
102 * Equality. Tests two LineData's for equality.
104 bool operator==( const LineData& l, const LineData& r );
107 * This calcs the rotation of point a around point c by arc arc. Arc
108 * is in radians, in the range 0 < arc < 2*pi ...
110 Coordinate calcRotatedPoint( const Coordinate& a, const Coordinate& c, const double arc );
113 * this returns a point, so that the line through point t
114 * and the point returned is perpendicular to the line l.
116 Coordinate calcPointOnPerpend( const LineData& l, const Coordinate& t );
119 * this returns a point, so that the line through point t and the
120 * point returned is perpendicular to the direction given in dir...
122 Coordinate calcPointOnPerpend( const Coordinate& dir, const Coordinate& t );
125 * this returns a point, so that the line through point t
126 * and the point returned is parallel with the line l
128 Coordinate calcPointOnParallel( const LineData& l, const Coordinate& t );
131 * this returns a point, so that the line through point t
132 * and the point returned is parallel with the direction given in dir...
134 Coordinate calcPointOnParallel( const Coordinate& dir, const Coordinate& t );
138 * this calcs the point where the lines l and m intersect...
140 Coordinate calcIntersectionPoint( const LineData& l, const LineData& m );
143 * this calcs the intersection points of the circle with center c and
144 * radius sqrt( r ), and the line l. As a circle and a
145 * line have two intersection points, side tells us which one we
146 * need... It should be 1 or -1. If the line and the circle have no
147 * intersection, valid is set to false, otherwise to true...
148 * Note that sqr is the _square_ of the radius. We do this to avoid
149 * rounding errors...
151 const Coordinate calcCircleLineIntersect( const Coordinate& c,
152 const double sqr,
153 const LineData& l,
154 int side );
157 * this calcs the intersection points of the arc with center c,
158 * radius sqrt( r ), start angle sa and angle angle, and the line l.
159 * As a arc and a line can have max two intersection points, side
160 * tells us which one we need... It should be 1 or -1. If the line
161 * and the arc have no intersection, valid is set to false, otherwise
162 * to true... Note that sqr is the _square_ of the radius. We do
163 * this to avoid rounding errors...
165 const Coordinate calcArcLineIntersect( const Coordinate& c, const double sqr,
166 const double sa, const double angle,
167 const LineData& l, int side );
170 * this calculates the perpendicular projection of point p on line
171 * ab...
173 const Coordinate calcPointProjection( const Coordinate& p,
174 const LineData& l );
177 * calc the distance of point p to the line through a and b...
179 double calcDistancePointLine( const Coordinate& p,
180 const LineData& l );
183 * this sets p1 and p2 to p1' and p2' so that p1'p2' is the same line
184 * as p1p2, and so that p1' and p2' are on the border of the Rect...
186 void calcBorderPoints( Coordinate& p1, Coordinate& p2, const Rect& r );
188 * overload...
190 void calcBorderPoints( double& xa, double& xb, double& ya, double& yb, const Rect& r);
192 * cleaner overload, intended to replace the above two...
194 const LineData calcBorderPoints( const LineData& l, const Rect& r );
197 * this does the same as the above function, but only for b..
199 void calcRayBorderPoints( const Coordinate& a, Coordinate& b, const Rect& r );
202 * This function calculates the center of the circle going through the
203 * three given points..
205 const Coordinate calcCenter(
206 const Coordinate& a, const Coordinate& b, const Coordinate& c );
209 * overload...
211 void calcRayBorderPoints( const double xa, const double xb, double& ya,
212 double& yb, const Rect& r );
215 * calc the mirror point of p over the line l
217 const Coordinate calcMirrorPoint( const LineData& l,
218 const Coordinate& p );
221 * test collinearity of three points
223 bool areCollinear( const Coordinate& p1, const Coordinate& p2,
224 const Coordinate& p3 );
227 * test if a 2x2 matrix is singular (relatively to the
228 * norm of the two row vectors)
230 bool isSingular( const double& a, const double& b,
231 const double& c, const double& d );
234 * is o on the line defined by point a and point b ?
235 * fault is the allowed difference...
237 bool isOnLine( const Coordinate& o, const Coordinate& a,
238 const Coordinate& b, const double fault );
241 * is o on the segment defined by point a and point b ?
242 * this calls isOnLine(), but also checks if o is "between" a and b...
243 * fault is the allowed difference...
245 bool isOnSegment( const Coordinate& o, const Coordinate& a,
246 const Coordinate& b, const double fault );
248 bool isOnRay( const Coordinate& o, const Coordinate& a,
249 const Coordinate& b, const double fault );
251 bool isOnArc( const Coordinate& o, const Coordinate& c, const double r,
252 const double sa, const double a, const double fault );
254 Coordinate calcCircleRadicalStartPoint( const Coordinate& ca,
255 const Coordinate& cb,
256 double sqra, double sqrb );
259 * Is the line, segment, ray or vector inside r ? We need the imp to
260 * distinguish between rays, lines, segments or whatever.. ( we use
261 * their contains functions actually.. )
263 bool lineInRect( const Rect& r, const Coordinate& a, const Coordinate& b,
264 const int width, const ObjectImp* imp, const KigWidget& w );
266 template <typename T>
267 T kigMin( const T& a, const T& b )
269 return a < b ? a : b;
272 template <typename T>
273 T kigMax( const T& a, const T& b )
275 return a > b ? a : b;
278 template <typename T>
279 T kigAbs( const T& a )
281 return a >= 0 ? a : -a;
284 template <typename T>
285 int kigSgn( const T& a )
287 return a == 0 ? 0 : a > 0 ? +1 : -1;
290 extern const double test_threshold;
292 #endif