• 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
tangent_type.cc
Go to the documentation of this file.
1 // Copyright (C) 2004 Pino Toscano <toscano.pino@tiscali.it>
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 "tangent_type.h"
19 
20 #include "bogus_imp.h"
21 #include "conic_imp.h"
22 #include "cubic_imp.h"
23 #include "curve_imp.h"
24 #include "other_imp.h"
25 #include "point_imp.h"
26 #include "line_imp.h"
27 
28 #include "../misc/common.h"
29 #include "../misc/conic-common.h"
30 //#include "../misc/calcpaths.h"
31 #include "../kig/kig_part.h"
32 #include "../kig/kig_view.h"
33 
34 static const char constructlinetangentpoint[] = "SHOULDNOTBESEEN";
35 static const char selecttangent1[] =
36  I18N_NOOP( "Select the curve..." );
37 static const char selecttangent2[] =
38  I18N_NOOP( "Select the point for the tangent to go through..." );
39 
40 static const ArgsParser::spec argsspecTangentConic[] =
41 {
42  { ConicImp::stype(), "SHOULDNOTBESEEN", selecttangent1, false },
43  { PointImp::stype(), constructlinetangentpoint, selecttangent2, true }
44 };
45 
46 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( TangentConicType )
47 
48 TangentConicType::TangentConicType()
49  : ArgsParserObjectType( "TangentConic", argsspecTangentConic, 2 )
50 {
51 }
52 
53 TangentConicType::~TangentConicType()
54 {
55 }
56 
57 const TangentConicType* TangentConicType::instance()
58 {
59  static const TangentConicType t;
60  return &t;
61 }
62 
63 ObjectImp* TangentConicType::calc( const Args& args, const KigDocument& doc ) const
64 {
65  if ( !margsparser.checkArgs( args ) )
66  return new InvalidImp;
67 
68  const ConicImp* c = static_cast<const ConicImp*>( args[0] );
69  const Coordinate& p = static_cast<const PointImp*>( args[1] )->coordinate();
70 
71  if ( !c->containsPoint( p, doc ) )
72  return new InvalidImp;
73 
74  bool ok;
75  const LineData tangent = calcConicPolarLine( c->cartesianData(), p, ok );
76 
77  if ( !ok )
78  return new InvalidImp;
79 
80  return new LineImp( tangent );
81 }
82 
83 const ObjectImpType* TangentConicType::resultId() const
84 {
85  return LineImp::stype();
86 }
87 
88 /*** Arc starts here ***/
89 
90 static const ArgsParser::spec argsspecTangentArc[] =
91 {
92  { ArcImp::stype(), "SHOULDNOTBESEEN", selecttangent1, false },
93  { PointImp::stype(), constructlinetangentpoint, selecttangent2, true }
94 };
95 
96 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( TangentArcType )
97 
98 TangentArcType::TangentArcType()
99  : ArgsParserObjectType( "TangentArc", argsspecTangentArc, 2 )
100 {
101 }
102 
103 TangentArcType::~TangentArcType()
104 {
105 }
106 
107 const TangentArcType* TangentArcType::instance()
108 {
109  static const TangentArcType t;
110  return &t;
111 }
112 
113 ObjectImp* TangentArcType::calc( const Args& args, const KigDocument& doc ) const
114 {
115  if ( !margsparser.checkArgs( args ) )
116  return new InvalidImp;
117 
118  const ArcImp* arc = static_cast<const ArcImp*>( args[0] );
119  const Coordinate& p = static_cast<const PointImp*>( args[1] )->coordinate();
120 
121  if ( !arc->containsPoint( p, doc ) )
122  return new InvalidImp;
123 
124  Coordinate c = arc->center();
125  double sqr = arc->radius();
126  sqr *= sqr;
127  ConicCartesianData data( 1.0, 1.0, 0.0, -2*c.x, -2*c.y, c.x*c.x + c.y*c.y - sqr );
128 
129  bool ok;
130  const LineData tangent = calcConicPolarLine( data, p, ok );
131 
132  if ( !ok )
133  return new InvalidImp;
134 
135  return new LineImp( tangent );
136 }
137 
138 const ObjectImpType* TangentArcType::resultId() const
139 {
140  return LineImp::stype();
141 }
142 
143 /**** Cubic starts here ****/
144 
145 static const ArgsParser::spec argsspecTangentCubic[] =
146 {
147  { CubicImp::stype(), "SHOULDNOTBESEEN", selecttangent1, false },
148  { PointImp::stype(), constructlinetangentpoint, selecttangent2, true }
149 };
150 
151 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( TangentCubicType )
152 
153 TangentCubicType::TangentCubicType()
154  : ArgsParserObjectType( "TangentCubic", argsspecTangentCubic, 2 )
155 {
156 }
157 
158 TangentCubicType::~TangentCubicType()
159 {
160 }
161 
162 const TangentCubicType* TangentCubicType::instance()
163 {
164  static const TangentCubicType t;
165  return &t;
166 }
167 
168 ObjectImp* TangentCubicType::calc( const Args& args, const KigDocument& doc ) const
169 {
170  if ( !margsparser.checkArgs( args ) )
171  return new InvalidImp;
172 
173  const CubicImp* cubic = static_cast<const CubicImp*>( args[0] );
174  const Coordinate& p = static_cast<const PointImp*>( args[1] )->coordinate();
175 
176  if ( !cubic->containsPoint( p, doc ) )
177  return new InvalidImp;
178 
179  double x = p.x;
180  double y = p.y;
181  CubicCartesianData data = cubic->data();
182 // double aconst = data.coeffs[0];
183  double ax = data.coeffs[1];
184  double ay = data.coeffs[2];
185  double axx = data.coeffs[3];
186  double axy = data.coeffs[4];
187  double ayy = data.coeffs[5];
188  double axxx = data.coeffs[6];
189  double axxy = data.coeffs[7];
190  double axyy = data.coeffs[8];
191  double ayyy = data.coeffs[9];
192 
193  /* mp: the tangent vector (-gy,gx) is orthogonal to the gradient (gx,gy)
194  * which is easy to compute from the CartesianData
195  *
196  * note: same thing could be done for conics, which would be
197  * much more efficient...
198  */
199 
200  Coordinate tangvec = Coordinate (
201  - axxy*x*x - 2*axyy*x*y - 3*ayyy*y*y - axy*x - 2*ayy*y - ay,
202  3*axxx*x*x + 2*axxy*x*y + axyy*y*y + 2*axx*x + axy*y + ax
203  );
204  const LineData tangent = LineData( p, p + tangvec );
205 
206  return new LineImp( tangent );
207 }
208 
209 const ObjectImpType* TangentCubicType::resultId() const
210 {
211  return LineImp::stype();
212 }
213 
214 /**** Curve (locus) starts here ****/
215 
216 static const ArgsParser::spec argsspecTangentCurve[] =
217 {
218  { CurveImp::stype(), "SHOULDNOTBESEEN", selecttangent1, false },
219  { PointImp::stype(), constructlinetangentpoint, selecttangent2, true }
220 };
221 
222 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( TangentCurveType )
223 
224 TangentCurveType::TangentCurveType()
225  : ArgsParserObjectType( "TangentCurve", argsspecTangentCurve, 2 )
226 {
227 }
228 
229 TangentCurveType::~TangentCurveType()
230 {
231 }
232 
233 const TangentCurveType* TangentCurveType::instance()
234 {
235  static const TangentCurveType t;
236  return &t;
237 }
238 
239 ObjectImp* TangentCurveType::calc( const Args& args, const KigDocument& doc ) const
240 {
241  if ( !margsparser.checkArgs( args ) )
242  return new InvalidImp;
243 
244  const CurveImp* curve = static_cast<const CurveImp*>( args[0] );
245  const Coordinate& p = static_cast<const PointImp*>( args[1] )->coordinate();
246  if ( !curve->containsPoint( p, doc ) )
247  return new InvalidImp;
248 
249  const double t = curve->getParam( p, doc );
250  const double tau0 = 1e-3;
251  const double sigma = 1e-5;
252  const int maxiter = 20;
253 
254  double tau = tau0;
255  Coordinate tang, err;
256  double tplus = t + tau;
257  double tminus = t - tau;
258  if ( tplus > 1 ) {tplus = 1; tminus = 1 - 2*tau;}
259  if ( tminus < 0 ) {tminus = 0; tplus = 2*tau;}
260  Coordinate tangold = (curve->getPoint( tplus, doc ) - curve->getPoint( tminus, doc ))/(2*tau);
261 
262  for (int i = 0; i < maxiter; i++)
263  {
264  tau = tau/2;
265  tplus = t + tau;
266  tminus = t - tau;
267  if ( tplus > 1 ) {tplus = 1; tminus = 1 - 2*tau;}
268  if ( tminus < 0 ) {tminus = 0; tplus = 2*tau;}
269  tang = (curve->getPoint( tplus, doc ) - curve->getPoint( tminus, doc ))/(2*tau);
270  err = (tangold - tang)/3;
271  if (err.length() < sigma)
272  {
273  tang = (4*tang - tangold)/3;
274  const LineData tangent = LineData( p, p + tang );
275  return new LineImp( tangent );
276  }
277  tangold = tang;
278  }
279  return new InvalidImp;
280 }
281 
282 const ObjectImpType* TangentCurveType::resultId() const
283 {
284  return LineImp::stype();
285 }
TangentCurveType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: tangent_type.cc:239
CubicImp
An ObjectImp representing a cubic.
Definition: cubic_imp.h:29
calcConicPolarLine
const LineData calcConicPolarLine(const ConicCartesianData &data, const Coordinate &cpole, bool &valid)
This function calculates the polar line of the point cpole with respect to the given conic data...
Definition: conic-common.cpp:293
ObjectImpType
Instances of this class represent a certain ObjectImp type.
Definition: object_imp.h:95
CubicCartesianData::coeffs
double coeffs[10]
Definition: cubic-common.h:34
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(SegmentAxisType)
TangentConicType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: tangent_type.cc:83
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
TangentConicType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: tangent_type.cc:63
LineData
Simple class representing a line.
Definition: misc/common.h:49
ConicCartesianData
Cartesian Conic Data.
Definition: conic-common.h:37
argsspecTangentArc
static const ArgsParser::spec argsspecTangentArc[]
Definition: tangent_type.cc:90
TangentArcType
the line tangent to an arc
Definition: tangent_type.h:41
TangentCurveType
the line tangent to a curve
Definition: tangent_type.h:71
LineImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the LineImp type.
Definition: line_imp.cc:528
ArcImp::radius
double radius() const
Return the radius of this arc.
Definition: other_imp.cc:537
argsspecTangentCubic
static const ArgsParser::spec argsspecTangentCubic[]
Definition: tangent_type.cc:145
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
ConicImp::containsPoint
bool containsPoint(const Coordinate &p, const KigDocument &doc) const
Return whether this Curve contains the given point.
Definition: conic_imp.cc:402
TangentCurveType::instance
static const TangentCurveType * instance()
Definition: tangent_type.cc:233
Coordinate::length
double length() const
Length.
Definition: coordinate.cpp:144
CurveImp::getPoint
virtual const Coordinate getPoint(double param, const KigDocument &) const =0
ArgsParserObjectType::margsparser
const ArgsParser margsparser
Definition: object_type.h:117
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
TangentCubicType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: tangent_type.cc:209
ArgsParser::checkArgs
bool checkArgs(const std::vector< ObjectCalcer * > &os) const
Definition: argsparser.cpp:222
CubicCartesianData
This class represents an equation of a cubic in the form (in homogeneous coordinates, ), .
Definition: cubic-common.h:31
ArgsParser::spec
Definition: argsparser.h:113
ArcImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the ArcImp type.
Definition: other_imp.cc:629
ConicImp
An ObjectImp representing a conic.
Definition: conic_imp.h:39
TangentArcType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: tangent_type.cc:138
ArcImp::center
const Coordinate center() const
Return the center of this arc.
Definition: other_imp.cc:532
TangentCubicType::instance
static const TangentCubicType * instance()
Definition: tangent_type.cc:162
TangentArcType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: tangent_type.cc:113
TangentCubicType
the line tangent to a cubic
Definition: tangent_type.h:56
TangentCubicType::calc
ObjectImp * calc(const Args &args, const KigDocument &) const
Definition: tangent_type.cc:168
argsspecTangentConic
static const ArgsParser::spec argsspecTangentConic[]
Definition: tangent_type.cc:40
PointImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing PointImp's.
Definition: point_imp.cc:159
CubicImp::data
const CubicCartesianData data() const
Return the cartesian representation of this cubic.
Definition: cubic_imp.cc:331
line_imp.h
ConicImp::cartesianData
virtual const ConicCartesianData cartesianData() const
Return the cartesian representation of this conic.
Definition: conic_imp.cc:285
CurveImp::containsPoint
virtual bool containsPoint(const Coordinate &p, const KigDocument &) const =0
Return whether this Curve contains the given point.
selecttangent2
static const char selecttangent2[]
Definition: tangent_type.cc:37
CurveImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the CurveImp type.
Definition: curve_imp.cc:27
ConicImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the ConicImp type.
Definition: conic_imp.cc:380
cubic_imp.h
KigDocument
KigDocument is the class holding the real data in a Kig document.
Definition: kig_document.h:36
LineImp
An ObjectImp representing a line.
Definition: line_imp.h:184
constructlinetangentpoint
static const char constructlinetangentpoint[]
Definition: tangent_type.cc:34
ArcImp
An ObjectImp representing an arc.
Definition: other_imp.h:169
Coordinate::x
double x
X Component.
Definition: coordinate.h:126
tangent_type.h
ArcImp::containsPoint
bool containsPoint(const Coordinate &p, const KigDocument &doc) const
Return whether this Curve contains the given point.
Definition: other_imp.cc:661
CubicImp::containsPoint
bool containsPoint(const Coordinate &p, const KigDocument &doc) const
Return whether this Curve contains the given point.
Definition: cubic_imp.cc:369
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
argsspecTangentCurve
static const ArgsParser::spec argsspecTangentCurve[]
Definition: tangent_type.cc:216
TangentConicType::instance
static const TangentConicType * instance()
Definition: tangent_type.cc:57
TangentConicType
the line tangent to a generic conic
Definition: tangent_type.h:26
curve_imp.h
CubicImp::stype
static const ObjectImpType * stype()
Definition: cubic_imp.cc:352
TangentArcType::instance
static const TangentArcType * instance()
Definition: tangent_type.cc:107
conic_imp.h
CurveImp::getParam
virtual double getParam(const Coordinate &point, const KigDocument &) const
Definition: curve_imp.cc:149
selecttangent1
static const char selecttangent1[]
Definition: tangent_type.cc:35
InvalidImp
This ObjectImp represents an invalid object.
Definition: bogus_imp.h:61
TangentCurveType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: tangent_type.cc:282
other_imp.h
CurveImp
This class represents a curve: something which is composed of points, like a line, a circle, a locus.
Definition: curve_imp.h:27
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