• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kig

  • sources
  • kde-4.12
  • kdeedu
  • kig
  • objects
circle_imp.cc
Go to the documentation of this file.
1 // Copyright (C) 2003 Dominique Devriese <devriese@kde.org>
2 
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17 
18 #include "circle_imp.h"
19 
20 #include "bogus_imp.h"
21 #include "point_imp.h"
22 
23 #include "../misc/kigtransform.h"
24 #include "../misc/kigpainter.h"
25 #include "../misc/equation.h"
26 #include "../misc/coordinate_system.h"
27 
28 #include "../kig/kig_document.h"
29 #include "../kig/kig_view.h"
30 
31 #include <klocale.h>
32 
33 #include <math.h>
34 
35 CircleImp::CircleImp( const Coordinate& center, double radius )
36  : mcenter( center ), mradius( radius )
37 {
38 }
39 
40 CircleImp::~CircleImp()
41 {
42 }
43 
44 ObjectImp* CircleImp::transform( const Transformation& t ) const
45 {
46  if ( t.isHomothetic() )
47  {
48  Coordinate nc = t.apply( mcenter );
49  double nr = t.apply( mradius );
50  if ( nc.valid() )
51  return new CircleImp( nc, nr );
52  else return new InvalidImp;
53  }
54  else
55  {
56  // domi: i should return a ConicImp here, but i don't know how to
57  // calculate it..
58  return Parent::transform( t );
59  };
60 }
61 
62 void CircleImp::draw( KigPainter& p ) const
63 {
64  p.drawCircle( mcenter, mradius );
65 }
66 
67 bool CircleImp::contains( const Coordinate& p, int width, const KigWidget& w ) const
68 {
69  return fabs((mcenter - p).length() - mradius) <= w.screenInfo().normalMiss( width );
70 }
71 
72 bool CircleImp::inRect( const Rect& r, int width, const KigWidget& w ) const
73 {
74  // first we check if the rect contains at least one of the
75  // north/south/east/west points of the circle
76  if ( r.contains( mcenter + Coordinate( 0, -mradius ) ) ) return true;
77  if ( r.contains( mcenter + Coordinate( mradius, 0 ) ) ) return true;
78  if ( r.contains( mcenter + Coordinate( 0, mradius ) ) ) return true;
79  if ( r.contains( mcenter + Coordinate( -mradius, 0 ) ) ) return true;
80 
81  // we allow a miss of some pixels ..
82  double miss = w.screenInfo().normalMiss( width );
83  double bigradius = mradius + miss;
84  bigradius *= bigradius;
85  double smallradius = mradius - miss;
86  smallradius *= smallradius;
87 
88  const int in = -1;
89  const int undecided = 0;
90  const int out = 1;
91 
92  int inorout = undecided;
93 
94  Coordinate coords[4];
95  coords[0] = r.topLeft();
96  coords[1] = r.topRight();
97  coords[2] = r.bottomRight();
98  coords[3] = r.bottomLeft();
99 
100  // we check if the corners of the rect are either
101  for ( Coordinate* i = coords; i < coords + 4; ++i )
102  {
103  double t = ( *i - mcenter ).squareLength();
104  if ( t >= bigradius )
105  {
106  if ( inorout == in ) return true;
107  inorout = out;
108  }
109  else if ( t <= smallradius )
110  {
111  if ( inorout == out ) return true;
112  inorout = in;
113  }
114  }
115  return inorout == undecided;
116 }
117 
118 bool CircleImp::valid() const
119 {
120  return true;
121 }
122 
123 int CircleImp::numberOfProperties() const
124 {
125  // We _intentionally_ do not use the Conic properties..
126  return CurveImp::numberOfProperties() + 7;
127 }
128 
129 const QByteArrayList CircleImp::propertiesInternalNames() const
130 {
131  QByteArrayList l = CurveImp::propertiesInternalNames();
132  l << "surface";
133  l << "circumference";
134  l << "radius";
135  l << "center";
136  l << "cartesian-equation";
137  l << "simply-cartesian-equation";
138  l << "polar-equation";
139  assert( l.size() == CircleImp::numberOfProperties() );
140  return l;
141 }
142 
143 const QByteArrayList CircleImp::properties() const
144 {
145  QByteArrayList l = CurveImp::properties();
146  l << I18N_NOOP( "Surface" );
147  l << I18N_NOOP( "Circumference" );
148  l << I18N_NOOP( "Radius" );
149  l << I18N_NOOP( "Center" );
150  l << I18N_NOOP( "Expanded Cartesian Equation" );
151  l << I18N_NOOP( "Cartesian Equation" );
152  l << I18N_NOOP( "Polar Equation" );
153  assert( l.size() == CircleImp::numberOfProperties() );
154  return l;
155 }
156 
157 const ObjectImpType* CircleImp::impRequirementForProperty( int which ) const
158 {
159  if ( which < CurveImp::numberOfProperties() )
160  return CurveImp::impRequirementForProperty( which );
161  else return CircleImp::stype();
162 }
163 
164 const char* CircleImp::iconForProperty( int which ) const
165 {
166  assert( which < CircleImp::numberOfProperties() );
167  if ( which < CurveImp::numberOfProperties() )
168  return CurveImp::iconForProperty( which );
169  else if ( which == CurveImp::numberOfProperties() )
170  return "areaCircle"; // surface
171  else if ( which == CurveImp::numberOfProperties() + 1 )
172  return "circumference"; // circumference
173  else if ( which == CurveImp::numberOfProperties() + 2 )
174  return ""; //radius
175  else if ( which == CurveImp::numberOfProperties() + 3 )
176  return "baseCircle"; // circle center
177  else if ( which == CurveImp::numberOfProperties() + 4 )
178  return "kig_text"; // cartesian equation
179  else if ( which == CurveImp::numberOfProperties() + 5 )
180  return "kig_text"; // simply cartesian equation
181  else if ( which == CurveImp::numberOfProperties() + 6 )
182  return "kig_text"; // polar equation
183  else assert( false );
184  return "";
185 }
186 
187 ObjectImp* CircleImp::property( int which, const KigDocument& w ) const
188 {
189  assert( which < CircleImp::numberOfProperties() );
190  if ( which < CurveImp::numberOfProperties() )
191  return CurveImp::property( which, w );
192  if ( which == CurveImp::numberOfProperties() )
193  return new DoubleImp( surface() );
194  else if ( which == CurveImp::numberOfProperties() + 1 )
195  return new DoubleImp( circumference() );
196  else if ( which == CurveImp::numberOfProperties() + 2 )
197  return new DoubleImp( radius() );
198  else if ( which == CurveImp::numberOfProperties() + 3 )
199  return new PointImp( center() );
200  else if ( which == CurveImp::numberOfProperties() + 4 )
201  return new StringImp( cartesianEquationString( w ) );
202  else if ( which == CurveImp::numberOfProperties() + 5 )
203  return new StringImp( simplyCartesianEquationString( w ) );
204  else if ( which == CurveImp::numberOfProperties() + 6 )
205  return new StringImp( polarEquationString( w ) );
206  else assert( false );
207  return new InvalidImp;
208 }
209 
210 const Coordinate CircleImp::center() const
211 {
212  return mcenter;
213 }
214 
215 double CircleImp::radius() const
216 {
217  return mradius;
218 }
219 
220 double CircleImp::surface() const
221 {
222  return M_PI * squareRadius();
223 }
224 
225 double CircleImp::squareRadius() const
226 {
227  return mradius * mradius;
228 }
229 
230 double CircleImp::circumference() const
231 {
232  return 2 * M_PI * radius();
233 }
234 
235 QString CircleImp::polarEquationString( const KigDocument& w ) const
236 {
237  ConicPolarData data = polarData();
238  QString ret = ki18n( "rho = %1 [centered at %2]" )
239  .subs( data.pdimen, 0, 'g', 3 )
240  .subs( w.coordinateSystem().fromScreen( data.focus1, w ) )
241  .toString();
242  return ret;
243 }
244 
245 QString CircleImp::cartesianEquationString( const KigDocument& ) const
246 {
247 // QString ret = i18n( "x² + y² + %1 x + %2 y + %3 = 0" );
248  ConicCartesianData data = cartesianData();
249  EquationString ret = EquationString( "" );
250  bool needsign = false;
251  ret.addTerm( 1., ret.x2(), needsign );
252  ret.addTerm( 1., ret.y2(), needsign );
253  ret.addTerm( data.coeffs[3], ret.x(), needsign );
254  ret.addTerm( data.coeffs[4], ret.y(), needsign );
255  ret.addTerm( data.coeffs[5], "", needsign );
256  ret.append( " = 0" );
257 // ret = ret.arg( data.coeffs[3], 0, 'g', 3 );
258 // ret = ret.arg( data.coeffs[4], 0, 'g', 3 );
259 // ret = ret.arg( data.coeffs[5], 0, 'g', 3 );
260  return ret;
261 }
262 
263 QString CircleImp::simplyCartesianEquationString( const KigDocument& ) const
264 {
265  EquationString ret = EquationString( "( x" );
266  bool needsign = true;
267  ret.addTerm( -mcenter.x, "", needsign );
268  ret.append( QString::fromUtf8( " )² + ( y" ) );
269  ret.addTerm( -mcenter.y, "", needsign );
270  ret.append( QString::fromUtf8( " )² = " ) );
271  needsign = false;
272  ret.addTerm( mradius*mradius, "", needsign );
273  ret.prettify();
274 
275 // QString ret = i18n( "( x - %1 )² + ( y - %2 )² = %3" );
276 // ret = ret.arg( mcenter.x, 0, 'g', 3 );
277 // ret = ret.arg( mcenter.y, 0, 'g', 3 );
278 // ret = ret.arg( mradius * mradius, 0, 'g', 3 );
279  return ret;
280 }
281 
282 Coordinate CircleImp::focus1() const
283 {
284  return center();
285 }
286 
287 Coordinate CircleImp::focus2() const
288 {
289  return center();
290 }
291 
292 int CircleImp::conicType() const
293 {
294  return 1;
295 }
296 
297 const ConicCartesianData CircleImp::cartesianData() const
298 {
299  Coordinate c = center();
300  double sqr = squareRadius();
301  ConicCartesianData data(
302  1.0, 1.0, 0.0, -2*c.x, -2*c.y,
303  c.x*c.x + c.y*c.y - sqr );
304  return data;
305 }
306 
307 const ConicPolarData CircleImp::polarData() const
308 {
309  return ConicPolarData( center(), radius(), 0, 0 );
310 }
311 
312 CircleImp* CircleImp::copy() const
313 {
314  return new CircleImp( mcenter, mradius );
315 }
316 
317 double CircleImp::getParam( const Coordinate& point, const KigDocument& ) const
318 {
319  Coordinate tmp = point - mcenter;
320  double ret = atan2(tmp.y, tmp.x) / ( 2 * M_PI );
321  if ( ret > 0 ) return ret;
322  else return ret + 1;
323 }
324 
325 const Coordinate CircleImp::getPoint( double p, const KigDocument& ) const
326 {
327  return mcenter + Coordinate (cos(p * 2 * M_PI), sin(p * 2 * M_PI)) * mradius;
328 }
329 
330 void CircleImp::visit( ObjectImpVisitor* vtor ) const
331 {
332  vtor->visit( this );
333 }
334 
335 bool CircleImp::equals( const ObjectImp& rhs ) const
336 {
337  return rhs.inherits( CircleImp::stype() ) &&
338  static_cast<const CircleImp&>( rhs ).center() == center() &&
339  static_cast<const CircleImp&>( rhs ).radius() == radius();
340 }
341 
342 const ObjectImpType* CircleImp::stype()
343 {
344  static const ObjectImpType t(
345  Parent::stype(), "circle",
346  I18N_NOOP( "circle" ),
347  I18N_NOOP( "Select this circle" ),
348  I18N_NOOP( "Select circle %1" ),
349  I18N_NOOP( "Remove a Circle" ),
350  I18N_NOOP( "Add a Circle" ),
351  I18N_NOOP( "Move a Circle" ),
352  I18N_NOOP( "Attach to this circle" ),
353  I18N_NOOP( "Show a Circle" ),
354  I18N_NOOP( "Hide a Circle" )
355  );
356  return &t;
357 }
358 
359 const ObjectImpType* CircleImp::type() const
360 {
361  return CircleImp::stype();
362 }
363 
364 bool CircleImp::isPropertyDefinedOnOrThroughThisImp( int which ) const
365 {
366  assert( which < CircleImp::numberOfProperties() );
367  if ( which < CurveImp::numberOfProperties() )
368  return CurveImp::isPropertyDefinedOnOrThroughThisImp( which );
369  return false;
370 }
371 
372 Rect CircleImp::surroundingRect() const
373 {
374  Coordinate d( mradius, mradius );
375  return Rect( mcenter - d, mcenter + d );
376 }
CircleImp::isPropertyDefinedOnOrThroughThisImp
bool isPropertyDefinedOnOrThroughThisImp(int which) const
Definition: circle_imp.cc:364
EquationString::y
const QString y() const
Definition: equation.cc:110
ObjectImpType
Instances of this class represent a certain ObjectImp type.
Definition: object_imp.h:95
ObjectImp::inherits
bool inherits(const ObjectImpType *t) const
Returns true if this ObjectImp inherits the ObjectImp type represented by t.
Definition: object_imp.cc:279
CircleImp::impRequirementForProperty
const ObjectImpType * impRequirementForProperty(int which) const
Definition: circle_imp.cc:157
CircleImp::simplyCartesianEquationString
QString simplyCartesianEquationString(const KigDocument &w) const
Return a string containing the cartesian equation of this circle.
Definition: circle_imp.cc:263
point_imp.h
KigPainter::drawCircle
void drawCircle(const Coordinate &center, double radius)
draw a circle...
Definition: kigpainter.cpp:84
CircleImp::inRect
bool inRect(const Rect &r, int width, const KigWidget &) const
Definition: circle_imp.cc:72
StringImp
This ObjectImp is a BogusImp containing only a string value.
Definition: bogus_imp.h:167
CircleImp::transform
ObjectImp * transform(const Transformation &) const
Return this ObjectImp, transformed by the transformation t.
Definition: circle_imp.cc:44
CircleImp::contains
bool contains(const Coordinate &p, int width, const KigWidget &) const
Definition: circle_imp.cc:67
ScreenInfo::normalMiss
double normalMiss(int width) const
Definition: screeninfo.cc:88
CircleImp::copy
CircleImp * copy() const
Returns a copy of this ObjectImp.
Definition: circle_imp.cc:312
CircleImp::CircleImp
CircleImp(const Coordinate &center, double radius)
Construct a Circle with a given center and radius.
Definition: circle_imp.cc:35
ObjectImpVisitor::visit
void visit(const ObjectImp *imp)
Definition: object_imp.cc:81
CircleImp
An ObjectImp representing a circle.
Definition: circle_imp.h:27
ObjectImp::impRequirementForProperty
virtual const ObjectImpType * impRequirementForProperty(int which) const
Definition: object_imp.cc:76
CircleImp::properties
const QByteArrayList properties() const
Definition: circle_imp.cc:143
Rect::bottomLeft
Coordinate bottomLeft() const
Definition: rect.cc:161
CircleImp::polarEquationString
QString polarEquationString(const KigDocument &w) const
Return a string containing the polar equation of this circle.
Definition: circle_imp.cc:235
ObjectImp::property
virtual ObjectImp * property(int which, const KigDocument &d) const
Definition: object_imp.cc:70
Rect
This file is part of Kig, a KDE program for Interactive Geometry...
Definition: rect.h:34
CircleImp::getPoint
const Coordinate getPoint(double param, const KigDocument &) const
Definition: circle_imp.cc:325
EquationString
Simple class that represents an equation.
Definition: equation.h:49
CircleImp::polarData
const ConicPolarData polarData() const
Return the polar representation of this conic.
Definition: circle_imp.cc:307
ConicCartesianData
Cartesian Conic Data.
Definition: conic-common.h:37
CircleImp::center
const Coordinate center() const
Return the center of this circle.
Definition: circle_imp.cc:210
CoordinateSystem::fromScreen
virtual QString fromScreen(const Coordinate &pt, const KigDocument &w) const =0
circle_imp.h
ObjectImp::propertiesInternalNames
virtual const QByteArrayList propertiesInternalNames() const
Definition: object_imp.cc:63
EquationString::x
const QString x() const
Definition: equation.cc:105
CircleImp::conicType
int conicType() const
Always returns 1, since a circle always is an ellipse.
Definition: circle_imp.cc:292
ConicCartesianData::coeffs
double coeffs[6]
Definition: conic-common.h:40
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
CircleImp::propertiesInternalNames
const QByteArrayList propertiesInternalNames() const
Definition: circle_imp.cc:129
CircleImp::property
ObjectImp * property(int which, const KigDocument &w) const
Definition: circle_imp.cc:187
Transformation::isHomothetic
bool isHomothetic() const
Returns whether this is a homothetic (affine) transformation.
Definition: kigtransform.cpp:681
CircleImp::getParam
double getParam(const Coordinate &point, const KigDocument &) const
Definition: circle_imp.cc:317
CircleImp::surface
double surface() const
Return the surface of this circle.
Definition: circle_imp.cc:220
CircleImp::visit
void visit(ObjectImpVisitor *vtor) const
Definition: circle_imp.cc:330
EquationString::addTerm
void addTerm(double coeff, const QString &unknowns, bool &needsign)
Definition: equation.cc:41
KigWidget::screenInfo
const ScreenInfo & screenInfo() const
the part of the document we're currently showing i.e.
Definition: kig_view.cpp:272
ObjectImp::iconForProperty
virtual const char * iconForProperty(int which) const
Definition: object_imp.cc:187
Rect::contains
bool contains(const Coordinate &p) const
Definition: rect.cc:222
KigPainter
KigPainter is an extended QPainter.
Definition: kigpainter.h:51
bogus_imp.h
CircleImp::equals
bool equals(const ObjectImp &rhs) const
Returns true if this ObjectImp is equal to rhs.
Definition: circle_imp.cc:335
EquationString::y2
const QString y2() const
Definition: equation.cc:95
CircleImp::cartesianData
const ConicCartesianData cartesianData() const
Return the cartesian representation of this conic.
Definition: circle_imp.cc:297
PointImp
An ObjectImp representing a point.
Definition: point_imp.h:27
CircleImp::cartesianEquationString
QString cartesianEquationString(const KigDocument &w) const
Return a string containing the cartesian equation of this circle.
Definition: circle_imp.cc:245
Rect::topLeft
Coordinate topLeft() const
Definition: rect.cc:171
CircleImp::valid
bool valid() const
Definition: circle_imp.cc:118
EquationString::x2
const QString x2() const
Definition: equation.cc:90
Transformation
Class representing a transformation.
Definition: kigtransform.h:37
ConicPolarData::focus1
Coordinate focus1
The first focus of this conic.
Definition: conic-common.h:106
CircleImp::type
const ObjectImpType * type() const
Returns the lowermost ObjectImpType that this object is an instantiation of.
Definition: circle_imp.cc:359
CircleImp::surroundingRect
Rect surroundingRect() const
Definition: circle_imp.cc:372
ConicPolarData::pdimen
double pdimen
The pdimen value from the polar equation.
Definition: conic-common.h:110
KigWidget
This class is the real widget showing the document.
Definition: kig_view.h:50
ObjectImp::isPropertyDefinedOnOrThroughThisImp
virtual bool isPropertyDefinedOnOrThroughThisImp(int which) const
Definition: object_imp.cc:326
CircleImp::iconForProperty
const char * iconForProperty(int which) const
Definition: circle_imp.cc:164
CircleImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the CircleImp type.
Definition: circle_imp.cc:342
Rect::topRight
Coordinate topRight() const
Definition: rect.cc:176
Transformation::apply
const Coordinate apply(const double x0, const double x1, const double x2) const
Apply this Tranformation.
Definition: kigtransform.cpp:611
CircleImp::radius
double radius() const
Return the radius of this circle.
Definition: circle_imp.cc:215
CircleImp::focus1
Coordinate focus1() const
The first focus of a circle is simply its center.
Definition: circle_imp.cc:282
ConicImp::transform
ObjectImp * transform(const Transformation &) const
Return this ObjectImp, transformed by the transformation t.
Definition: conic_imp.cc:34
ConicImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the ConicImp type.
Definition: conic_imp.cc:380
DoubleImp
This ObjectImp is a BogusImp containing only a double value.
Definition: bogus_imp.h:89
KigDocument
KigDocument is the class holding the real data in a Kig document.
Definition: kig_document.h:36
Coordinate::x
double x
X Component.
Definition: coordinate.h:126
QByteArrayList
QList< QByteArray > QByteArrayList
Definition: objects/common.h:50
Coordinate::y
double y
Y Component.
Definition: coordinate.h:129
CircleImp::~CircleImp
~CircleImp()
Definition: circle_imp.cc:40
ObjectImp
The ObjectImp class represents the behaviour of an object after it is calculated. ...
Definition: object_imp.h:226
CircleImp::draw
void draw(KigPainter &p) const
Definition: circle_imp.cc:62
Rect::bottomRight
Coordinate bottomRight() const
Definition: rect.cc:166
CircleImp::focus2
Coordinate focus2() const
The second focus of a circle is simply its center.
Definition: circle_imp.cc:287
KigDocument::coordinateSystem
const CoordinateSystem & coordinateSystem() const
Definition: kig_document.cc:40
CircleImp::squareRadius
double squareRadius() const
Return the square radius of this circle.
Definition: circle_imp.cc:225
ObjectImp::numberOfProperties
virtual int numberOfProperties() const
Definition: object_imp.cc:58
EquationString::prettify
void prettify(void)
Definition: equation.cc:117
Coordinate::valid
bool valid() const
Return whether this is a valid Coordinate.
Definition: coordinate.cpp:176
CircleImp::circumference
double circumference() const
Return the circumference of this circle.
Definition: circle_imp.cc:230
ObjectImpVisitor
Definition: object_imp.h:56
ObjectImp::properties
virtual const QByteArrayList properties() const
Definition: object_imp.cc:51
InvalidImp
This ObjectImp represents an invalid object.
Definition: bogus_imp.h:61
CircleImp::numberOfProperties
int numberOfProperties() const
Definition: circle_imp.cc:123
ConicPolarData
This class represents an equation of a conic in the form .
Definition: conic-common.h:85
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kig

Skip menu "kig"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal