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

kig

  • sources
  • kde-4.14
  • kdeedu
  • kig
  • objects
arc_type.cc
Go to the documentation of this file.
1 // Copyright (C) 2003-2004 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 "arc_type.h"
19 
20 #include "bogus_imp.h"
21 #include "other_imp.h"
22 #include "point_imp.h"
23 #include "conic_imp.h"
24 #include "line_imp.h"
25 #include "locus_imp.h"
26 
27 #include "../misc/common.h"
28 #include "../misc/calcpaths.h"
29 #include "../misc/goniometry.h"
30 #include "../kig/kig_part.h"
31 #include "../kig/kig_view.h"
32 #include "../kig/kig_commands.h"
33 
34 #include <functional>
35 #include <algorithm>
36 #include <cmath>
37 #include <math.h>
38 
39 using std::find;
40 
41 #include <qstringlist.h>
42 
43 /*
44  * arc by three points
45  */
46 
47 static const char constructarcstartingstat[] = I18N_NOOP( "Construct an arc starting at this point" );
48 
49 static const ArgsParser::spec argsspecArcBTP[] =
50 {
51  { PointImp::stype(), constructarcstartingstat,
52  I18N_NOOP( "Select the start point of the new arc..." ), true },
53  { PointImp::stype(), I18N_NOOP( "Construct an arc through this point" ),
54  I18N_NOOP( "Select a point for the new arc to go through..." ), true },
55  { PointImp::stype(), I18N_NOOP( "Construct an arc ending at this point" ),
56  I18N_NOOP( "Select the end point of the new arc..." ), true }
57 };
58 
59 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( ArcBTPType )
60 
61 ArcBTPType::ArcBTPType()
62  : ArgsParserObjectType( "ArcBTP", argsspecArcBTP, 3 )
63 {
64 }
65 
66 ArcBTPType::~ArcBTPType()
67 {
68 }
69 
70 const ArcBTPType* ArcBTPType::instance()
71 {
72  static const ArcBTPType t;
73  return &t;
74 }
75 
76 ObjectImp* ArcBTPType::calc( const Args& args, const KigDocument& ) const
77 {
78  if ( ! margsparser.checkArgs( args, 2 ) )
79  return new InvalidImp;
80 
81  const Coordinate a =
82  static_cast<const PointImp*>( args[0] )->coordinate();
83  const Coordinate b =
84  static_cast<const PointImp*>( args[1] )->coordinate();
85  Coordinate center;
86  double angle = 0.;
87  double startangle = 0.;
88  if ( args.size() == 3 )
89  {
90  Coordinate c = static_cast<const PointImp*>( args[2] )->coordinate();
91  center = calcCenter( a, b, c );
92  if ( ! center.valid() )
93  {
94  if ( fabs( a.x - c.x ) > fabs( a.y - c.y ) )
95  {
96  if ( ( b.x - a.x)*(c.x - b.x) > 1e-12 ) return new SegmentImp(a, c);
97  } else
98  {
99  if ( ( b.y - a.y)*(c.y - b.y) > 1e-12 ) return new SegmentImp(a, c);
100  }
101  return new InvalidImp;
102  }
103  Coordinate ad = a - center;
104  Coordinate bd = b - center;
105  Coordinate cd = c - center;
106  double anglea = atan2( ad.y, ad.x );
107  double angleb = atan2( bd.y, bd.x );
108  double anglec = atan2( cd.y, cd.x );
109 
110  // anglea should be smaller than anglec
111  if ( anglea > anglec )
112  {
113  double t = anglea;
114  anglea = anglec;
115  anglec = t;
116  };
117  if ( angleb > anglec || angleb < anglea )
118  {
119  startangle = anglec;
120  angle = 2 * M_PI + anglea - startangle;
121  }
122  else
123  {
124  startangle = anglea;
125  angle = anglec - anglea;
126  };
127  }
128  else
129  {
130  // find a center and angles that look natural..
131  center = (b+a)/2 + .6*(b-a).orthogonal();
132  Coordinate bd = b - center;
133  Coordinate ad = a - center;
134  startangle = atan2( ad.y, ad.x );
135  double halfangle = atan2( bd.y, bd.x ) - startangle;
136  if ( halfangle < - M_PI ) halfangle += 2*M_PI;
137  angle = 2 * halfangle;
138  };
139 
140  double radius = ( a - center ).length();
141  return new ArcImp( center, radius, startangle, angle );
142 }
143 
144 const ObjectImpType* ArcBTPType::impRequirement( const ObjectImp*, const Args& ) const
145 {
146  return PointImp::stype();
147 }
148 
149 bool ArcBTPType::inherits( int type ) const
150 {
151  return Parent::inherits( type );
152 }
153 
154 const ObjectImpType* ArcBTPType::resultId() const
155 {
156  return ArcImp::stype();
157 }
158 
159 /*
160  * arc by center, starting point and angle
161  */
162 
163 static const ArgsParser::spec argsspecArcBCPA[] =
164 {
165  { PointImp::stype(), I18N_NOOP( "Construct an arc with this center" ),
166  I18N_NOOP( "Select the center of the new arc..." ), true },
167  { PointImp::stype(), constructarcstartingstat,
168  I18N_NOOP( "Select the start point of the new arc..." ), true },
169  { AngleImp::stype(), I18N_NOOP( "Construct an arc with this angle" ),
170  I18N_NOOP( "Select the angle of the new arc..." ), true }
171 };
172 
173 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( ArcBCPAType )
174 
175 ArcBCPAType::ArcBCPAType()
176  : ArgsParserObjectType( "ArcBCPA", argsspecArcBCPA, 3 )
177 {
178 }
179 
180 ArcBCPAType::~ArcBCPAType()
181 {
182 }
183 
184 const ArcBCPAType* ArcBCPAType::instance()
185 {
186  static const ArcBCPAType t;
187  return &t;
188 }
189 
190 ObjectImp* ArcBCPAType::calc( const Args& args, const KigDocument& ) const
191 {
192  if ( ! margsparser.checkArgs( args ) )
193  return new InvalidImp;
194 
195  const Coordinate center = static_cast<const PointImp*>( args[0] )->coordinate();
196  const Coordinate p = static_cast<const PointImp*>( args[1] )->coordinate();
197  const AngleImp* a = static_cast<const AngleImp*>( args[2] );
198  const double angle = a->angle();
199  const Coordinate dir = p - center;
200  const double startangle = atan2( dir.y, dir.x );
201  const double radius = center.distance( p );
202 
203  return new ArcImp( center, radius, startangle, angle );
204 }
205 
206 const ObjectImpType* ArcBCPAType::impRequirement( const ObjectImp*, const Args& ) const
207 {
208  return PointImp::stype();
209 }
210 
211 bool ArcBCPAType::inherits( int type ) const
212 {
213  return Parent::inherits( type );
214 }
215 
216 const ObjectImpType* ArcBCPAType::resultId() const
217 {
218  return ArcImp::stype();
219 }
220 
221 /*
222  * arc of conic by three points and center
223  */
224 
225 static const char constructconicarcstartingstat[] = I18N_NOOP( "Construct a conic arc starting at this point" );
226 static const char selectconicarcstartingstat[] = I18N_NOOP( "Select the start point of the new conic arc..." );
227 static const char constructconicarcthrustat[] = I18N_NOOP( "Construct a conic arc through this point" );
228 static const char selectconicarcthrustat[] = I18N_NOOP( "Select a point for the new conic arc to go through..." );
229 static const char constructconicarcendingstat[] = I18N_NOOP( "Construct a conic arc ending at this point" );
230 static const char selectconicarcendingstat[] = I18N_NOOP( "Select the end point of the new conic arc..." );
231 
232 static const ArgsParser::spec argsspecConicArcBCTP[] =
233 {
234  { PointImp::stype(), I18N_NOOP( "Construct an conic arc with this center" ),
235  I18N_NOOP( "Select the center of the new conic arc..." ), false },
236  { PointImp::stype(), constructconicarcstartingstat,
237  selectconicarcstartingstat, true },
238  { PointImp::stype(), constructconicarcthrustat,
239  selectconicarcthrustat, true },
240  { PointImp::stype(), constructconicarcendingstat,
241  selectconicarcendingstat, true }
242 };
243 
244 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( ConicArcBCTPType )
245 
246 ConicArcBCTPType::ConicArcBCTPType()
247  : ArgsParserObjectType( "ConicArcBCTP", argsspecConicArcBCTP, 4 )
248 {
249 }
250 
251 ConicArcBCTPType::~ConicArcBCTPType()
252 {
253 }
254 
255 const ConicArcBCTPType* ConicArcBCTPType::instance()
256 {
257  static const ConicArcBCTPType t;
258  return &t;
259 }
260 
261 ObjectImp* ConicArcBCTPType::calc( const Args& args, const KigDocument& ) const
262 {
263  if ( ! margsparser.checkArgs( args, 2 ) )
264  return new InvalidImp;
265 
266  const Coordinate center =
267  static_cast<const PointImp*>( args[0] )->coordinate();
268  const Coordinate a =
269  static_cast<const PointImp*>( args[1] )->coordinate();
270  const Coordinate d = 2*center - a;
271  Coordinate b = center + (a-center).orthogonal();
272  Coordinate e = 2*center - b;
273  if ( args.size() >= 3 )
274  {
275  b = static_cast<const PointImp*>( args[2] )->coordinate();
276  e = 2*center - b;
277  }
278  bool have_c = false;
279  Coordinate c;
280  if ( args.size() == 4 )
281  {
282  c = static_cast<const PointImp*>( args[3] )->coordinate();
283  const Coordinate e = 2*center - c;
284  have_c = true;
285  }
286 
287  std::vector<Coordinate> points;
288  points.push_back( a );
289  points.push_back( b );
290  if (have_c) points.push_back( c );
291  points.push_back( d );
292  points.push_back( e );
293  ConicCartesianData cart =
294  calcConicThroughPoints( points, zerotilt, circleifzt, ysymmetry );
295  if ( ! d.valid() )
296  return new InvalidImp;
297 
298  ConicArcImp *me = new ConicArcImp( cart, 0.0, 2*M_PI );
299  double angle = 0.;
300  double startangle = 0.;
301  double anglea = 2*M_PI*me->getParam( a );
302  double angleb = anglea + M_PI/2;
303  angleb = 2*M_PI*me->getParam( b );
304  double anglec = 2*angleb - anglea;
305  if ( have_c ) anglec = 2*M_PI*me->getParam( c );
306 
307  // anglea should be smaller than anglec
308  if ( anglea > anglec )
309  {
310  double t = anglea;
311  anglea = anglec;
312  anglec = t;
313  };
314  if ( angleb > anglec || angleb < anglea )
315  {
316  startangle = anglec;
317  angle = 2 * M_PI + anglea - startangle;
318  }
319  else
320  {
321  startangle = anglea;
322  angle = anglec - anglea;
323  };
324 
325  me->setStartAngle( startangle );
326  me->setAngle( angle );
327  return me;
328 }
329 
330 const ObjectImpType* ConicArcBCTPType::resultId() const
331 {
332  return ConicArcImp::stype();
333 }
334 
335 /*
336  * arc of conic by five points
337  */
338 
339 static const ArgsParser::spec argsspecConicArcB5P[] =
340 {
341  { PointImp::stype(), constructconicarcstartingstat,
342  selectconicarcstartingstat, true },
343  { PointImp::stype(), constructconicarcthrustat,
344  selectconicarcthrustat, true },
345  { PointImp::stype(), constructconicarcthrustat,
346  selectconicarcthrustat, true },
347  { PointImp::stype(), constructconicarcthrustat,
348  selectconicarcthrustat, true },
349  { PointImp::stype(), constructconicarcendingstat,
350  selectconicarcendingstat, true }
351 };
352 
353 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( ConicArcB5PType )
354 
355 ConicArcB5PType::ConicArcB5PType()
356  : ArgsParserObjectType( "ConicArcB5P", argsspecConicArcB5P, 5 )
357 {
358 }
359 
360 ConicArcB5PType::~ConicArcB5PType()
361 {
362 }
363 
364 const ConicArcB5PType* ConicArcB5PType::instance()
365 {
366  static const ConicArcB5PType t;
367  return &t;
368 }
369 
370 ObjectImp* ConicArcB5PType::calc( const Args& args, const KigDocument& ) const
371 {
372  if ( ! margsparser.checkArgs( args, 2 ) )
373  return new InvalidImp;
374 
375  const Coordinate a =
376  static_cast<const PointImp*>( args[0] )->coordinate();
377  const Coordinate b =
378  static_cast<const PointImp*>( args[1] )->coordinate();
379 
380  Coordinate c, d, e;
381  bool have_c = false;
382  bool have_d = false;
383  bool have_e = false;
384  if ( args.size() >= 3 )
385  {
386  c = static_cast<const PointImp*>( args[2] )->coordinate();
387  have_c = true;
388  }
389  if ( args.size() >= 4 )
390  {
391  d = static_cast<const PointImp*>( args[3] )->coordinate();
392  have_d = true;
393  }
394  if ( args.size() >= 5 )
395  {
396  e = static_cast<const PointImp*>( args[4] )->coordinate();
397  have_e = true;
398  }
399 
400  std::vector<Coordinate> points;
401  points.push_back( a );
402  points.push_back( b );
403  if (have_c) points.push_back( c );
404  if (have_d) points.push_back( d );
405  if (have_e) points.push_back( e );
406  ConicCartesianData cart =
407  calcConicThroughPoints( points, zerotilt, circleifzt, ysymmetry );
408  if ( ! d.valid() )
409  return new InvalidImp;
410 
411  ConicArcImp *me = new ConicArcImp( cart, 0.0, 2*M_PI );
412  double angle = 0.;
413  double startangle = 0.;
414  double anglea = 2*M_PI*me->getParam( a );
415  double angleb = anglea + M_PI/2;
416  angleb = 2*M_PI*me->getParam( b );
417  if ( have_c ) angleb = 2*M_PI*me->getParam( c );
418  double anglec = 2*angleb - anglea;
419  if ( have_e ) anglec = 2*M_PI*me->getParam( e );
420 
421  // anglea should be smaller than anglec
422  if ( anglea > anglec )
423  {
424  double t = anglea;
425  anglea = anglec;
426  anglec = t;
427  };
428  if ( angleb > anglec || angleb < anglea )
429  {
430  startangle = anglec;
431  angle = 2 * M_PI + anglea - startangle;
432  }
433  else
434  {
435  startangle = anglea;
436  angle = anglec - anglea;
437  };
438 
439  me->setStartAngle( startangle );
440  me->setAngle( angle );
441  return me;
442 }
443 
444 const ObjectImpType* ConicArcB5PType::resultId() const
445 {
446  return ConicArcImp::stype();
447 }
448 
locus_imp.h
ObjectImpType
Instances of this class represent a certain ObjectImp type.
Definition: object_imp.h:95
ArcBTPType::instance
static const ArcBTPType * instance()
Definition: arc_type.cc:70
ConicArcB5PType::instance
static const ConicArcB5PType * instance()
Definition: arc_type.cc:364
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(SegmentAxisType)
point_imp.h
ArgsParserObjectType
This is a convenience subclass of ObjectType that a type should inherit from if its parents can be sp...
Definition: object_type.h:113
selectconicarcstartingstat
static const char selectconicarcstartingstat[]
Definition: arc_type.cc:226
ObjectType::inherits
virtual bool inherits(int type) const
Definition: object_type.cc:64
selectconicarcthrustat
static const char selectconicarcthrustat[]
Definition: arc_type.cc:228
ArcBTPType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: arc_type.cc:76
constructconicarcthrustat
static const char constructconicarcthrustat[]
Definition: arc_type.cc:227
ArcBTPType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: arc_type.cc:154
ConicCartesianData
Cartesian Conic Data.
Definition: conic-common.h:37
argsspecArcBTP
static const ArgsParser::spec argsspecArcBTP[]
Definition: arc_type.cc:49
ArcBCPAType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: arc_type.cc:190
calcConicThroughPoints
const ConicCartesianData calcConicThroughPoints(const std::vector< Coordinate > &points, const LinearConstraints c1, const LinearConstraints c2, const LinearConstraints c3, const LinearConstraints c4, const LinearConstraints c5)
Calculate a conic through a given set of points.
Definition: conic-common.cpp:169
ConicArcB5PType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: arc_type.cc:444
ConicArcBCTPType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: arc_type.cc:330
circleifzt
Definition: conic-common.h:138
ConicArcImp
A conic arc, which is given by the cartesian equation and two angles.
Definition: conic_imp.h:172
ArcBTPType
an arc by a start point, an intermediate point and an end point
Definition: arc_type.h:27
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
ConicArcBCTPType::instance
static const ConicArcBCTPType * instance()
Definition: arc_type.cc:255
ArgsParserObjectType::margsparser
const ArgsParser margsparser
Definition: object_type.h:117
ArcBCPAType::impRequirement
const ObjectImpType * impRequirement(const ObjectImp *o, const Args &parents) const
Supposing that parents would be given as parents to this type's calc function, this function returns ...
Definition: arc_type.cc:206
constructarcstartingstat
static const char constructarcstartingstat[]
Definition: arc_type.cc:47
ArcBTPType::impRequirement
const ObjectImpType * impRequirement(const ObjectImp *o, const Args &parents) const
Supposing that parents would be given as parents to this type's calc function, this function returns ...
Definition: arc_type.cc:144
arc_type.h
bogus_imp.h
PointImp
An ObjectImp representing a point.
Definition: point_imp.h:27
Args
std::vector< const ObjectImp * > Args
Definition: objects/common.h:47
ArcBCPAType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: arc_type.cc:216
argsspecArcBCPA
static const ArgsParser::spec argsspecArcBCPA[]
Definition: arc_type.cc:163
ConicArcImp::setAngle
void setAngle(double a)
Set the dimension in radians of this arc.
Definition: conic_imp.h:219
AngleImp::angle
double angle() const
Return the dimension in radians of this angle.
Definition: other_imp.h:87
ArgsParser::checkArgs
bool checkArgs(const std::vector< ObjectCalcer * > &os) const
Definition: argsparser.cpp:222
selectconicarcendingstat
static const char selectconicarcendingstat[]
Definition: arc_type.cc:230
ConicArcImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the ConicImp type.
Definition: conic_imp.cc:606
ArgsParser::spec
Definition: argsparser.h:113
ArcImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the ArcImp type.
Definition: other_imp.cc:629
ysymmetry
Definition: conic-common.h:139
ArcBCPAType
an arc by a point (center), a starting point and an angle
Definition: arc_type.h:47
argsspecConicArcB5P
static const ArgsParser::spec argsspecConicArcB5P[]
Definition: arc_type.cc:339
calcCenter
const Coordinate calcCenter(const Coordinate &a, const Coordinate &b, const Coordinate &c)
This function calculates the center of the circle going through the three given points.
Definition: common.cpp:366
ArcBCPAType::instance
static const ArcBCPAType * instance()
Definition: arc_type.cc:184
argsspecConicArcBCTP
static const ArgsParser::spec argsspecConicArcBCTP[]
Definition: arc_type.cc:232
ArcBCPAType::inherits
bool inherits(int type) const
Definition: arc_type.cc:211
PointImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing PointImp's.
Definition: point_imp.cc:159
line_imp.h
AngleImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the AngleImp type.
Definition: other_imp.cc:597
KigDocument
KigDocument is the class holding the real data in a Kig document.
Definition: kig_document.h:36
ConicArcB5PType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: arc_type.cc:370
constructconicarcendingstat
static const char constructconicarcendingstat[]
Definition: arc_type.cc:229
ArcImp
An ObjectImp representing an arc.
Definition: other_imp.h:169
Coordinate::x
double x
X Component.
Definition: coordinate.h:126
ConicArcImp::getParam
double getParam(const Coordinate &point, const KigDocument &) const
Definition: conic_imp.cc:646
zerotilt
Definition: conic-common.h:138
ConicArcB5PType
a conic arc by a five points, a starting point, intermediate, intermediate (used to compute angles)...
Definition: arc_type.h:87
Coordinate::orthogonal
const Coordinate orthogonal() const
Orthogonal.
Definition: coordinate.cpp:149
ArcBTPType::inherits
bool inherits(int type) const
Definition: arc_type.cc:149
Coordinate::y
double y
Y Component.
Definition: coordinate.h:129
ObjectImp
The ObjectImp class represents the behaviour of an object after it is calculated. ...
Definition: object_imp.h:226
ConicArcBCTPType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: arc_type.cc:261
ConicArcBCTPType
a conic arc by a start point, an intermediate point, an end point and the conic center ...
Definition: arc_type.h:68
conic_imp.h
ConicArcImp::setStartAngle
void setStartAngle(double sa)
Set the start angle in radians of this arc.
Definition: conic_imp.h:215
constructconicarcstartingstat
static const char constructconicarcstartingstat[]
Definition: arc_type.cc:225
Coordinate::valid
bool valid() const
Return whether this is a valid Coordinate.
Definition: coordinate.cpp:176
InvalidImp
This ObjectImp represents an invalid object.
Definition: bogus_imp.h:61
other_imp.h
AngleImp
An ObjectImp representing an angle.
Definition: other_imp.h:28
SegmentImp
An ObjectImp representing a segment.
Definition: line_imp.h:81
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:12:05 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
  • 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