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"
30 #include "rs_infoarea.h"
38 RS_InfoArea::RS_InfoArea() {
46 RS_InfoArea::~RS_InfoArea() {}
51 * Adds a point to the internal list
53 * @param p co-ordinate of the point
55 void RS_InfoArea::addPoint(const Vector& p) {
56 if (thePoints.empty()) {
68 void RS_InfoArea::reset() {
77 * Closes the polygon if it is not closed already.
79 void RS_InfoArea::close() {
80 if (isValid() && isClosed()==false) {
82 thePoints.append(thePoints.first());
84 RS_DEBUG->print("RS_InfoArea::close: closed");
91 * @retval true If the area is closed (i.e. start point and end point are
93 * @retval false Otherwise.
95 bool RS_InfoArea::isClosed() {
96 return (thePoints.first().distanceTo(thePoints.last())<1.0e-4);
102 * @retval true If the area is defined (i.e. there are at least 3 points)
103 * @retval false If there are only two or less points.
105 bool RS_InfoArea::isValid() {
106 RS_DEBUG->print("RS_InfoArea::isValid: count: %d", thePoints.count());
107 return (thePoints.count()>2);
113 * Calculates the area and the circumference of the area.
115 void RS_InfoArea::calculate() {
122 // at least 3 points needed for an area
124 ptFirst = thePoints.last();
125 thePoints.pop_back();
128 while (!thePoints.empty()) {
129 p2 = thePoints.last();
130 thePoints.pop_back();
132 area += calcSubArea(p1, p2);
133 circumference += p1.distanceTo(p2);
134 //if (p1 != ptFirst) {
139 area += calcSubArea(p1, ptFirst);
140 circumference += p1.distanceTo(ptFirst);
152 * Calculates a sub area.
154 * @param p1 first point
155 * @param p2 second point
157 double RS_InfoArea::calcSubArea(const Vector& p1, const Vector& p2) {
158 double width = p2.x - p1.x;
159 double height = (p1.y - baseY) + (p2.y - baseY);
161 return width * height / 2.0;
166 /*! Calculates a distance
167 \param _p1 first point
168 \param _p2 second point
171 RS_InfoArea::calcDistance(Vector *_p1, Vector *_p2)
173 return mtGetDistance(_p1->getX(), _p1->getY(), _p2->getX(), _p2->getY());