Skip to content

Commit

Permalink
Made Bezier code slightly more easy to read
Browse files Browse the repository at this point in the history
  • Loading branch information
kintel committed Mar 12, 2015
1 parent 72ca3c8 commit c2cb237
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/DrawingCallback.cc
Expand Up @@ -95,24 +95,27 @@ void DrawingCallback::line_to(Vector2d to)
pen = to;
}

// Quadric Bezier curve
void DrawingCallback::curve_to(Vector2d c1, Vector2d to)
{
for (unsigned long idx = 1;idx <= fn;idx++) {
const double a = idx * (1.0 / (double)fn);
const double x = pen[0] * t(a, 2) + c1[0] * 2 * t(a, 1) * a + to[0] * a * a;
const double y = pen[1] * t(a, 2) + c1[1] * 2 * t(a, 1) * a + to[1] * a * a;
add_vertex(Vector2d(x, y));
add_vertex(pen * pow(1-a, 2) +
c1 * 2 * pow(1-a, 1) * a +
to * pow(a, 2));
}
pen = to;
}

// Cubic Bezier curve
void DrawingCallback::curve_to(Vector2d c1, Vector2d c2, Vector2d to)
{
for (unsigned long idx = 1;idx <= fn;idx++) {
const double a = idx * (1.0 / (double)fn);
const double x = pen[0] * t(a, 3) + c1[0] * 3 * t(a, 2) * a + c2[0] * 3 * t(a, 1) * a * a + to[0] * a * a * a;
const double y = pen[1] * t(a, 3) + c1[1] * 3 * t(a, 2) * a + c2[1] * 3 * t(a, 1) * a * a + to[1] * a * a * a;
add_vertex(Vector2d(x, y));
add_vertex(pen * pow(1-a, 3) +
c1 * 3 * pow(1-a, 2) * a +
c2 * 3 * pow(1-a, 1) * pow(a, 2) +
to * pow(a, 3));
}
pen = to;
}
4 changes: 0 additions & 4 deletions src/DrawingCallback.h
Expand Up @@ -56,8 +56,4 @@ class DrawingCallback {
std::vector<const class Geometry *> polygons;

void add_vertex(Vector2d v);

inline double t(double t, int exp) const {
return pow(1.0 - t, exp);
}
};

0 comments on commit c2cb237

Please sign in to comment.