Path::Path(pointf* points, int pointCount)
{
/* copy over the points */
- _points = (pointf*)malloc(sizeof(_points[0]) * pointCount);
- memcpy(_points, points, sizeof(_points[0]) * pointCount);
- _pointCount = pointCount;
- }
-
- Path::~Path()
- {
- /* since we copied, we need to free */
- free(_points);
+ _points.reserve(pointCount);
+ for (int i = 0; i< pointCount; ++i) {
+ _points.push_back(points[i]);
+ }
}
boxf Path::GetBounds() const
{
boxf bounds;
- if (_points && _pointCount > 0)
+ if (!_points.empty())
{
/* lower left is the minimal point, upper right is maximal point */
bounds.LL.x = bounds.UR.x = _points[0].x;
bounds.LL.y = bounds.UR.y = _points[0].y;
- for (int i = 1; i < _pointCount; ++i)
+ for (const pointf &p : _points)
{
- if (bounds.LL.x > _points[i].x)
- bounds.LL.x = _points[i].x;
- if (bounds.LL.y > _points[i].y)
- bounds.LL.y = _points[i].y;
- if (bounds.UR.x < _points[i].x)
- bounds.UR.x = _points[i].x;
- if (bounds.UR.y < _points[i].y)
- bounds.UR.y = _points[i].y;
+ if (bounds.LL.x > p.x)
+ bounds.LL.x = p.x;
+ if (bounds.LL.y > p.y)
+ bounds.LL.y = p.y;
+ if (bounds.UR.x < p.x)
+ bounds.UR.x = p.x;
+ if (bounds.UR.y < p.y)
+ bounds.UR.y = p.y;
}
}
else
pointf Bezier::GetCenter() const
{
- if (_pointCount >= 4 && _pointCount % 2 == 0)
+ if (_points.size() >= 4 && _points.size() % 2 == 0)
{
pointf center;
/* the central control polygon for the bezier curve */
- pointf p0 = _points[_pointCount / 2 - 2];
- pointf p1 = _points[_pointCount / 2 - 1];
- pointf p2 = _points[_pointCount / 2];
- pointf p3 = _points[_pointCount / 2 + 1];
+ pointf p0 = _points[_points.size() / 2 - 2];
+ pointf p1 = _points[_points.size() / 2 - 1];
+ pointf p2 = _points[_points.size() / 2];
+ pointf p3 = _points[_points.size() / 2 + 1];
/* use de Casteljou's algorithm to get a midpoint */
center.x = 0.125 * p0.x + 0.375 * p1.x + 0.375 * p2.x + 0.125 * p3.x;
}
else
/* just return the middle point */
- return _points[_pointCount / 2];
+ return _points[_points.size() / 2];
}
void Bezier::Print(GVJ_t* job, pointf first, pointf last, bool allowCurves) const
gvputs(job, "<Geom>\n");
if (!_filled)
gvputs(job, "<NoFill>1</NoFill>\n");
- if (_pointCount > 0)
+ if (!_points.empty())
{
double xscale = 1.0 / (last.x - first.x);
double yscale = 1.0 / (last.y - first.y);
gvputs(job, "<NURBSTo>");
/* Ctl[P-1].X */
- gvprintf(job, "<X F='Width*%f'/>", (_points[_pointCount - 1].x - first.x) * xscale);
+ gvprintf(job, "<X F='Width*%f'/>", (_points.back().x - first.x) * xscale);
/* Ctl[P-1].Y */
- gvprintf(job, "<Y F='Height*%f'/>", (_points[_pointCount - 1].y - first.y) * yscale);
+ gvprintf(job, "<Y F='Height*%f'/>", (_points.back().y - first.y) * yscale);
/* Knot[P-1] */
- gvprintf(job, "<A>%d</A>", max(_pointCount - 4, 0));
+ gvprintf(job, "<A>%zu</A>", _points.size() < 4 ? 0 : (_points.size() - 4));
/* Ctl[P-1].Weight */
gvputs(job, "<B>1</B>");
gvputs(job, "<D>1</D>");
/* Knot[P], Degree, XType, YType */
- gvprintf(job, "<E F='NURBS(%d, 3, 0, 0", max(_pointCount - 3, 0));
- for (int i = 1; i < _pointCount; ++i)
+ gvprintf(job, "<E F='NURBS(%zu, 3, 0, 0", _points.size() < 3 ? 0 : (_points.size() - 3));
+ for (size_t i = 1; i < _points.size(); ++i)
/* Ctl[i].X, Ctl[i].Y, Knot[i], Ctl[i].Weight */
- gvprintf(job, ", %f, %f, %d, 1",
+ gvprintf(job, ", %f, %f, %zu, 1",
(_points[i].x - first.x) * xscale,
(_points[i].y - first.y) * yscale,
- max(i - 3, 0));
+ i < 3 ? 0 : (i - 3));
gvputs(job, ")'/>");
gvputs(job, "</NURBSTo>\n");
{
/* output lines only, so skip all the Bezier control points i.e. use every 3rd point */
- if (_pointCount == 4)
+ if (_points.size() == 4)
{
/* single point, use VDX LineTo */
gvputs(job, "<LineTo>");
{
/* multiple points, use VDX PolylineTo */
gvputs(job, "<PolylineTo>");
- gvprintf(job, "<X F='Width*%f' />", (_points[_pointCount - 1].x - first.x) * xscale);
- gvprintf(job, "<Y F='Height*%f' />", (_points[_pointCount - 1].y - first.y) * yscale);
+ gvprintf(job, "<X F='Width*%f' />", (_points.back().x - first.x) * xscale);
+ gvprintf(job, "<Y F='Height*%f' />", (_points.back().y - first.y) * yscale);
gvputs(job, "<A F='POLYLINE(0, 0");
- for (int i = 3; i < _pointCount - 1; i += 3)
+ for (size_t i = 3; i + 1 < _points.size(); i += 3)
gvprintf(job, ", %f, %f", (_points[i].x - first.x) * xscale, (_points[i].y - first.y) * yscale);
gvputs(job, ")' />");
gvputs(job, "</PolylineTo>\n");
gvputs(job, "<Geom>\n");
if (!_filled)
gvputs(job, "<NoFill>1</NoFill>\n");
- if (_pointCount > 0)
+ if (!_points.empty())
{
/* compute scale. if infinite, scale by 0 instead */
double xscale = 1.0 / (last.x - first.x);
gvprintf(job, "<Y F='Height*%f' />", (_points[0].y - first.y) * yscale);
gvputs(job, "</MoveTo>\n");
- if (_pointCount == 1)
+ if (_points.size() == 1)
{
/* single point, use VDX LineTo */
gvputs(job, "<LineTo>");
gvprintf(job, "<X F='Width*%f' />", (_points[0].x - first.x) * xscale);
gvprintf(job, "<Y F='Height*%f' />", (_points[0].y - first.y) * yscale);
gvputs(job, "<A F='POLYLINE(0, 0");
- for (int i = 1; i < _pointCount; ++i)
+ for (size_t i = 1; i < _points.size(); ++i)
gvprintf(job, ", %f, %f", (_points[i].x - first.x) * xscale, (_points[i].y - first.y) * yscale);
gvputs(job, ")' />");
gvputs(job, "</PolylineTo>\n");
void Polyline::Print(GVJ_t* job, pointf first, pointf last, bool) const
{
gvputs(job, "<Geom>\n");
- if (_pointCount > 0)
+ if (!_points.empty())
{
/* compute scale. if infinite, scale by 0 instead */
double xscale = 1.0 / (last.x - first.x);
gvprintf(job, "<Y F='Height*%f' />", (_points[0].y - first.y) * yscale);
gvputs(job, "</MoveTo>\n");
- if (_pointCount == 2)
+ if (_points.size() == 2)
{
/* single point, use VDX LineTo */
gvputs(job, "<LineTo>");
{
/* multiple points, use VDX PolylineTo */
gvputs(job, "<PolylineTo>");
- gvprintf(job, "<X F='Width*%f' />", (_points[_pointCount - 1].x - first.x) * xscale);
- gvprintf(job, "<Y F='Height*%f' />", (_points[_pointCount - 1].y - first.y) * yscale);
+ gvprintf(job, "<X F='Width*%f' />", (_points.back().x - first.x) * xscale);
+ gvprintf(job, "<Y F='Height*%f' />", (_points.back().y - first.y) * yscale);
gvputs(job, "<A F='POLYLINE(0, 0");
- for (int i = 1; i < _pointCount - 1; ++i)
+ for (size_t i = 1; i + 1 < _points.size(); ++i)
gvprintf(job, ", %f, %f", (_points[i].x - first.x) * xscale, (_points[i].y - first.y) * yscale);
gvputs(job, ")' />");
gvputs(job, "</PolylineTo>\n");