Cairo-Chart/src/Math.vala

152 lines
4.2 KiB
Vala

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 virtual 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 virtual 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 virtual bool x_in_range (double x, double x0, double x1) {
if (x0 <= x <= x1 || x1 <= x <= x0)
return true;
return false;
}
public virtual bool y_in_range (double y, double y0, double y1) {
if (y0 <= y <= y1 || y1 <= y <= y0)
return true;
return false;
}
public virtual bool point_in_rect (Point p, double x0, double x1, double y0, double y1) {
if (x_in_range(p.x, x0, x1) && y_in_range(p.y, y0, y1))
return true;
return false;
}
public virtual bool hcross (Point a1, Point a2, Float128 h_x1, Float128 h_x2, Float128 h_y, out Float128 x) {
x = 0;
if (a1.y == a2.y) return false;
if (a1.y >= h_y && a2.y >= h_y || a1.y <= h_y && a2.y <= h_y) return false;
x = a1.x + (a2.x - a1.x) * (h_y - a1.y) / (a2.y - a1.y);
if (h_x1 <= x <= h_x2 || h_x2 <= x <= h_x1)
return true;
return false;
}
public virtual bool vcross (Point a1, Point a2, Float128 v_x, Float128 v_y1, Float128 v_y2, out Float128 y) {
y = 0;
if (a1.x == a2.x) return false;
if (a1.x >= v_x && a2.x >= v_x || a1.x <= v_x && a2.x <= v_x) return false;
y = a1.y + (a2.y - a1.y) * (v_x - a1.x) / (a2.x - a1.x);
if (v_y1 <= y <= v_y2 || v_y2 <= y <= v_y1)
return true;
return false;
}
public delegate int PointComparator(Point a, Point b);
public virtual void sort_points_delegate(Point[] points, PointComparator compare) {
for(var i = 0; i < points.length; ++i) {
for(var j = i + 1; j < points.length; ++j) {
if(compare(points[i], points[j]) > 0) {
var tmp = points[i];
points[i] = points[j];
points[j] = tmp;
}
}
}
}
public virtual bool cut_line (Point p_min, Point p_max, Point a, Point b, out Point c, out Point d) {
int ncross = 0;
Float128 x = 0, y = 0;
Point pc[4];
if (hcross(a, b, p_min.x, p_max.x, p_min.y, out x))
pc[ncross++] = Point(x, p_min.y);
if (hcross(a, b, p_min.x, p_max.x, p_max.y, out x))
pc[ncross++] = Point(x, p_max.y);
if (vcross(a, b, p_min.x, p_min.y, p_max.y, out y))
pc[ncross++] = Point(p_min.x, y);
if (vcross(a, b, p_max.x, p_min.y, p_max.y, out y))
pc[ncross++] = Point(p_max.x, y);
c = a;
d = b;
if (ncross == 0) {
if ( point_in_rect (a, p_min.x, p_max.x, p_min.y, p_max.y)
&& point_in_rect (b, p_min.x, p_max.x, p_min.y, p_max.y))
return true;
return false;
}
if (ncross >= 2) {
c = pc[0]; d = pc[1];
return true;
}
if (ncross == 1) {
if (point_in_rect (a, p_min.x, p_max.x, p_min.y, p_max.y)) {
c = a;
d = pc[0];
return true;
} else if (point_in_rect (b, p_min.x, p_max.x, p_min.y, p_max.y)) {
c = b;
d = pc[0];
return true;
}
}
return false;
}
public virtual Point[] sort_points (Series s, Series.Sort sort) {
var points = s.points;
switch(sort) {
case Series.Sort.BY_X:
sort_points_delegate(points, (a, b) => {
if (a.x < b.x) return -1;
if (a.x > b.x) return 1;
return 0;
});
break;
case Series.Sort.BY_Y:
sort_points_delegate(points, (a, b) => {
if (a.y < b.y) return -1;
if (a.y > b.y) return 1;
return 0;
});
break;
}
return points;
}
public Math () {}
}
}