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 06/01/2010 Added this text. :-)
15 #include "rs_infoarea.h"
23 RS_InfoArea::RS_InfoArea()
30 RS_InfoArea::~RS_InfoArea()
35 * Adds a point to the internal list
37 * @param p co-ordinate of the point
39 void RS_InfoArea::addPoint(const Vector & p)
41 if (thePoints.empty())
50 void RS_InfoArea::reset()
58 * Closes the polygon if it is not closed already.
60 void RS_InfoArea::close()
62 if (isValid() && isClosed() == false)
64 thePoints.append(thePoints.first());
66 RS_DEBUG->print("RS_InfoArea::close: closed");
71 * @retval true If the area is closed (i.e. start point and end point are
73 * @retval false Otherwise.
75 bool RS_InfoArea::isClosed()
77 return (thePoints.first().distanceTo(thePoints.last()) < 1.0e-4);
81 * @retval true If the area is defined (i.e. there are at least 3 points)
82 * @retval false If there are only two or less points.
84 bool RS_InfoArea::isValid()
86 RS_DEBUG->print("RS_InfoArea::isValid: count: %d", thePoints.count());
87 return (thePoints.count() > 2);
91 * Calculates the area and the circumference of the area.
93 void RS_InfoArea::calculate()
101 // at least 3 points needed for an area
104 ptFirst = thePoints.last();
105 thePoints.pop_back();
109 while (!thePoints.empty())
111 p2 = thePoints.last();
112 thePoints.pop_back();
114 area += calcSubArea(p1, p2);
115 circumference += p1.distanceTo(p2);
116 //if (p1 != ptFirst) {
122 area += calcSubArea(p1, ptFirst);
123 circumference += p1.distanceTo(ptFirst);
132 double RS_InfoArea::getArea()
137 double RS_InfoArea::getCircumference()
139 return circumference;
142 int RS_InfoArea::count()
144 return thePoints.count();
148 * Calculates a sub area.
150 * @param p1 first point
151 * @param p2 second point
153 double RS_InfoArea::calcSubArea(const Vector & p1, const Vector & p2)
155 double width = p2.x - p1.x;
156 double height = (p1.y - baseY) + (p2.y - baseY);
158 return (width * height) / 2.0;
161 /*! Calculates a distance
162 \param _p1 first point
163 \param _p2 second point
166 RS_InfoArea::calcDistance(Vector *_p1, Vector *_p2)
168 return mtGetDistance(_p1->getX(), _p1->getY(), _p2->getX(), _p2->getY());