1 /****************************************************************************
2 ** $Id: rs_infoarea.cpp 1892 2004-07-09 23:54:59Z andrew $
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
6 ** This file is part of the qcadlib Library project.
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 ** See http://www.ribbonsoft.com for further details.
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
25 **********************************************************************/
27 #include "rs_infoarea.h"
35 RS_InfoArea::RS_InfoArea()
42 RS_InfoArea::~RS_InfoArea()
47 * Adds a point to the internal list
49 * @param p co-ordinate of the point
51 void RS_InfoArea::addPoint(const Vector & p)
53 if (thePoints.empty())
62 void RS_InfoArea::reset()
70 * Closes the polygon if it is not closed already.
72 void RS_InfoArea::close()
74 if (isValid() && isClosed() == false)
76 thePoints.append(thePoints.first());
78 RS_DEBUG->print("RS_InfoArea::close: closed");
83 * @retval true If the area is closed (i.e. start point and end point are
85 * @retval false Otherwise.
87 bool RS_InfoArea::isClosed()
89 return (thePoints.first().distanceTo(thePoints.last()) < 1.0e-4);
93 * @retval true If the area is defined (i.e. there are at least 3 points)
94 * @retval false If there are only two or less points.
96 bool RS_InfoArea::isValid()
98 RS_DEBUG->print("RS_InfoArea::isValid: count: %d", thePoints.count());
99 return (thePoints.count() > 2);
103 * Calculates the area and the circumference of the area.
105 void RS_InfoArea::calculate()
113 // at least 3 points needed for an area
116 ptFirst = thePoints.last();
117 thePoints.pop_back();
121 while (!thePoints.empty())
123 p2 = thePoints.last();
124 thePoints.pop_back();
126 area += calcSubArea(p1, p2);
127 circumference += p1.distanceTo(p2);
128 //if (p1 != ptFirst) {
134 area += calcSubArea(p1, ptFirst);
135 circumference += p1.distanceTo(ptFirst);
144 double RS_InfoArea::getArea()
149 double RS_InfoArea::getCircumference()
151 return circumference;
154 int RS_InfoArea::count()
156 return thePoints.count();
160 * Calculates a sub area.
162 * @param p1 first point
163 * @param p2 second point
165 double RS_InfoArea::calcSubArea(const Vector & p1, const Vector & p2)
167 double width = p2.x - p1.x;
168 double height = (p1.y - baseY) + (p2.y - baseY);
170 return (width * height) / 2.0;
173 /*! Calculates a distance
174 \param _p1 first point
175 \param _p2 second point
178 RS_InfoArea::calcDistance(Vector *_p1, Vector *_p2)
180 return mtGetDistance(_p1->getX(), _p1->getY(), _p2->getX(), _p2->getY());