• 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
circle_type.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_type.h"
19 
20 #include <math.h>
21 
22 #include "circle_imp.h"
23 #include "bogus_imp.h"
24 #include "line_imp.h"
25 #include "point_imp.h"
26 #include "special_imptypes.h"
27 
28 #include "../misc/common.h"
29 
30 #include <klocale.h>
31 
32 static const char constructcirclethroughpointstat[] = I18N_NOOP( "Construct a circle through this point" );
33 
34 static const char constructcirclewithcenterstat[] = I18N_NOOP( "Construct a circle with this center" );
35 
36 static const ArgsParser::spec argsspecCircleBCP[] =
37 {
38  { PointImp::stype(), constructcirclewithcenterstat,
39  I18N_NOOP( "Select the center of the new circle..." ), false },
40  { PointImp::stype(), constructcirclethroughpointstat,
41  I18N_NOOP( "Select a point for the new circle to go through..." ), true }
42 };
43 
44 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( CircleBCPType )
45 
46 CircleBCPType::CircleBCPType()
47  : ObjectABType( "CircleBCP", argsspecCircleBCP, 2 )
48 {
49 }
50 
51 CircleBCPType::~CircleBCPType()
52 {
53 }
54 
55 const CircleBCPType* CircleBCPType::instance()
56 {
57  static const CircleBCPType s;
58  return &s;
59 }
60 
61 ObjectImp* CircleBCPType::calcx( const Coordinate& a, const Coordinate& b ) const
62 {
63  return new CircleImp( a, ( b - a ).length() );
64 }
65 
66 const CircleBTPType* CircleBTPType::instance()
67 {
68  static const CircleBTPType t;
69  return &t;
70 }
71 
72 static const ArgsParser::spec argsspecCircleBTP[] =
73 {
74  { PointImp::stype(), constructcirclethroughpointstat,
75  I18N_NOOP( "Select a point for the new circle to go through..." ), true },
76  { PointImp::stype(), constructcirclethroughpointstat,
77  I18N_NOOP( "Select a point for the new circle to go through..." ), true },
78  { PointImp::stype(), constructcirclethroughpointstat,
79  I18N_NOOP( "Select a point for the new circle to go through..." ), true }
80 };
81 
82 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( CircleBTPType )
83 
84 CircleBTPType::CircleBTPType()
85  : ArgsParserObjectType( "CircleBTP", argsspecCircleBTP, 3 )
86 {
87 }
88 
89 CircleBTPType::~CircleBTPType()
90 {
91 }
92 
93 ObjectImp* CircleBTPType::calc( const Args& args, const KigDocument& ) const
94 {
95  if ( ! margsparser.checkArgs( args, 2 ) ) return new InvalidImp;
96 
97  const Coordinate a = static_cast<const PointImp*>( args[0] )->coordinate();
98  const Coordinate b = static_cast<const PointImp*>( args[1] )->coordinate();
99  Coordinate c;
100  if ( args.size() == 3 )
101  c = static_cast<const PointImp*>( args[2] )->coordinate();
102  else
103  {
104  // we pick the third point so that the three points form a
105  // triangle with equal sides...
106 
107  // midpoint:
108  Coordinate m = ( b + a ) / 2;
109  if ( b.y != a.y )
110  {
111  // direction of the perpend:
112  double d = -(b.x-a.x)/(b.y-a.y);
113 
114  // length:
115  // sqrt( 3 ) == tan( 60° ) == sqrt( 2^2 - 1^2 )
116  double l = 1.73205080756 * (a-b).length() / 2;
117 
118  double d2 = d*d;
119  double l2 = l*l;
120  double dx = sqrt( l2 / ( d2 + 1 ) );
121  double dy = sqrt( l2 * d2 / ( d2 + 1 ) );
122  if( d < 0 ) dy = -dy;
123 
124  c.x = m.x + dx;
125  c.y = m.y + dy;
126  }
127  else
128  {
129  c.x = m.x;
130  c.y = m.y + ( a.x - b.x );
131  };
132  };
133 
134  const Coordinate center = calcCenter( a, b, c );
135  if ( center.valid() )
136  return new CircleImp( center, (center - a ).length() );
137 
138  /*
139  * case of collinear points, we need to identify the intermediate one
140  */
141 
142  double xmin = fmin( a.x, fmin( b.x, c.x) );
143  double xmax = fmax( a.x, fmax( b.x, c.x) );
144  double ymin = fmin( a.y, fmin( b.y, c.y) );
145  double ymax = fmax( a.y, fmax( b.y, c.y) );
146  double axy, bxy, cxy;
147 
148  /* decide whether to work with x coordinate or y coordinate */
149 
150  if ( xmax - xmin > ymax - ymin )
151  {
152  axy = a.x;
153  bxy = b.x;
154  cxy = c.x;
155  } else
156  {
157  axy = a.y;
158  bxy = b.y;
159  cxy = c.y;
160  }
161 
162  /*
163  * compute baricentric coordinate of c with respect to a and b
164  * (if a and c are not coincident)
165  */
166  if ( fabs( cxy - axy ) < 1e-6*fabs( bxy - axy ) ) return new InvalidImp;
167  double t = (bxy - axy)/(cxy - axy);
168 
169  if ( fabs( t ) < 1e-6 || fabs( 1.0 - t ) < 1e-6 ) return new InvalidImp;
170 
171  /*
172  * t < 0: a between c and b
173  * 0 < t < 1: b between a and c
174  * t > 1: c between a and b
175  */
176  if ( t < 0.0 ) return new LineImp( c, b );
177  if ( t > 1.0 ) return new LineImp( a, b );
178  return new LineImp( a, c );
179 }
180 
181 const ObjectImpType* CircleBCPType::resultId() const
182 {
183  return CircleImp::stype();
184 }
185 
186 const ObjectImpType* CircleBTPType::resultId() const
187 {
188  return CircleImp::stype();
189 }
190 
191 static const ArgsParser::spec argsspecCircleBPR[] =
192 {
193  { PointImp::stype(), constructcirclewithcenterstat,
194  I18N_NOOP( "Select the center of the new circle..." ), false },
195  { &lengthimptypeinstance, I18N_NOOP( "With this radius" ),
196  I18N_NOOP( "Select the length of the radius..." ), false }
197 };
198 
199 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( CircleBPRType )
200 
201 CircleBPRType::CircleBPRType()
202  : ArgsParserObjectType( "CircleBPR", argsspecCircleBPR, 2 )
203 {
204 }
205 
206 CircleBPRType::~CircleBPRType()
207 {
208 }
209 
210 const CircleBPRType* CircleBPRType::instance()
211 {
212  static const CircleBPRType t;
213  return &t;
214 }
215 
216 ObjectImp* CircleBPRType::calc( const Args& args, const KigDocument& ) const
217 {
218  if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;
219  const Coordinate c = static_cast<const PointImp*>( args[0] )->coordinate();
220  bool valid;
221  double r = getDoubleFromImp( args[1], valid);
222  if ( ! valid ) return new InvalidImp;
223  r = fabs( r );
224  // double r = static_cast<const DoubleImp*>( args[1] )->data();
225  return new CircleImp( c, r );
226 }
227 
228 const ObjectImpType* CircleBPRType::resultId() const
229 {
230  return CircleImp::stype();
231 }
ObjectImpType
Instances of this class represent a certain ObjectImp type.
Definition: object_imp.h:95
circle_type.h
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(SegmentAxisType)
ObjectABType
Definition: base_type.h:27
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
CircleBCPType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: circle_type.cc:181
CircleBTPType
Circle by three points.
Definition: circle_type.h:56
CircleImp
An ObjectImp representing a circle.
Definition: circle_imp.h:27
CircleBPRType
Circle by point and radius.
Definition: circle_type.h:41
CircleBPRType::instance
static const CircleBPRType * instance()
Definition: circle_type.cc:210
special_imptypes.h
circle_imp.h
lengthimptypeinstance
LengthImpType lengthimptypeinstance
argsspecCircleBTP
static const ArgsParser::spec argsspecCircleBTP[]
Definition: circle_type.cc:72
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
Coordinate::length
double length() const
Length.
Definition: coordinate.cpp:144
ArgsParserObjectType::margsparser
const ArgsParser margsparser
Definition: object_type.h:117
constructcirclethroughpointstat
static const char constructcirclethroughpointstat[]
Definition: circle_type.cc:32
constructcirclewithcenterstat
static const char constructcirclewithcenterstat[]
Definition: circle_type.cc:34
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
CircleBTPType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: circle_type.cc:93
ArgsParser::checkArgs
bool checkArgs(const std::vector< ObjectCalcer * > &os) const
Definition: argsparser.cpp:222
CircleBPRType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: circle_type.cc:216
ArgsParser::spec
Definition: argsparser.h:113
CircleBCPType::instance
static const CircleBCPType * instance()
Definition: circle_type.cc:55
CircleImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the CircleImp type.
Definition: circle_imp.cc:342
CircleBTPType::instance
static const CircleBTPType * instance()
Definition: circle_type.cc:66
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
CircleBCPType::calcx
ObjectImp * calcx(const Coordinate &a, const Coordinate &b) const
Definition: circle_type.cc:61
PointImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing PointImp's.
Definition: point_imp.cc:159
line_imp.h
KigDocument
KigDocument is the class holding the real data in a Kig document.
Definition: kig_document.h:36
argsspecCircleBCP
static const ArgsParser::spec argsspecCircleBCP[]
Definition: circle_type.cc:36
LineImp
An ObjectImp representing a line.
Definition: line_imp.h:184
Coordinate::x
double x
X Component.
Definition: coordinate.h:126
getDoubleFromImp
double getDoubleFromImp(const ObjectImp *obj, bool &valid)
Definition: special_imptypes.cc:27
CircleBTPType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: circle_type.cc:186
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
CircleBPRType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: circle_type.cc:228
argsspecCircleBPR
static const ArgsParser::spec argsspecCircleBPR[]
Definition: circle_type.cc:191
CircleBCPType
Circle by center and point.
Definition: circle_type.h:26
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
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