Polygon points vector STL-ified.
This commit is contained in:
36
dw/types.cc
36
dw/types.cc
@ -123,29 +123,23 @@ bool Circle::isPointWithin (int x, int y)
|
||||
|
||||
Polygon::Polygon ()
|
||||
{
|
||||
points = new misc::SimpleVector<Point> (8);
|
||||
minx = miny = 0xffffff;
|
||||
maxx = maxy = -0xffffff;
|
||||
}
|
||||
|
||||
Polygon::~Polygon ()
|
||||
{
|
||||
delete points;
|
||||
}
|
||||
|
||||
/*
|
||||
* Draw polygon in view relative to point (x,y).
|
||||
*/
|
||||
void Polygon::draw (core::View *view, core::style::Style *style, int x, int y)
|
||||
{
|
||||
if (points->size()) {
|
||||
if (points.size()) {
|
||||
int i;
|
||||
const bool filled = false, convex = false;
|
||||
std::vector< Point > pointArray( points->size() );
|
||||
std::vector< Point > pointArray( points.size() );
|
||||
|
||||
for (i = 0; i < points->size(); i++) {
|
||||
pointArray[i].x = x + points->getRef(i)->x;
|
||||
pointArray[i].y = y + points->getRef(i)->y;
|
||||
for (i = 0; i < points.size(); i++) {
|
||||
pointArray[i].x = x + points.at(i).x;
|
||||
pointArray[i].y = y + points.at(i).y;
|
||||
}
|
||||
view->drawPolygon(style->color, core::style::Color::SHADING_NORMAL,
|
||||
filled, convex, pointArray.data(), i);
|
||||
@ -154,9 +148,9 @@ void Polygon::draw (core::View *view, core::style::Style *style, int x, int y)
|
||||
|
||||
void Polygon::addPoint (int x, int y)
|
||||
{
|
||||
points->increase ();
|
||||
points->getRef(points->size () - 1)->x = x;
|
||||
points->getRef(points->size () - 1)->y = y;
|
||||
points.emplace_back ();
|
||||
points.back().x = x;
|
||||
points.back().y = y;
|
||||
|
||||
minx = misc::min(minx, x);
|
||||
miny = misc::min(miny, y);
|
||||
@ -198,21 +192,21 @@ bool Polygon::linesCross(int ax1, int ay1, int ax2, int ay2,
|
||||
|
||||
bool Polygon::isPointWithin (int x, int y)
|
||||
{
|
||||
if (points->size () < 3 ||
|
||||
if (points.size () < 3 ||
|
||||
(x < minx || x > maxx || y < miny || y >= maxy))
|
||||
return false;
|
||||
else {
|
||||
int numCrosses = 0;
|
||||
for (int i = 0; i < points->size () - 1; i++) {
|
||||
for (int i = 0; i < points.size () - 1; i++) {
|
||||
if (linesCross (minx - 1, miny - 1, x, y,
|
||||
points->getRef(i)->x, points->getRef(i)->y,
|
||||
points->getRef(i + 1)->x, points->getRef(i + 1)->y))
|
||||
points.at(i).x, points.at(i).y,
|
||||
points.at(i + 1).x, points.at(i + 1).y))
|
||||
numCrosses++;
|
||||
}
|
||||
if (linesCross (minx - 1, miny - 1, x, y,
|
||||
points->getRef(points->size () - 1)->x,
|
||||
points->getRef(points->size () - 1)->y,
|
||||
points->getRef(0)->x, points->getRef(0)->y))
|
||||
points.back().x,
|
||||
points.back().y,
|
||||
points.at(0).x, points.at(0).y))
|
||||
numCrosses++;
|
||||
|
||||
return numCrosses % 2 == 1;
|
||||
|
@ -107,7 +107,7 @@ public:
|
||||
class Polygon: public Shape
|
||||
{
|
||||
private:
|
||||
lout::misc::SimpleVector<Point> *points;
|
||||
std::vector< Point > points;
|
||||
int minx, miny, maxx, maxy;
|
||||
|
||||
/**
|
||||
@ -126,7 +126,6 @@ private:
|
||||
|
||||
public:
|
||||
Polygon ();
|
||||
~Polygon ();
|
||||
|
||||
void draw (core::View *view, core::style::Style *style, int x, int y);
|
||||
void addPoint (int x, int y);
|
||||
|
Reference in New Issue
Block a user