on bedf8bc help sign in
unity8/qml/Stage/Spread/MathUtils.js
79 lines 2.9 kB history raw
1.pragma library
3/**
4 * From processing.js: https://raw.githubusercontent.com/processing-js/processing-js/v1.4.8/processing.js
5 *
6 * Re-map a number from one range to another. In the example above, the number
7 * '25' is converted from a value in the range 0..100 into a value that
8 * ranges from the left edge (0) to the right edge (width) of the screen.
9 * Numbers outside the range are not clamped to 0 and 1, because out-of-range
10 * values are often intentional and useful.
11 *
12 * @param {Number} value The incoming value to be converted
13 * @param {Number} istart Lower bound of the value's current range
14 * @param {Number} istop Upper bound of the value's current range
15 * @param {Number} ostart Lower bound of the value's target range
16 * @param {Number} ostop Upper bound of the value's target range
17 *
18 * @returns {Number}
19 */
20function map(value, istart, istop, ostart, ostop) {
21 return ostart + (ostop - ostart) * ((value - istart) / (istop - istart))
24/**
25 * Return a value which is always between `min` and `max`
26 *
27 * @param {Number} value The current value
28 * @param {Number} min The minimum value
29 * @param {Number} max The maximum value
30 *
31 * @returns {Number}
32 */
33function clamp(value, min, max) {
34 if (value < min) return min
35 if (value > max) return max
36 return value
39// calculates the distance from the middle of one rect to middle of other rect
40function rectDistance(rect1, rect2) {
41 return pointDistance(Qt.point(rect1.x + rect1.width / 2, rect1.y + rect1.height / 2),
42 Qt.point(rect2.x + rect2.width / 2, rect2.y + rect2.height / 2))
45// calculates the distance between two points
46function pointDistance(point1, point2) {
47 return Math.sqrt(Math.pow(point1.x - point2.x, 2) +
48 Math.pow(point1.y - point2.y, 2)
49 )
52// from http://stackoverflow.com/questions/14616829/java-method-to-find-the-rectangle-that-is-the-intersection-of-two-rectangles-usi
53function intersectionRect(r1, r2) {
54 var xmin = Math.max(r1.x, r2.x);
55 var xmax1 = r1.x + r1.width;
56 var xmax2 = r2.x + r2.width;
57 var xmax = Math.min(xmax1, xmax2);
58 var out = {x:0, y:0, width:0, height:0}
59 if (xmax > xmin) {
60 var ymin = Math.max(r1.y, r2.y);
61 var ymax1 = r1.y + r1.height;
62 var ymax2 = r2.y + r2.height;
63 var ymax = Math.min(ymax1, ymax2);
64 if (ymax > ymin) {
65 out.x = xmin;
66 out.y = ymin;
67 out.width = xmax - xmin;
68 out.height = ymax - ymin;
69 }
70 }
71 return out;
74function easeOutCubic(t) { return (--t)*t*t+1 }
76function linearAnimation(startProgress, endProgress, startValue, endValue, progress) {
77 // progress : progressDiff = value : valueDiff => value = progress * valueDiff / progressDiff
78 return (progress - startProgress) * (endValue - startValue) / (endProgress - startProgress) + startValue;