Permalink
Browse files

Utils, better tests for points, simplified point implementation.

  • Loading branch information...
1 parent 2fd9c67 commit d60ed3646be2470932ff8403a83f1d4768010382 @tmcw tmcw committed Feb 15, 2012
Showing with 42 additions and 6 deletions.
  1. +6 −6 src/point.js
  2. +1 −0 test/browser/index.html
  3. +16 −0 test/browser/spec/Point.js
  4. +19 −0 test/browser/spec/Util.js
View
@@ -17,14 +17,14 @@
// Get the euclidean distance between two points
MM.Point.distance = function(p1, p2) {
- var dx = (p2.x - p1.x);
- var dy = (p2.y - p1.y);
- return Math.sqrt(dx*dx + dy*dy);
+ return Math.sqrt(
+ Math.pow(p2.x - p1.x, 2),
+ Math.pow(p2.y - p1.y, 2));
};
// Get a point between two other points, biased by `t`.
MM.Point.interpolate = function(p1, p2, t) {
- var px = p1.x + (p2.x - p1.x) * t;
- var py = p1.y + (p2.y - p1.y) * t;
- return new MM.Point(px, py);
+ return new MM.Point(
+ p1.x + (p2.x - p1.x) * t,
+ p1.y + (p2.y - p1.y) * t);
};
@@ -27,6 +27,7 @@
<script type="text/javascript" src="spec/Projection.js"></script>
<script type="text/javascript" src="spec/Transformation.js"></script>
<script type="text/javascript" src="spec/Provider.js"></script>
+ <script type="text/javascript" src="spec/Util.js"></script>
<script type="text/javascript">
(function() {
@@ -4,4 +4,20 @@ describe('Point', function() {
expect(p.x).toEqual(0);
expect(p.y).toEqual(1);
});
+
+ it('correctly computes distance to another point', function() {
+ var p = new MM.Point(0, 0);
+ var q = new MM.Point(0, 10);
+ expect(MM.Point.distance(p, q)).toEqual(10);
+
+ var p1 = new MM.Point(0, 0);
+ var q1 = new MM.Point(5, 2);
+ expect(MM.Point.distance(p1, q1)).toBeCloseTo(5.3851);
+ });
+
+ it('correctly interpolates positions', function() {
+ var p = new MM.Point(0, 0);
+ var q = new MM.Point(0, 10);
+ expect(MM.Point.interpolate(p, q, 0.5).y).toEqual(5);
+ });
});
@@ -0,0 +1,19 @@
+function Receiver() { }
+Receiver.prototype.receive = function() { };
+
+describe('Util', function() {
+ it('can get a frame', function() {
+ sink = new Receiver();
+ spyOn(sink, 'receive');
+
+ runs(function() {
+ MM.getFrame(sink.receive);
+ });
+
+ waits(200);
+
+ runs(function() {
+ expect(sink.receive).toHaveBeenCalled();
+ });
+ });
+});

0 comments on commit d60ed36

Please sign in to comment.