Debian Codegraph @

closure-library/closure/goog/math/bezier_test.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.math.BezierTest');
16 goog.setTestOnly('goog.math.BezierTest');
17
18 goog.require('goog.math');
19 goog.require('goog.math.Bezier');
20 goog.require('goog.math.Coordinate');
21 goog.require('goog.testing.jsunit');
22
23 function testEquals() {
24 var input = new goog.math.Bezier(1, 2, 3, 4, 5, 6, 7, 8);
25
26 assert(input.equals(input));
27 }
28
29 function testClone() {
30 var input = new goog.math.Bezier(1, 2, 3, 4, 5, 6, 7, 8);
31
32 assertNotEquals('Clone returns a new object', input, input.clone());
33 assert('Contents of clone match original', input.equals(input.clone()));
34 }
35
36 function testFlip() {
37 var input = new goog.math.Bezier(1, 1, 2, 2, 3, 3, 4, 4);
38 var compare = new goog.math.Bezier(4, 4, 3, 3, 2, 2, 1, 1);
39
40 var flipped = input.clone();
41 flipped.flip();
42 assert('Flipped behaves as expected', compare.equals(flipped));
43
44 flipped.flip();
45 assert('Flipping twice gives original', input.equals(flipped));
46 }
47
48 function testGetPoint() {
49 var input = new goog.math.Bezier(0, 1, 1, 2, 2, 3, 3, 4);
50
51 assert(
52 goog.math.Coordinate.equals(
53 input.getPoint(0), new goog.math.Coordinate(0, 1)));
54 assert(
55 goog.math.Coordinate.equals(
56 input.getPoint(1), new goog.math.Coordinate(3, 4)));
57 assert(
58 goog.math.Coordinate.equals(
59 input.getPoint(0.5), new goog.math.Coordinate(1.5, 2.5)));
60 }
61
62 function testGetPointX() {
63 var input = new goog.math.Bezier(0, 1, 1, 2, 2, 3, 3, 4);
64
65 assert(goog.math.nearlyEquals(input.getPointX(0), 0));
66 assert(goog.math.nearlyEquals(input.getPointX(1), 3));
67 assert(goog.math.nearlyEquals(input.getPointX(0.5), 1.5));
68 }
69
70 function testGetPointY() {
71 var input = new goog.math.Bezier(0, 1, 1, 2, 2, 3, 3, 4);
72
73 assert(goog.math.nearlyEquals(input.getPointY(0), 1));
74 assert(goog.math.nearlyEquals(input.getPointY(1), 4));
75 assert(goog.math.nearlyEquals(input.getPointY(0.5), 2.5));
76 }
77
78 function testSubdivide() {
79 var input = new goog.math.Bezier(0, 1, 1, 2, 2, 3, 3, 4);
80
81 input.subdivide(1 / 3, 2 / 3);
82
83 assert(goog.math.nearlyEquals(1, input.x0));
84 assert(goog.math.nearlyEquals(2, input.y0));
85 assert(goog.math.nearlyEquals(2, input.x3));
86 assert(goog.math.nearlyEquals(3, input.y3));
87 }
88
89 function testSolvePositionFromXValue() {
90 var eps = 1e-6;
91 var bezier = new goog.math.Bezier(0, 0, 0.25, 0.1, 0.25, 1, 1, 1);
92 var pt = bezier.getPoint(0.5);
93 assertRoughlyEquals(0.3125, pt.x, eps);
94 assertRoughlyEquals(0.5375, pt.y, eps);
95 assertRoughlyEquals(
96 0.321, bezier.solvePositionFromXValue(bezier.getPoint(0.321).x), eps);
97 }
98
99 function testSolveYValueFromXValue() {
100 var eps = 1e-6;
101 // The following example is taken from
102 // http://www.netzgesta.de/dev/cubic-bezier-timing-function.html.
103 // The timing values shown in that page are 1 - <value> so the
104 // bezier curves in this test are constructed with 1 - ctrl points.
105 // E.g. ctrl points (0, 0, 0.25, 0.1, 0.25, 1, 1, 1) become
106 // (1, 1, 0.75, 0, 0.75, 0.9, 0, 0) here. Since chanding the order of
107 // the ctrl points does not affect the shape of the curve, once can also
108 // have (0, 0, 0.75, 0.9, 0.75, 0, 1, 1).
109
110 // netzgesta example.
111 var bezier = new goog.math.Bezier(1, 1, 0.75, 0.9, 0.75, 0, 0, 0);
112 assertRoughlyEquals(0.024374631, bezier.solveYValueFromXValue(0.2), eps);
113 assertRoughlyEquals(0.317459494, bezier.solveYValueFromXValue(0.6), eps);
114 assertRoughlyEquals(0.905205002, bezier.solveYValueFromXValue(0.9), eps);
115
116 // netzgesta example with ctrl points in the reverse order so that 1st and
117 // last ctrl points are (0, 0) and (1, 1). Note the result is exactly the
118 // same.
119 bezier = new goog.math.Bezier(0, 0, 0.75, 0, 0.75, 0.9, 1, 1);
120 assertRoughlyEquals(0.024374631, bezier.solveYValueFromXValue(0.2), eps);
121 assertRoughlyEquals(0.317459494, bezier.solveYValueFromXValue(0.6), eps);
122 assertRoughlyEquals(0.905205002, bezier.solveYValueFromXValue(0.9), eps);
123
124 // Ease-out css animation timing in webkit.
125 bezier = new goog.math.Bezier(0, 0, 0, 0, 0.58, 1, 1, 1);
126 assertRoughlyEquals(0.308366667, bezier.solveYValueFromXValue(0.2), eps);
127 assertRoughlyEquals(0.785139061, bezier.solveYValueFromXValue(0.6), eps);
128 assertRoughlyEquals(0.982973389, bezier.solveYValueFromXValue(0.9), eps);
129 }