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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/21/2010 Added this text. :-)
15 #include "vectorsolutions.h"
17 #include "rs.h" // For RS_MIN/MAXDOUBLE (!)
21 * Constructor for no solution.
23 VectorSolutions::VectorSolutions(): vector(NULL)
31 VectorSolutions::VectorSolutions(const VectorSolutions & s): vector(NULL)
34 setTangent(s.isTangent());
36 for(int i=0; i<s.getNumber(); ++i)
41 * Constructor for num solutions.
43 VectorSolutions::VectorSolutions(int num): vector(NULL)
49 * Constructor for one solution.
51 VectorSolutions::VectorSolutions(const Vector & v1)
54 vector = new Vector[num];
60 * Constructor for two solutions.
62 VectorSolutions::VectorSolutions(const Vector & v1, const Vector & v2)
65 vector = new Vector[num];
72 * Constructor for three solutions.
74 VectorSolutions::VectorSolutions(const Vector & v1, const Vector & v2, const Vector & v3)
77 vector = new Vector[num];
85 * Constructor for four solutions.
87 VectorSolutions::VectorSolutions(const Vector & v1, const Vector & v2, const Vector & v3,
91 vector = new Vector[num];
100 * Constructor for four solutions.
102 VectorSolutions::VectorSolutions(const Vector & v1, const Vector & v2, const Vector & v3,
103 const Vector & v4, const Vector & v5)
106 vector = new Vector[num];
118 VectorSolutions::~VectorSolutions()
124 * Allocates 'num' vectors.
126 void VectorSolutions::alloc(int num)
130 vector = new Vector[num];
132 for(int i=0; i<num; ++i)
133 vector[i] = Vector(false);
139 * Deletes vector array and resets everything.
141 void VectorSolutions::clean()
152 * @return vector solution number i or an invalid vector if there
153 * are less solutions.
155 Vector VectorSolutions::get(int i) const
160 return Vector(false);
164 * @return Number of solutions available.
166 int VectorSolutions::getNumber() const
172 * @retval true There's at least one valid solution.
173 * @retval false There's no valid solution.
175 bool VectorSolutions::hasValid() const
177 for(int i=0; i<num; i++)
185 * Sets the solution i to the given vector.
186 * If i is greater than the current number of solutions available,
189 void VectorSolutions::set(int i, const Vector & v)
196 * Sets the tangent flag.
198 void VectorSolutions::setTangent(bool t)
204 * @return true if at least one of the solutions is a double solution
207 bool VectorSolutions::isTangent() const
213 * Rotates all vectors around the given center by the given angle.
215 void VectorSolutions::rotate(Vector center, double ang)
217 for(int i=0; i<num; i++)
219 vector[i].rotate(center, ang);
223 * Scales all vectors by the given factors with the given center.
225 void VectorSolutions::scale(Vector center, Vector factor)
227 for(int i=0; i<num; i++)
229 vector[i].scale(center, factor);
233 * @return vector solution which is the closest to the given coordinate.
234 * dist will contain the distance if it doesn't point to NULL (default).
236 Vector VectorSolutions::getClosest(const Vector & coord, double * dist, int * index) const
239 double minDist = RS_MAXDOUBLE;
240 Vector closestPoint(false);
242 for(int i=0; i<num; i++)
246 curDist = coord.distanceTo(vector[i]);
248 if (curDist < minDist)
250 closestPoint = vector[i];
265 VectorSolutions VectorSolutions::operator=(const VectorSolutions & s)
267 alloc(s.getNumber());
268 setTangent(s.isTangent());
270 for (int i=0; i<s.getNumber(); ++i)
276 std::ostream& operator << (std::ostream& os, const VectorSolutions& s)
278 for(int i=0; i<s.num; ++i)
279 os << "(" << s.get(i) << ")\n";
281 os << " tangent: " << (int)s.isTangent() << "\n";