|
1 | 1 | var polygon = require('@src/lib/polygon'),
|
2 |
| - polygonTester = polygon.tester; |
| 2 | + polygonTester = polygon.tester, |
| 3 | + isBent = polygon.isSegmentBent, |
| 4 | + filter = polygon.filter; |
3 | 5 |
|
4 | 6 | describe('polygon.tester', function() {
|
5 | 7 | 'use strict';
|
@@ -144,3 +146,65 @@ describe('polygon.tester', function() {
|
144 | 146 | });
|
145 | 147 | });
|
146 | 148 | });
|
| 149 | + |
| 150 | +describe('polygon.isSegmentBent', function() { |
| 151 | + 'use strict'; |
| 152 | + |
| 153 | + var pts = [[0, 0], [1, 1], [2, 0], [1, 0], [100, -37]]; |
| 154 | + |
| 155 | + it('should treat any two points as straight', function() { |
| 156 | + for(var i = 0; i < pts.length - 1; i++) { |
| 157 | + expect(isBent(pts, i, i + 1, 0)).toBe(false); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + function rotatePt(theta) { |
| 162 | + return function(pt) { |
| 163 | + return [ |
| 164 | + pt[0] * Math.cos(theta) - pt[1] * Math.sin(theta), |
| 165 | + pt[0] * Math.sin(theta) + pt[1] * Math.cos(theta)]; |
| 166 | + }; |
| 167 | + } |
| 168 | + |
| 169 | + it('should find a bent line at the right tolerance', function() { |
| 170 | + for(var theta = 0; theta < 6; theta += 0.3) { |
| 171 | + var pts2 = pts.map(rotatePt(theta)); |
| 172 | + expect(isBent(pts2, 0, 2, 0.99)).toBe(true); |
| 173 | + expect(isBent(pts2, 0, 2, 1.01)).toBe(false); |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + it('should treat any backward motion as bent', function() { |
| 178 | + expect(isBent([[0, 0], [2, 0], [1, 0]], 0, 2, 10)).toBe(true); |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +describe('polygon.filter', function() { |
| 183 | + 'use strict'; |
| 184 | + |
| 185 | + var pts = [ |
| 186 | + [0, 0], [1, 0], [2, 0], [3, 0], |
| 187 | + [3, 1], [3, 2], [3, 3], |
| 188 | + [2, 3], [1, 3], [0, 3], |
| 189 | + [0, 2], [0, 1], [0, 0]]; |
| 190 | + |
| 191 | + var ptsOut = [[0, 0], [3, 0], [3, 3], [0, 3], [0, 0]]; |
| 192 | + |
| 193 | + it('should give the right result if points are provided upfront', function() { |
| 194 | + expect(filter(pts, 0.5).filtered).toEqual(ptsOut); |
| 195 | + }); |
| 196 | + |
| 197 | + it('should give the right result if points are added one-by-one', function() { |
| 198 | + var p = filter([pts[0]], 0.5), |
| 199 | + i; |
| 200 | + |
| 201 | + // intermediate result (the last point isn't in the final) |
| 202 | + for(i = 1; i < 6; i++) p.addPt(pts[i]); |
| 203 | + expect(p.filtered).toEqual([[0, 0], [3, 0], [3, 2]]); |
| 204 | + |
| 205 | + // final result |
| 206 | + for(i = 6; i < pts.length; i++) p.addPt(pts[i]); |
| 207 | + expect(p.filtered).toEqual(ptsOut); |
| 208 | + }); |
| 209 | + |
| 210 | +}); |
0 commit comments