CoastalME (Coastal Modelling Environment)
Simulates the long-term behaviour of complex coastlines
Loading...
Searching...
No Matches
2d_shape.cpp
Go to the documentation of this file.
1
10
11/* ===============================================================================================================================
12 This file is part of CoastalME, the Coastal Modelling Environment.
13
14 CoastalME is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19===============================================================================================================================*/
20#include "2d_point.h"
21#include "2d_shape.h"
22
27
32
35{
36 // TODO 055 Maybe add a safety check?
37 return m_VPoints[n];
38}
39
40// //! Clears this 2D shape
41// void CA2DShape::Clear(void)
42// {
43// m_VPoints.clear();
44// }
45
47void CA2DShape::Resize(int const nSize)
48{
49 m_VPoints.resize(nSize);
50}
51
52// Returns the number of elements in this 2D shape
53int CA2DShape::nGetSize(void) const
54{
55 return static_cast<int>(m_VPoints.size());
56}
57
58// void CA2DShape::InsertAtFront(double const dX, double const dY)
59// {
60// m_VPoints.insert(m_VPoints.begin(), CGeom2DPoint(dX, dY));
61// }
62
65{
66 m_VPoints.push_back(*pPtNew);
67}
68
70void CA2DShape::Append(double const dX, double const dY)
71{
72 m_VPoints.push_back(CGeom2DPoint(dX, dY));
73}
74
76void CA2DShape::AppendIfNotPrevious(double const dX, double const dY)
77{
78 CGeom2DPoint const PtIn(dX, dY);
79
80 if (m_VPoints.empty())
81 m_VPoints.push_back(PtIn);
82
83 else if (m_VPoints.back() != &PtIn)
84 m_VPoints.push_back(PtIn);
85}
86
89{
90 return &m_VPoints.back();
91}
92
93// void CA2DShape::SetPoints(const vector<CGeom2DPoint>* VNewPoints)
94// {
95// m_VPoints = *VNewPoints;
96// }
97
98// int CA2DShape::nLookUp(CGeom2DPoint* Pt)
99// {
100// auto it = find(m_VPoints.begin(), m_VPoints.end(), *Pt);
101// if (it != m_VPoints.end())
102// return it - m_VPoints.begin();
103// else
104// return -1;
105// }
106
107// double CA2DShape::dGetLength(void) const
108// {
109// int nSize = m_VPoints.size();
110//
111// if (nSize < 2)
112// return -1;
113//
114// double dLength = 0;
115// for (int n = 1; n < nSize; n++)
116// {
117// double dXlen = m_VPoints[n].dGetX() - m_VPoints[n-1].dGetX();
118// double dYlen = m_VPoints[n].dGetY() - m_VPoints[n-1].dGetY();
119//
120// dLength += hypot(dXlen, dYlen);
121// }
122//
123// return dLength;
124// }
125
127vector<CGeom2DPoint>* CA2DShape::pPtVGetPoints(void)
128{
129 return &m_VPoints;
130}
131
132// //! Computes the centroid of this 2D polygon (which may be outside, if this is a concave polygon). From http://stackoverflow.com/questions/2792443/finding-the-centroid-of-a-polygon
133// CGeom2DPoint CA2DShape::PtGetCentroid(void)
134// {
135// int nVertexCount = static_cast<int>(m_VPoints.size());
136// double dSignedArea = 0;
137// double dCentroidX = 0;
138// double dCentroidY = 0;
139//
140// // For all vertices
141// for (int i = 0; i < nVertexCount; ++i)
142// {
143// double dXThis = m_VPoints[i].dGetX();
144// double dYThis = m_VPoints[i].dGetY();
145// double dXNext = m_VPoints[(i+1) % nVertexCount].dGetX();
146// double dYNext = m_VPoints[(i+1) % nVertexCount].dGetY();
147//
148// double dA = (dXThis * dYNext) - (dXNext * dYThis);
149// dSignedArea += dA;
150//
151// dCentroidX += (dXThis + dXNext) * dA;
152// dCentroidY += (dYThis + dYNext) * dA;
153// }
154//
155// dSignedArea *= 0.5;
156// dCentroidX /= (6 * dSignedArea);
157// dCentroidY /= (6 * dSignedArea);
158//
159// return (CGeom2DPoint(dCentroidX, dCentroidY));
160// }
161
164{
165 reverse(m_VPoints.begin(), m_VPoints.end());
166}
Contains CGeom2DPoint definitions.
Contains CA2DShape definitions.
vector< CGeom2DPoint > m_VPoints
The points which comprise the float-coordinate 2D shape.
Definition 2d_shape.h:37
void Append(CGeom2DPoint const *)
Appends a point to this 2D shape.
Definition 2d_shape.cpp:64
vector< CGeom2DPoint > * pPtVGetPoints(void)
Returns the address of the vector which represents this 2D shape.
Definition 2d_shape.cpp:127
void Reverse(void)
Reverses the sequence of points in the vector which represents this 2D polygon.
Definition 2d_shape.cpp:163
CA2DShape(void)
Constructor.
Definition 2d_shape.cpp:24
CGeom2DPoint * pPtBack(void)
Returns the last element of this 2D shape.
Definition 2d_shape.cpp:88
int nGetSize(void) const
Definition 2d_shape.cpp:53
void Resize(int const)
Resizes the vector which represents this 2D shape.
Definition 2d_shape.cpp:47
void AppendIfNotPrevious(double const, double const)
Appends a point to this 2D shape only if the point is not the same as the previous point in the vecto...
Definition 2d_shape.cpp:76
CGeom2DPoint & operator[](int const)
Operator to return one point of this 2D shape.
Definition 2d_shape.cpp:34
virtual ~CA2DShape(void)
Destructor.
Definition 2d_shape.cpp:29
Geometry class used to represent 2D point objects with floating-point coordinates.
Definition 2d_point.h:25