From 912446f2963edbe69667cf535a6791b41f0951b9 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Wed, 10 Jan 2018 15:20:44 +0300 Subject: [PATCH] Math.vala created #1 --- src/Chart.vala | 50 ++++++++++--------------------------------------- src/Marker.vala | 4 ++-- src/Math.vala | 39 ++++++++++++++++++++++++++++++++++++++ src/Text.vala | 4 ++-- 4 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 src/Math.vala diff --git a/src/Chart.vala b/src/Chart.vala index b6e9fc1..bd52590 100644 --- a/src/Chart.vala +++ b/src/Chart.vala @@ -216,25 +216,6 @@ namespace CairoChart { context.stroke(); } - protected virtual Float128 calc_round_step (Float128 aver_step, bool date_time = false) { - Float128 step = 1.0; - - if (aver_step > 1.0) { - if (date_time) while (step < aver_step) step *= 60; - if (date_time) while (step < aver_step) step *= 60; - if (date_time) while (step < aver_step) step *= 24; - while (step < aver_step) step *= 10; - if (step / 5 > aver_step) step /= 5; - while (step / 2 > aver_step) step /= 2; - } else if (aver_step > 0) { - while (step / 10 > aver_step) step /= 10; - if (step / 5 > aver_step) step /= 5; - while (step / 2 > aver_step) step /= 2; - } - - return step; - } - public double plot_x_min = 0; public double plot_x_max = 0; public double plot_y_min = 0; @@ -244,13 +225,6 @@ namespace CairoChart { public bool joint_y { get; protected set; default = false; } public Color joint_axis_color = Color (0, 0, 0, 1); - bool are_intersect (double a_min, double a_max, double b_min, double b_max) { - if ( a_min < a_max <= b_min < b_max - || b_min < b_max <= a_min < a_max) - return false; - return true; - } - protected virtual void set_vertical_axes_titles () { for (var si = 0; si < series.length; ++si) { var s = series[si]; @@ -305,7 +279,7 @@ namespace CairoChart { for (int sk = si; sk > sj; --sk) { var s3 = series[sk]; if (!s3.zoom_show) continue; - if (are_intersect(s2.place.zoom_x_min, s2.place.zoom_x_max, s3.place.zoom_x_min, s3.place.zoom_x_max) + if (math.are_intersect(s2.place.zoom_x_min, s2.place.zoom_x_max, s3.place.zoom_x_min, s3.place.zoom_x_max) || s2.axis_x.position != s3.axis_x.position || s2.axis_x.type != s3.axis_x.type) { has_intersection = true; @@ -359,7 +333,7 @@ namespace CairoChart { for (int sk = si; sk > sj; --sk) { var s3 = series[sk]; if (!s3.zoom_show) continue; - if (are_intersect(s2.place.zoom_y_min, s2.place.zoom_y_max, s3.place.zoom_y_min, s3.place.zoom_y_max) + if (math.are_intersect(s2.place.zoom_y_min, s2.place.zoom_y_max, s3.place.zoom_y_min, s3.place.zoom_y_max) || s2.axis_y.position != s3.axis_y.position || s2.axis_x.type != s3.axis_x.type) { has_intersection = true; @@ -396,12 +370,6 @@ namespace CairoChart { } } - bool point_belong (Float128 p, Float128 a, Float128 b) { - if (a > b) { Float128 tmp = a; a = b; b = tmp; } - if (a <= p <= b) return true; - return false; - } - protected virtual double compact_rec_x_pos (Series s, Float128 x, Text text) { var sz = text.get_size(context); return get_scr_x(s, x) - sz.width / 2.0 @@ -414,6 +382,8 @@ namespace CairoChart { + sz.height * (y - (s.axis_y.zoom_min + s.axis_y.zoom_max) / 2.0) / (s.axis_y.zoom_max - s.axis_y.zoom_min); } + protected CairoChart.Math math = new Math(); + protected virtual void draw_horizontal_axis () { for (var si = series.length - 1, nskip = 0; si >=0; --si) { var s = series[si]; @@ -428,7 +398,7 @@ namespace CairoChart { long max_nrecs = (long) ((plot_x_max - plot_x_min) * (s.place.zoom_x_max - s.place.zoom_x_min) / max_rec_width); // 3. Calculate grid step. - Float128 step = calc_round_step ((s.axis_x.zoom_max - s.axis_x.zoom_min) / max_nrecs, s.axis_x.type == Axis.Type.DATE_TIME); + Float128 step = math.calc_round_step ((s.axis_x.zoom_max - s.axis_x.zoom_min) / max_nrecs, s.axis_x.type == Axis.Type.DATE_TIME); if (step > s.axis_x.zoom_max - s.axis_x.zoom_min) step = s.axis_x.zoom_max - s.axis_x.zoom_min; @@ -469,7 +439,7 @@ namespace CairoChart { } // 5. Draw records, update cur_{x,y}_{min,max}. - for (Float128 x = x_min, x_max = s.axis_x.zoom_max; point_belong (x, x_min, x_max); x += step) { + for (Float128 x = x_min, x_max = s.axis_x.zoom_max; math.point_belong (x, x_min, x_max); x += step) { if (joint_x) set_source_rgba(joint_axis_color); else set_source_rgba(s.axis_x.color); string text = "", time_text = ""; @@ -551,7 +521,7 @@ namespace CairoChart { for (int sk = si; sk > sj; --sk) { var s3 = series[sk]; if (!s3.zoom_show) continue; - if (are_intersect(s2.place.zoom_x_min, s2.place.zoom_x_max, s3.place.zoom_x_min, s3.place.zoom_x_max) + if (math.are_intersect(s2.place.zoom_x_min, s2.place.zoom_x_max, s3.place.zoom_x_min, s3.place.zoom_x_max) || s2.axis_x.position != s3.axis_x.position || s2.axis_x.type != s3.axis_x.type) { has_intersection = true; @@ -593,7 +563,7 @@ namespace CairoChart { long max_nrecs = (long) ((plot_y_max - plot_y_min) * (s.place.zoom_y_max - s.place.zoom_y_min) / max_rec_height); // 3. Calculate grid step. - Float128 step = calc_round_step ((s.axis_y.zoom_max - s.axis_y.zoom_min) / max_nrecs); + Float128 step = math.calc_round_step ((s.axis_y.zoom_max - s.axis_y.zoom_min) / max_nrecs); if (step > s.axis_y.zoom_max - s.axis_y.zoom_min) step = s.axis_y.zoom_max - s.axis_y.zoom_min; @@ -638,7 +608,7 @@ namespace CairoChart { } // 5. Draw records, update cur_{x,y}_{min,max}. - for (Float128 y = y_min, y_max = s.axis_y.zoom_max; point_belong (y, y_min, y_max); y += step) { + for (Float128 y = y_min, y_max = s.axis_y.zoom_max; math.point_belong (y, y_min, y_max); y += step) { if (joint_y) set_source_rgba(joint_axis_color); else set_source_rgba(s.axis_y.color); var text = s.axis_y.format.printf((LongDouble)y); @@ -690,7 +660,7 @@ namespace CairoChart { for (int sk = si; sk > sj; --sk) { var s3 = series[sk]; if (!s3.zoom_show) continue; - if (are_intersect(s2.place.zoom_y_min, s2.place.zoom_y_max, s3.place.zoom_y_min, s3.place.zoom_y_max) + if (math.are_intersect(s2.place.zoom_y_min, s2.place.zoom_y_max, s3.place.zoom_y_min, s3.place.zoom_y_max) || s2.axis_y.position != s3.axis_y.position) { has_intersection = true; break; diff --git a/src/Marker.vala b/src/Marker.vala index e88b874..e5902ba 100644 --- a/src/Marker.vala +++ b/src/Marker.vala @@ -33,7 +33,7 @@ namespace CairoChart { break; case Type.CIRCLE: - chart.context.arc (x, y, size / 2, 0, 2*Math.PI); + chart.context.arc (x, y, size / 2, 0, 2 * GLib.Math.PI); chart.context.fill(); break; @@ -52,7 +52,7 @@ namespace CairoChart { break; case Type.PRICLE_CIRCLE: - chart.context.arc (x, y, size / 2, 0, 2*Math.PI); + chart.context.arc (x, y, size / 2, 0, 2 * GLib.Math.PI); chart.context.stroke(); break; diff --git a/src/Math.vala b/src/Math.vala new file mode 100644 index 0000000..da72045 --- /dev/null +++ b/src/Math.vala @@ -0,0 +1,39 @@ +namespace CairoChart { + + public class Math { + + public virtual Float128 calc_round_step (Float128 aver_step, bool date_time = false) { + Float128 step = 1.0; + + if (aver_step > 1.0) { + if (date_time) while (step < aver_step) step *= 60; + if (date_time) while (step < aver_step) step *= 60; + if (date_time) while (step < aver_step) step *= 24; + while (step < aver_step) step *= 10; + if (step / 5 > aver_step) step /= 5; + while (step / 2 > aver_step) step /= 2; + } else if (aver_step > 0) { + while (step / 10 > aver_step) step /= 10; + if (step / 5 > aver_step) step /= 5; + while (step / 2 > aver_step) step /= 2; + } + + return step; + } + + public bool are_intersect (double a_min, double a_max, double b_min, double b_max) { + if ( a_min < a_max <= b_min < b_max + || b_min < b_max <= a_min < a_max) + return false; + return true; + } + + public bool point_belong (Float128 p, Float128 a, Float128 b) { + if (a > b) { Float128 tmp = a; a = b; b = tmp; } + if (a <= p <= b) return true; + return false; + } + + public Math () {} + } +} diff --git a/src/Text.vala b/src/Text.vala index 4938391..b8813bc 100644 --- a/src/Text.vala +++ b/src/Text.vala @@ -58,9 +58,9 @@ namespace CairoChart { style.weight); context.set_font_size(style.size); if (style.orientation == Font.Orientation.VERTICAL) { - context.rotate(- Math.PI / 2.0); + context.rotate(- GLib.Math.PI / 2.0); context.show_text(text); - context.rotate(Math.PI / 2.0); + context.rotate(GLib.Math.PI / 2.0); } else { context.show_text(text); }