-double*
-Curve::solve (std::vector<double> const & rhs)
-{
- std::vector<double>::size_type n = rhs.size();
- double* x = new double[n]; // Solution vector.
- double* tmp = new double[n]; // Temp workspace.
-
- double b = 2.0;
-
- x[0] = rhs[0] / b;
-
- for (std::vector<double>::size_type i = 1; i < n; i++) {
- // Decomposition and forward substitution.
- tmp[i] = 1 / b;
- b = (i < n - 1 ? 4.0 : 3.5) - tmp[i];
- x[i] = (rhs[i] - x[i - 1]) / b;
- }
-
- for (std::vector<double>::size_type i = 1; i < n; i++) {
- // Backsubstitution
- x[n - i - 1] -= tmp[n - i] * x[n - i];
+ /* curve of at least 3 points */
+
+ /* x-axis limits of the curve, in window space coordinates */
+
+ Duple w1 = item_to_window (Duple (_points.front().x, 0.0));
+ Duple w2 = item_to_window (Duple (_points.back().x, 0.0));
+
+ /* clamp actual draw to area bound by points, rather than our bounding box which is slightly different */
+
+ context->save ();
+ context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
+ context->clip ();
+
+ /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
+ they will still occur, but cairo's clipping will hide them.
+ */
+
+ draw = draw.expand (4.0);
+
+ /* now clip it to the actual points in the curve */
+
+ if (draw.x0 < w1.x) {
+ draw.x0 = w1.x;
+ }
+
+ if (draw.x1 >= w2.x) {
+ draw.x1 = w2.x;
+ }
+
+ /* find left and right-most sample */
+ Duple window_space;
+ Points::size_type left = 0;
+ Points::size_type right = n_samples;
+
+ for (Points::size_type idx = 0; idx < n_samples - 1; ++idx) {
+ left = idx;
+ window_space = item_to_window (Duple (samples[idx].x, 0.0));
+ if (window_space.x >= draw.x0) break;
+ }
+ for (Points::size_type idx = n_samples; idx > left + 1; --idx) {
+ window_space = item_to_window (Duple (samples[idx].x, 0.0));
+ if (window_space.x <= draw.x1) break;
+ right = idx;
+ }
+
+ /* draw line between samples */
+ window_space = item_to_window (Duple (samples[left].x, samples[left].y));
+ context->move_to (window_space.x, window_space.y);
+ for (uint32_t idx = left + 1; idx < right; ++idx) {
+ window_space = item_to_window (Duple (samples[idx].x, samples[idx].y));
+ context->line_to (window_space.x, window_space.y);
+ }
+
+ switch (curve_fill) {
+ case None:
+ context->stroke();
+ break;
+ case Inside:
+ context->stroke_preserve ();
+ window_space = item_to_window (Duple (samples[right-1].x, draw.height()));
+ context->line_to (window_space.x, window_space.y);
+ window_space = item_to_window (Duple (samples[left].x, draw.height()));
+ context->line_to (window_space.x, window_space.y);
+ context->close_path();
+ setup_fill_context(context);
+ context->fill ();
+ break;
+ case Outside:
+ context->stroke_preserve ();
+ window_space = item_to_window (Duple (samples[right-1].x, 0.0));
+ context->line_to (window_space.x, window_space.y);
+ window_space = item_to_window (Duple (samples[left].x, 0.0));
+ context->line_to (window_space.x, window_space.y);
+ context->close_path();
+ setup_fill_context(context);
+ context->fill ();
+ break;
+ }
+ context->restore ();