3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/21/2010 Split this file out of vector.cpp where it didn't belong. :-)
17 #include "vectorsolutions.h"
19 #include "enums.h" // For RS_MIN/MAXDOUBLE (!)
20 #include "mathextra.h"
23 * Constructor for no solution.
25 VectorSolutions::VectorSolutions(): vector(NULL)
33 VectorSolutions::VectorSolutions(const VectorSolutions & s): vector(NULL)
36 setTangent(s.isTangent());
38 for(int i=0; i<s.getNumber(); ++i)
43 * Constructor for num solutions.
45 VectorSolutions::VectorSolutions(int num): vector(NULL)
51 * Constructor for one solution.
53 VectorSolutions::VectorSolutions(const Vector & v1)
56 vector = new Vector[num];
62 * Constructor for two solutions.
64 VectorSolutions::VectorSolutions(const Vector & v1, const Vector & v2)
67 vector = new Vector[num];
74 * Constructor for three solutions.
76 VectorSolutions::VectorSolutions(const Vector & v1, const Vector & v2, const Vector & v3)
79 vector = new Vector[num];
87 * Constructor for four solutions.
89 VectorSolutions::VectorSolutions(const Vector & v1, const Vector & v2, const Vector & v3,
93 vector = new Vector[num];
102 * Constructor for four solutions.
104 VectorSolutions::VectorSolutions(const Vector & v1, const Vector & v2, const Vector & v3,
105 const Vector & v4, const Vector & v5)
108 vector = new Vector[num];
120 VectorSolutions::~VectorSolutions()
126 * Allocates 'num' vectors.
128 void VectorSolutions::alloc(int num)
132 vector = new Vector[num];
134 for(int i=0; i<num; ++i)
135 vector[i] = Vector(false);
141 * Deletes vector array and resets everything.
143 void VectorSolutions::clean()
154 * @return vector solution number i or an invalid vector if there
155 * are less solutions.
157 Vector VectorSolutions::get(int i) const
162 return Vector(false);
166 * @return Number of solutions available.
168 int VectorSolutions::getNumber() const
174 * @retval true There's at least one valid solution.
175 * @retval false There's no valid solution.
177 bool VectorSolutions::hasValid() const
179 for(int i=0; i<num; i++)
187 * Sets the solution i to the given vector.
188 * If i is greater than the current number of solutions available,
191 void VectorSolutions::set(int i, const Vector & v)
198 * Sets the tangent flag.
200 void VectorSolutions::setTangent(bool t)
206 * @return true if at least one of the solutions is a double solution
209 bool VectorSolutions::isTangent() const
215 * Rotates all vectors around the given center by the given angle.
217 void VectorSolutions::rotate(Vector center, double ang)
219 for(int i=0; i<num; i++)
221 vector[i].rotate(center, ang);
225 * Scales all vectors by the given factors with the given center.
227 void VectorSolutions::scale(Vector center, Vector factor)
229 for(int i=0; i<num; i++)
231 vector[i].scale(center, factor);
235 * @return vector solution which is the closest to the given coordinate.
236 * dist will contain the distance if it doesn't point to NULL (default).
238 Vector VectorSolutions::getClosest(const Vector & coord, double * dist, int * index) const
241 double minDist = RS_MAXDOUBLE;
242 Vector closestPoint(false);
244 for(int i=0; i<num; i++)
248 curDist = coord.distanceTo(vector[i]);
250 if (curDist < minDist)
252 closestPoint = vector[i];
267 VectorSolutions VectorSolutions::operator=(const VectorSolutions & s)
269 alloc(s.getNumber());
270 setTangent(s.isTangent());
272 for (int i=0; i<s.getNumber(); ++i)
278 std::ostream& operator << (std::ostream& os, const VectorSolutions& s)
280 for(int i=0; i<s.num; ++i)
281 os << "(" << s.get(i) << ")\n";
283 os << " tangent: " << (int)s.isTangent() << "\n";