]> Shamusworld >> Repos - architektonas/blob - src/base/rs_infoarea.cpp.bak
Initial import
[architektonas] / src / base / rs_infoarea.cpp.bak
1 /****************************************************************************
2 ** $Id: rs_infoarea.cpp 1892 2004-07-09 23:54:59Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
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.
12 **
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.
16 **
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.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_infoarea.h"
28
29
30 #include "rs_infoarea.h"
31 #include "rs_math.h"
32 #include "rs_debug.h"
33
34
35 /**
36  * Constructor.
37  */
38 RS_InfoArea::RS_InfoArea() {
39 }
40
41
42
43 /**
44  * Destructor.
45  */
46 RS_InfoArea::~RS_InfoArea() {}
47
48
49
50 /** 
51  * Adds a point to the internal list
52  *
53  * @param p co-ordinate of the point
54  */
55 void RS_InfoArea::addPoint(const Vector& p) {
56         if (thePoints.empty()) {
57                 baseY = p.y;
58         }
59         
60         thePoints.append(p);
61 }
62
63
64
65 /**
66  * Resets the points.
67  */
68 void RS_InfoArea::reset() {
69         thePoints.clear();
70         area = 0.0;
71         circumference = 0.0;
72 }
73
74
75
76 /**
77  * Closes the polygon if it is not closed already.
78  */
79 void RS_InfoArea::close() {
80         if (isValid() && isClosed()==false) {
81                 
82                 thePoints.append(thePoints.first());
83
84                 RS_DEBUG->print("RS_InfoArea::close: closed");
85         }
86 }
87
88
89
90 /**
91  * @retval true If the area is closed (i.e. start point and end point are
92  *   identical)
93  * @retval false Otherwise.
94  */
95 bool RS_InfoArea::isClosed() {
96         return (thePoints.first().distanceTo(thePoints.last())<1.0e-4);
97 }
98
99
100
101 /**
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.
104  */
105 bool RS_InfoArea::isValid() {
106         RS_DEBUG->print("RS_InfoArea::isValid: count: %d", thePoints.count());
107         return (thePoints.count()>2);
108 }
109
110
111
112 /**
113  * Calculates the area and the circumference of the area.
114  */
115 void RS_InfoArea::calculate() {
116         area = 0.0;
117         circumference = 0.0;
118         Vector ptFirst; 
119         Vector p1;
120         Vector p2;
121         
122         // at least 3 points needed for an area
123         if (isValid()) {
124                 ptFirst = thePoints.last();
125                 thePoints.pop_back();
126                 
127                 p1 = ptFirst;
128                 while (!thePoints.empty()) {
129                         p2 = thePoints.last();
130                         thePoints.pop_back();
131                         
132                         area += calcSubArea(p1, p2);
133                         circumference += p1.distanceTo(p2);
134                         //if (p1 != ptFirst) {
135                         //      delete p1;
136                         //}
137                         p1 = p2;
138                 }
139                 area += calcSubArea(p1, ptFirst);
140                 circumference += p1.distanceTo(ptFirst);
141                 //delete p1;
142                 //delete ptFirst;
143         }
144
145         //thePoints.clear();
146         area = fabs(area);
147 }
148
149
150
151 /**
152  * Calculates a sub area.
153  * 
154  * @param p1 first point
155  * @param p2 second point
156  */
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);
160
161         return width * height / 2.0;
162 }
163
164
165
166 /*! Calculates a distance
167     \param _p1 first point
168     \param _p2 second point
169 */
170 /*double
171 RS_InfoArea::calcDistance(Vector *_p1, Vector *_p2)
172 {
173         return mtGetDistance(_p1->getX(), _p1->getY(), _p2->getX(), _p2->getY());
174 }*/
175
176
177 // EOF