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 ()
|
Polygon::Polygon ()
|
||||||
{
|
{
|
||||||
points = new misc::SimpleVector<Point> (8);
|
|
||||||
minx = miny = 0xffffff;
|
minx = miny = 0xffffff;
|
||||||
maxx = maxy = -0xffffff;
|
maxx = maxy = -0xffffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
Polygon::~Polygon ()
|
|
||||||
{
|
|
||||||
delete points;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Draw polygon in view relative to point (x,y).
|
* Draw polygon in view relative to point (x,y).
|
||||||
*/
|
*/
|
||||||
void Polygon::draw (core::View *view, core::style::Style *style, int x, int y)
|
void Polygon::draw (core::View *view, core::style::Style *style, int x, int y)
|
||||||
{
|
{
|
||||||
if (points->size()) {
|
if (points.size()) {
|
||||||
int i;
|
int i;
|
||||||
const bool filled = false, convex = false;
|
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++) {
|
for (i = 0; i < points.size(); i++) {
|
||||||
pointArray[i].x = x + points->getRef(i)->x;
|
pointArray[i].x = x + points.at(i).x;
|
||||||
pointArray[i].y = y + points->getRef(i)->y;
|
pointArray[i].y = y + points.at(i).y;
|
||||||
}
|
}
|
||||||
view->drawPolygon(style->color, core::style::Color::SHADING_NORMAL,
|
view->drawPolygon(style->color, core::style::Color::SHADING_NORMAL,
|
||||||
filled, convex, pointArray.data(), i);
|
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)
|
void Polygon::addPoint (int x, int y)
|
||||||
{
|
{
|
||||||
points->increase ();
|
points.emplace_back ();
|
||||||
points->getRef(points->size () - 1)->x = x;
|
points.back().x = x;
|
||||||
points->getRef(points->size () - 1)->y = y;
|
points.back().y = y;
|
||||||
|
|
||||||
minx = misc::min(minx, x);
|
minx = misc::min(minx, x);
|
||||||
miny = misc::min(miny, y);
|
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)
|
bool Polygon::isPointWithin (int x, int y)
|
||||||
{
|
{
|
||||||
if (points->size () < 3 ||
|
if (points.size () < 3 ||
|
||||||
(x < minx || x > maxx || y < miny || y >= maxy))
|
(x < minx || x > maxx || y < miny || y >= maxy))
|
||||||
return false;
|
return false;
|
||||||
else {
|
else {
|
||||||
int numCrosses = 0;
|
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,
|
if (linesCross (minx - 1, miny - 1, x, y,
|
||||||
points->getRef(i)->x, points->getRef(i)->y,
|
points.at(i).x, points.at(i).y,
|
||||||
points->getRef(i + 1)->x, points->getRef(i + 1)->y))
|
points.at(i + 1).x, points.at(i + 1).y))
|
||||||
numCrosses++;
|
numCrosses++;
|
||||||
}
|
}
|
||||||
if (linesCross (minx - 1, miny - 1, x, y,
|
if (linesCross (minx - 1, miny - 1, x, y,
|
||||||
points->getRef(points->size () - 1)->x,
|
points.back().x,
|
||||||
points->getRef(points->size () - 1)->y,
|
points.back().y,
|
||||||
points->getRef(0)->x, points->getRef(0)->y))
|
points.at(0).x, points.at(0).y))
|
||||||
numCrosses++;
|
numCrosses++;
|
||||||
|
|
||||||
return numCrosses % 2 == 1;
|
return numCrosses % 2 == 1;
|
||||||
|
|||||||
@ -107,7 +107,7 @@ public:
|
|||||||
class Polygon: public Shape
|
class Polygon: public Shape
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
lout::misc::SimpleVector<Point> *points;
|
std::vector< Point > points;
|
||||||
int minx, miny, maxx, maxy;
|
int minx, miny, maxx, maxy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,7 +126,6 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Polygon ();
|
Polygon ();
|
||||||
~Polygon ();
|
|
||||||
|
|
||||||
void draw (core::View *view, core::style::Style *style, int x, int y);
|
void draw (core::View *view, core::style::Style *style, int x, int y);
|
||||||
void addPoint (int x, int y);
|
void addPoint (int x, int y);
|
||||||
|
|||||||
Reference in New Issue
Block a user