moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kig / objects / circle_type.cc
blobd85fb8937fb03d83e2201c58dbaef027b455aadf
1 // Copyright (C) 2003 Dominique Devriese <devriese@kde.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
16 // 02111-1307, USA.
18 #include "circle_type.h"
20 #include "circle_imp.h"
21 #include "bogus_imp.h"
22 #include "line_imp.h"
23 #include "point_imp.h"
25 #include "../misc/common.h"
27 #include <klocale.h>
29 static const char constructcirclethroughpointstat[] = I18N_NOOP( "Construct a circle through this point" );
31 static const char constructcirclewithcenterstat[] = I18N_NOOP( "Construct a circle with this center" );
33 static const ArgsParser::spec argsspecCircleBCP[] =
35 { PointImp::stype(), constructcirclewithcenterstat,
36 I18N_NOOP( "Select the center of the new circle..." ), false },
37 { PointImp::stype(), constructcirclethroughpointstat,
38 I18N_NOOP( "Select a point for the new circle to go through..." ), true }
41 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( CircleBCPType )
43 CircleBCPType::CircleBCPType()
44 : ObjectABType( "CircleBCP", argsspecCircleBCP, 2 )
48 CircleBCPType::~CircleBCPType()
52 const CircleBCPType* CircleBCPType::instance()
54 static const CircleBCPType s;
55 return &s;
58 ObjectImp* CircleBCPType::calc( const Coordinate& a, const Coordinate& b ) const
60 return new CircleImp( a, ( b - a ).length() );
63 const CircleBTPType* CircleBTPType::instance()
65 static const CircleBTPType t;
66 return &t;
69 static const ArgsParser::spec argsspecCircleBTP[] =
71 { PointImp::stype(), constructcirclethroughpointstat,
72 I18N_NOOP( "Select a point for the new circle to go through..." ), true },
73 { PointImp::stype(), constructcirclethroughpointstat,
74 I18N_NOOP( "Select a point for the new circle to go through..." ), true },
75 { PointImp::stype(), constructcirclethroughpointstat,
76 I18N_NOOP( "Select a point for the new circle to go through..." ), true }
79 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( CircleBTPType )
81 CircleBTPType::CircleBTPType()
82 : ArgsParserObjectType( "CircleBTP", argsspecCircleBTP, 3 )
86 CircleBTPType::~CircleBTPType()
90 ObjectImp* CircleBTPType::calc( const Args& args, const KigDocument& ) const
92 if ( ! margsparser.checkArgs( args, 2 ) ) return new InvalidImp;
94 const Coordinate a = static_cast<const PointImp*>( args[0] )->coordinate();
95 const Coordinate b = static_cast<const PointImp*>( args[1] )->coordinate();
96 Coordinate c;
97 if ( args.size() == 3 )
98 c = static_cast<const PointImp*>( args[2] )->coordinate();
99 else
101 // we pick the third point so that the three points form a
102 // triangle with equal sides...
104 // midpoint:
105 Coordinate m = ( b + a ) / 2;
106 if ( b.y != a.y )
108 // direction of the perpend:
109 double d = -(b.x-a.x)/(b.y-a.y);
111 // length:
112 // sqrt( 3 ) == tan( 60° ) == sqrt( 2^2 - 1^2 )
113 double l = 1.73205080756 * (a-b).length() / 2;
115 double d2 = d*d;
116 double l2 = l*l;
117 double dx = sqrt( l2 / ( d2 + 1 ) );
118 double dy = sqrt( l2 * d2 / ( d2 + 1 ) );
119 if( d < 0 ) dy = -dy;
121 c.x = m.x + dx;
122 c.y = m.y + dy;
124 else
126 c.x = m.x;
127 c.y = m.y + ( a.x - b.x );
131 const Coordinate center = calcCenter( a, b, c );
132 if ( center.valid() )
133 return new CircleImp( center, (center - a ).length() );
134 else return new InvalidImp;
137 const ObjectImpType* CircleBCPType::resultId() const
139 return CircleImp::stype();
142 const ObjectImpType* CircleBTPType::resultId() const
144 return CircleImp::stype();
147 static const ArgsParser::spec argsspecCircleBPR[] =
149 { PointImp::stype(), "SHOULD NOT BE SEEN", "SHOULD NOT BE SEEN", false },
150 { DoubleImp::stype(), "SHOULD NOT BE SEEN", "SHOULD NOT BE SEEN", false }
153 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( CircleBPRType )
155 CircleBPRType::CircleBPRType()
156 : ArgsParserObjectType( "CircleBPR", argsspecCircleBPR, 2 )
160 CircleBPRType::~CircleBPRType()
164 const CircleBPRType* CircleBPRType::instance()
166 static const CircleBPRType t;
167 return &t;
170 ObjectImp* CircleBPRType::calc( const Args& args, const KigDocument& ) const
172 if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;
173 const Coordinate c = static_cast<const PointImp*>( args[0] )->coordinate();
174 double r = static_cast<const DoubleImp*>( args[1] )->data();
175 return new CircleImp( c, r );
178 const ObjectImpType* CircleBPRType::resultId() const
180 return CircleImp::stype();