static void plot_curve_accurate(const Curve &curve, float step, T plot_func) { if (curve.get_point_count() <= 1) { // Not enough points to make a curve, so it's just a straight line float y = curve.interpolate(0); plot_func(Vector2(0, y), Vector2(1.f, y), true); } else { Vector2 first_point = curve.get_point_position(0); Vector2 last_point = curve.get_point_position(curve.get_point_count() - 1); // Edge lines plot_func(Vector2(0, first_point.y), first_point, false); plot_func(Vector2(Curve::MAX_X, last_point.y), last_point, false); // Draw section by section, so that we get maximum precision near points. // It's an accurate representation, but slower than using the baked one. for (int i = 1; i < curve.get_point_count(); ++i) { Vector2 a = curve.get_point_position(i - 1); Vector2 b = curve.get_point_position(i); Vector2 pos = a; Vector2 prev_pos = a; float len = b.x - a.x; for (float x = step; x < len; x += step) { pos.x = a.x + x; pos.y = curve.interpolate_local_nocheck(i - 1, x); plot_func(prev_pos, pos, true); prev_pos = pos; } plot_func(prev_pos, b, true); } } }
main() { double ans, exact, err; int m; double eps = 1.0e-6; double a = 0.0; double b = PI; int n_jac = 12; std::vector<double> x_jac(n_jac); std::vector<double> w_jac(n_jac); gauss_jacobi(x_jac, w_jac, n_jac, 1.5, -0.5); printf("\n\n Abscissas and wights from gauss_jacobi with alpha = 1.5, beta = -0.5.\n"); for (int i = 0; i < n_jac; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i, x_jac[i], i, w_jac[i]); int n_leg = 12; std::vector<double> x_leg(n_leg); std::vector<double> w_leg(n_leg); gauss_legendre(n_leg, a, b, x_leg, w_leg); printf("\n\n Abscissas and wights from gauss_legendre.\n"); for (int i = 0; i < n_leg; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i, x_leg[i] / PIO2 - 1.0, i, w_leg[i] / PIO2); int n_jac2 = 12; std::vector<double> x_jac2(n_jac2); std::vector<double> w_jac2(n_jac2); gauss_jacobi(x_jac2, w_jac2, n_jac2, 0.0, 0.0); printf("\n\n Abscissas and wights from gauss_jacobi with alpha = beta = 0.0.\n"); for (int i = 0; i < n_jac2; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i, x_jac2[i], i, w_jac2[i]); int n_cheb = 12; std::vector<double> x_cheb(n_cheb); std::vector<double> w_cheb(n_cheb); gauss_chebyshev(x_cheb, w_cheb, n_cheb); printf("\n\n Abscissas and wights from gauss_chebyshev.\n"); for (int i = 0; i < (n_cheb + 1) / 2; ++i) printf("\n x[%d] = -x[%d] = %16.12f w[%d] = w[%d] = %16.12f", i, n_cheb + 1 - i , x_cheb[i], i, n_cheb + 1 - i, w_cheb[i]); int n_jac3 = 12; std::vector<double> x_jac3(n_jac3); std::vector<double> w_jac3(n_jac3); gauss_jacobi(x_jac3, w_jac3, n_jac3, -0.5, -0.5); printf("\n\n Abscissas and wights from gauss_jacobi with alpha = beta = -0.5.\n"); for (int i = 0; i < n_jac3; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i, x_jac3[i], i, w_jac3[i]); int n_herm = 12; std::vector<double> x_herm(n_herm); std::vector<double> w_herm(n_herm); gauss_hermite(x_herm, w_herm, n_herm); printf("\n\n Abscissas and wights from gauss_hermite.\n"); for (int i = 0; i < (n_herm + 1) / 2; ++i) printf("\n x[%d] = -x[%d] = %16.12f w[%d] = w[%d] = %16.12f", i, n_herm + 1 - i , x_herm[i], i, n_herm + 1 - i, w_herm[i]); n_lag = 12; std::vector<double> x_lag(n_lag); std::vector<double> w_lag(n_lag); gauss_laguerre(x_lag, w_lag, n_lag, 1.0); printf("\n\n Abscissas and wights from gauss_laguerre.\n"); for (int i = 0; i < n_lag; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i , x_lag[i], i, w_lag[i]); m = 40; std::vector<double> c(m); std::vector<double> cint(m); std::vector<double> cder(m); printf("\n\n\n\n Test of integration routines..."); printf("\n\n"); printf("\n\n %-40s %g", "Input requested error", eps); printf("\n\n %-40s %d", "Input order of Gaussian quadrature", n_leg); printf("\n\n %-40s %d", "Input order of Chebyshev fit", m); printf("\n\n"); a = 0.0; b = PI; printf("\n\n Integrate cos(x) from a = %f to b = %f . . .", a, PI); exact = 0.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(std::cos, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(std::cos, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's rule", ans, err); ans = quad_romberg(std::cos, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(std::cos, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, cos); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate sin(x) from a = %f to b = %f . . .", a, b); exact = 2.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(sin, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(sin, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's rule", ans, err); ans = quad_romberg(sin, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(sin, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, sin); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate cos^2(x) from a = %f to b = %f . . .", a, b); exact = PI/2.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(cos2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(cos2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's rule", ans, err); ans = quad_romberg(cos2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(cos2, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, cos2); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate sin^2(x) from a = %f to b = %f . . .", a, b); exact = PI/2.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(sin2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(sin2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's integral", ans, err); ans = quad_romberg(sin2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(sin2, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, sin2); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate J_1(x) from a = %f to b = %f . . .", a, b); exact = bessel_j0(0.0) - bessel_j0(PI); printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(bessel_j1, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(bessel_j1, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's integral", ans, err); ans = quad_romberg(bessel_j1, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(bessel_j1, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, bessel_j1); chebyshev_integ(a, b, c, cint, m); chebyshev_deriv(a, b, c, cder, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = 10.0*PI; printf("\n\n Integrate foo(x) = (1 - x)exp(-x/2) from a = %f to b = %f . . .", a, b); exact = 2.0*(1.0 + b)*exp(-b/2.0) - 2.0*(1.0 + a)*exp(-a/2.0); printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(foo, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(foo, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's rule", ans, err); ans = quad_romberg_open(foo, a, b, eps, midpoint_exp); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(foo, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); gauss_laguerre(x_lag, w_lag, n_lag, 0.0); ans = 0.0; for (int i = 0; i < n_lag; ++i) ans += 2 * w_lag[i] * foonum(2.0 * x_lag[i]); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Laguerre quadrature", ans, err); chebyshev_fit(a, b, c, m, foo); chebyshev_integ(a, b, c, cint, m); chebyshev_deriv(a, b, c, cder, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate funk1(x) = cos(x)/sqrt(x(PI - x)) from a = %f to b = %f . . .", a, b); exact = 0.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_romberg_open(funk1, a, b, eps, midpoint); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg quadrature with midpoint", ans, err); ans = quad_romberg_open(funk1, a, (a+b)/2, eps, midpoint_inv_sqrt_lower) + quad_romberg_open(funk1, (a+b)/2, b, eps, midpoint_inv_sqrt_upper); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg with inverse sqrt step", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(funk1, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); ans = quad_gauss(funk1num, a, b, x_cheb, w_cheb, n_cheb); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Chebyshev quadrature", ans, err); ans = quad_gauss(funk1num, a, b, x_jac, w_jac, n_jac); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Jacobi quadrature", ans, err); chebyshev_fit(a, b, c, m, funk1); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = dumb_gauss_crap(funk1num, a, b, 8); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Chebyshev quadrature", ans, err); printf("\n\n"); plot_func(funk1, a+0.1, b-0.1, "", "", "", ""); a = 0.0; b = PI; printf("\n\n Integrate funk2(x) = (2.0+sin(x))/sqrt(x(PI - x)) from a = %f to b = %f . . .", a, b); exact = 0.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_romberg_open(funk2, a, b, eps, midpoint); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg quadrature with midpoint", ans, err); ans = quad_romberg_open(funk2, a, (a+b)/2, eps, midpoint_inv_sqrt_lower) + quad_romberg_open(funk2, (a+b)/2, b, eps, midpoint_inv_sqrt_upper); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg with inverse sqrt step", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(funk2, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); ans = quad_gauss(funk2num, a, b, x_cheb, w_cheb, n_cheb); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Chebyshev quadrature", ans, err); ans = quad_gauss(funk2num, a, b, x_jac, w_jac, n_jac); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Jacobi quadrature", ans, err); chebyshev_fit(a, b, c, m, funk2); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = dumb_gauss_crap(funk2num, a, b, 8); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Adaptive Gauss - Chebyshev quadrature", ans, err); printf("\n\n"); plot_func(funk2, a+0.1, b-0.1, "", "", "", ""); printf("\n\n"); }
void CurveEditor::_draw() { if (_curve_ref.is_null()) return; Curve &curve = **_curve_ref; update_view_transform(); // Background Vector2 view_size = get_rect().size; draw_style_box(get_stylebox("bg", "Tree"), Rect2(Point2(), view_size)); // Grid draw_set_transform_matrix(_world_to_view); Vector2 min_edge = get_world_pos(Vector2(0, view_size.y)); Vector2 max_edge = get_world_pos(Vector2(view_size.x, 0)); const Color grid_color0 = get_color("grid_major_color", "Editor"); const Color grid_color1 = get_color("grid_minor_color", "Editor"); draw_line(Vector2(min_edge.x, curve.get_min_value()), Vector2(max_edge.x, curve.get_min_value()), grid_color0); draw_line(Vector2(max_edge.x, curve.get_max_value()), Vector2(min_edge.x, curve.get_max_value()), grid_color0); draw_line(Vector2(0, min_edge.y), Vector2(0, max_edge.y), grid_color0); draw_line(Vector2(1, max_edge.y), Vector2(1, min_edge.y), grid_color0); float curve_height = (curve.get_max_value() - curve.get_min_value()); const Vector2 grid_step(0.25, 0.5 * curve_height); for (real_t x = 0; x < 1.0; x += grid_step.x) { draw_line(Vector2(x, min_edge.y), Vector2(x, max_edge.y), grid_color1); } for (real_t y = curve.get_min_value(); y < curve.get_max_value(); y += grid_step.y) { draw_line(Vector2(min_edge.x, y), Vector2(max_edge.x, y), grid_color1); } // Markings draw_set_transform_matrix(Transform2D()); Ref<Font> font = get_font("font", "Label"); float font_height = font->get_height(); Color text_color = get_color("font_color", "Editor"); { // X axis float y = curve.get_min_value(); Vector2 off(0, font_height - 1); draw_string(font, get_view_pos(Vector2(0, y)) + off, "0.0", text_color); draw_string(font, get_view_pos(Vector2(0.25, y)) + off, "0.25", text_color); draw_string(font, get_view_pos(Vector2(0.5, y)) + off, "0.5", text_color); draw_string(font, get_view_pos(Vector2(0.75, y)) + off, "0.75", text_color); draw_string(font, get_view_pos(Vector2(1, y)) + off, "1.0", text_color); } { // Y axis float m0 = curve.get_min_value(); float m1 = 0.5 * (curve.get_min_value() + curve.get_max_value()); float m2 = curve.get_max_value(); Vector2 off(1, -1); draw_string(font, get_view_pos(Vector2(0, m0)) + off, String::num(m0, 2), text_color); draw_string(font, get_view_pos(Vector2(0, m1)) + off, String::num(m1, 2), text_color); draw_string(font, get_view_pos(Vector2(0, m2)) + off, String::num(m2, 3), text_color); } // Draw tangents for current point if (_selected_point >= 0) { const Color tangent_color = get_color("accent_color", "Editor"); int i = _selected_point; Vector2 pos = curve.get_point_position(i); if (i != 0) { Vector2 control_pos = get_tangent_view_pos(i, TANGENT_LEFT); draw_line(get_view_pos(pos), control_pos, tangent_color); draw_rect(Rect2(control_pos, Vector2(1, 1)).grow(2), tangent_color); } if (i != curve.get_point_count() - 1) { Vector2 control_pos = get_tangent_view_pos(i, TANGENT_RIGHT); draw_line(get_view_pos(pos), control_pos, tangent_color); draw_rect(Rect2(control_pos, Vector2(1, 1)).grow(2), tangent_color); } } // Draw lines draw_set_transform_matrix(_world_to_view); const Color line_color = get_color("highlight_color", "Editor"); const Color edge_line_color = get_color("font_color", "Editor"); CanvasItemPlotCurve plot_func(*this, line_color, edge_line_color); plot_curve_accurate(curve, 4.f / view_size.x, plot_func); // Draw points draw_set_transform_matrix(Transform2D()); const Color point_color = get_color("font_color", "Editor"); const Color selected_point_color = get_color("accent_color", "Editor"); for (int i = 0; i < curve.get_point_count(); ++i) { Vector2 pos = curve.get_point_position(i); draw_rect(Rect2(get_view_pos(pos), Vector2(1, 1)).grow(3), i == _selected_point ? selected_point_color : point_color); // TODO Circles are prettier. Needs a fix! Or a texture //draw_circle(pos, 2, point_color); } // Hover if (_hover_point != -1) { const Color hover_color = line_color; Vector2 pos = curve.get_point_position(_hover_point); stroke_rect(Rect2(get_view_pos(pos), Vector2(1, 1)).grow(_hover_radius), hover_color); } // Help text if (_selected_point > 0 && _selected_point + 1 < curve.get_point_count()) { text_color.a *= 0.4; draw_string(font, Vector2(50, font_height), TTR("Hold Shift to edit tangents individually"), text_color); } }
static void draw_func_name (FILE *fp, int x, int index) { char *string = plot_func (index); fprintf (fp, "(%s) %d %d name\n", string, x, 0); }