cornerstone-source 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +20 -0
  5. data/Rakefile +8 -0
  6. data/config.rb +79 -0
  7. data/config.ru +4 -0
  8. data/cornerstone.gemspec +22 -0
  9. data/doc_scraper.rb +51 -0
  10. data/game.js +4293 -0
  11. data/lib/assets/javascripts/cornerstone.js +4719 -0
  12. data/lib/cornerstone.rb +11 -0
  13. data/lib/cornerstone/rails.rb +5 -0
  14. data/lib/cornerstone/sprockets.rb +2 -0
  15. data/lib/cornerstone/version.rb +3 -0
  16. data/manifest.json +15 -0
  17. data/pixie.json +12 -0
  18. data/source/javascripts/_cornerstone/_object_extensions.js.coffee +108 -0
  19. data/source/javascripts/_cornerstone/array_extensions.js.coffee +570 -0
  20. data/source/javascripts/_cornerstone/bindable.js.coffee +125 -0
  21. data/source/javascripts/_cornerstone/command_stack.js.coffee +36 -0
  22. data/source/javascripts/_cornerstone/core_object.js.coffee +183 -0
  23. data/source/javascripts/_cornerstone/function_extensions.js.coffee +60 -0
  24. data/source/javascripts/_cornerstone/logging.js.coffee +19 -0
  25. data/source/javascripts/_cornerstone/matrix.js.coffee +337 -0
  26. data/source/javascripts/_cornerstone/number_extensions.js.coffee +491 -0
  27. data/source/javascripts/_cornerstone/point.js.coffee +641 -0
  28. data/source/javascripts/_cornerstone/random.js.coffee +86 -0
  29. data/source/javascripts/_cornerstone/rectangle.js.coffee +35 -0
  30. data/source/javascripts/_cornerstone/string_extensions.js.coffee +232 -0
  31. data/source/javascripts/_cornerstone/stubs.js.coffee +1042 -0
  32. data/source/javascripts/_cornerstone/uuid.js +96 -0
  33. data/source/javascripts/_test/array_extensions.coffee +173 -0
  34. data/source/javascripts/_test/bindable.coffee +68 -0
  35. data/source/javascripts/_test/command_stack.coffee +99 -0
  36. data/source/javascripts/_test/core_object.coffee +95 -0
  37. data/source/javascripts/_test/function_extensions.coffee +50 -0
  38. data/source/javascripts/_test/logging.coffee +7 -0
  39. data/source/javascripts/_test/matrix.coffee +174 -0
  40. data/source/javascripts/_test/number_extensions.coffee +138 -0
  41. data/source/javascripts/_test/object_extensions.coffee +53 -0
  42. data/source/javascripts/_test/point.coffee +196 -0
  43. data/source/javascripts/_test/random.coffee +22 -0
  44. data/source/javascripts/_test/rectangle.coffee +70 -0
  45. data/source/javascripts/_test/string_extensions.coffee +59 -0
  46. data/source/javascripts/cornerstone.js.coffee +1 -0
  47. data/source/javascripts/cornerstone_tests.js.coffee +2 -0
  48. data/source/test.html.haml +13 -0
  49. data/vendor/javascripts/qunit.js +1275 -0
  50. data/vendor/stylesheets/qunit.css.sass +115 -0
  51. metadata +141 -0
@@ -0,0 +1,641 @@
1
+ ( ->
2
+ ###*
3
+ Create a new point with given x and y coordinates. If no arguments are given
4
+ defaults to (0, 0).
5
+
6
+ point = Point()
7
+
8
+ p.x
9
+ # => 0
10
+
11
+ p.y
12
+ # => 0
13
+
14
+ point = Point(-2, 5)
15
+
16
+ p.x
17
+ # => -2
18
+
19
+ p.y
20
+ # => 5
21
+
22
+ @name Point
23
+ @param {Number} [x]
24
+ @param {Number} [y]
25
+ @constructor
26
+ ###
27
+ Point = (x, y) ->
28
+ if Object.isObject(x)
29
+ {x, y} = x
30
+
31
+ __proto__: Point::
32
+
33
+ ###*
34
+ The x coordinate of this point.
35
+ @name x
36
+ @fieldOf Point#
37
+ ###
38
+ x: x || 0
39
+ ###*
40
+ The y coordinate of this point.
41
+ @name y
42
+ @fieldOf Point#
43
+ ###
44
+ y: y || 0
45
+
46
+ Point:: =
47
+ ###*
48
+ Constrain the magnitude of a vector.
49
+
50
+ @name clamp
51
+ @methodOf Point#
52
+ @param {Number} n Maximum value for magnitude.
53
+ @returns {Point} A new point whose magnitude has been clamped to the given value.
54
+ ###
55
+ clamp: (n) ->
56
+ @copy().clamp$(n)
57
+
58
+ clamp$: (n) ->
59
+ if @magnitude() > n
60
+ @norm$(n)
61
+ else
62
+ this
63
+
64
+ ###*
65
+ Creates a copy of this point.
66
+
67
+ @name copy
68
+ @methodOf Point#
69
+ @returns {Point} A new point with the same x and y value as this point.
70
+
71
+ point = Point(1, 1)
72
+ pointCopy = point.copy()
73
+
74
+ point.equal(pointCopy)
75
+ # => true
76
+
77
+ point == pointCopy
78
+ # => false
79
+ ###
80
+ copy: ->
81
+ Point(@x, @y)
82
+
83
+ ###*
84
+ Adds a point to this one and returns the new point. You may
85
+ also use a two argument call like <code>point.add(x, y)</code>
86
+ to add x and y values without a second point object.
87
+
88
+ point = Point(2, 3).add(Point(3, 4))
89
+
90
+ point.x
91
+ # => 5
92
+
93
+ point.y
94
+ # => 7
95
+
96
+ anotherPoint = Point(2, 3).add(3, 4)
97
+
98
+ anotherPoint.x
99
+ # => 5
100
+
101
+ anotherPoint.y
102
+ # => 7
103
+
104
+ @name add
105
+ @methodOf Point#
106
+ @param {Point} other The point to add this point to.
107
+ @returns {Point} A new point, the sum of both.
108
+ ###
109
+ add: (first, second) ->
110
+ @copy().add$(first, second)
111
+
112
+ ###*
113
+ Adds a point to this one, returning a modified point. You may
114
+ also use a two argument call like <code>point.add(x, y)</code>
115
+ to add x and y values without a second point object.
116
+
117
+ point = Point(2, 3)
118
+
119
+ point.x
120
+ # => 2
121
+
122
+ point.y
123
+ # => 3
124
+
125
+ point.add$(Point(3, 4))
126
+
127
+ point.x
128
+ # => 5
129
+
130
+ point.y
131
+ # => 7
132
+
133
+ anotherPoint = Point(2, 3)
134
+ anotherPoint.add$(3, 4)
135
+
136
+ anotherPoint.x
137
+ # => 5
138
+
139
+ anotherPoint.y
140
+ # => 7
141
+
142
+ @name add$
143
+ @methodOf Point#
144
+ @param {Point} other The point to add this point to.
145
+ @returns {Point} The sum of both points.
146
+ ###
147
+ add$: (first, second) ->
148
+ if second?
149
+ @x += first
150
+ @y += second
151
+ else
152
+ @x += first.x
153
+ @y += first.y
154
+
155
+ return this
156
+
157
+ ###*
158
+ Subtracts a point to this one and returns the new point.
159
+
160
+ point = Point(1, 2).subtract(Point(2, 0))
161
+
162
+ point.x
163
+ # => -1
164
+
165
+ point.y
166
+ # => 2
167
+
168
+ anotherPoint = Point(1, 2).subtract(2, 0)
169
+
170
+ anotherPoint.x
171
+ # => -1
172
+
173
+ anotherPoint.y
174
+ # => 2
175
+
176
+ @name subtract
177
+ @methodOf Point#
178
+ @param {Point} other The point to subtract from this point.
179
+ @returns {Point} A new point, this - other.
180
+ ###
181
+ subtract: (first, second) ->
182
+ @copy().subtract$(first, second)
183
+
184
+ ###*
185
+ Subtracts a point to this one and returns the new point.
186
+
187
+ point = Point(1, 2)
188
+
189
+ point.x
190
+ # => 1
191
+
192
+ point.y
193
+ # => 2
194
+
195
+ point.subtract$(Point(2, 0))
196
+
197
+ point.x
198
+ # => -1
199
+
200
+ point.y
201
+ # => 2
202
+
203
+ anotherPoint = Point(1, 2)
204
+ anotherPoint.subtract$(2, 0)
205
+
206
+ anotherPoint.x
207
+ # => -1
208
+
209
+ anotherPoint.y
210
+ # => 2
211
+
212
+ @name subtract$
213
+ @methodOf Point#
214
+ @param {Point} other The point to subtract from this point.
215
+ @returns {Point} The difference of the two points.
216
+ ###
217
+ subtract$: (first, second) ->
218
+ if second?
219
+ @x -= first
220
+ @y -= second
221
+ else
222
+ @x -= first.x
223
+ @y -= first.y
224
+
225
+ return this
226
+
227
+ ###*
228
+ Scale this Point (Vector) by a constant amount.
229
+
230
+ point = Point(5, 6).scale(2)
231
+
232
+ point.x
233
+ # => 10
234
+
235
+ point.y
236
+ # => 12
237
+
238
+ @name scale
239
+ @methodOf Point#
240
+ @param {Number} scalar The amount to scale this point by.
241
+ @returns {Point} A new point, this * scalar.
242
+ ###
243
+ scale: (scalar) ->
244
+ @copy().scale$(scalar)
245
+
246
+ ###*
247
+ Scale this Point (Vector) by a constant amount. Modifies the point in place.
248
+
249
+ point = Point(5, 6)
250
+
251
+ point.x
252
+ # => 5
253
+
254
+ point.y
255
+ # => 6
256
+
257
+ point.scale$(2)
258
+
259
+ point.x
260
+ # => 10
261
+
262
+ point.y
263
+ # => 12
264
+
265
+ @name scale$
266
+ @methodOf Point#
267
+ @param {Number} scalar The amount to scale this point by.
268
+ @returns {Point} this * scalar.
269
+ ###
270
+ scale$: (scalar) ->
271
+ @x *= scalar
272
+ @y *= scalar
273
+
274
+ return this
275
+
276
+ ###*
277
+ The norm of a vector is the unit vector pointing in the same direction. This method
278
+ treats the point as though it is a vector from the origin to (x, y).
279
+
280
+ point = Point(2, 3).norm()
281
+
282
+ point.x
283
+ # => 0.5547001962252291
284
+
285
+ point.y
286
+ # => 0.8320502943378437
287
+
288
+ anotherPoint = Point(2, 3).norm(2)
289
+
290
+ anotherPoint.x
291
+ # => 1.1094003924504583
292
+
293
+ anotherPoint.y
294
+ # => 1.6641005886756874
295
+
296
+ @name norm
297
+ @methodOf Point#
298
+ @returns {Point} The unit vector pointing in the same direction as this vector.
299
+ ###
300
+ norm: (length=1.0) ->
301
+ @copy().norm$(length)
302
+
303
+ ###*
304
+ The norm of a vector is the unit vector pointing in the same direction. This method
305
+ treats the point as though it is a vector from the origin to (x, y). Modifies the point in place.
306
+
307
+ point = Point(2, 3).norm$()
308
+
309
+ point.x
310
+ # => 0.5547001962252291
311
+
312
+ point.y
313
+ # => 0.8320502943378437
314
+
315
+ anotherPoint = Point(2, 3).norm$(2)
316
+
317
+ anotherPoint.x
318
+ # => 1.1094003924504583
319
+
320
+ anotherPoint.y
321
+ # => 1.6641005886756874
322
+
323
+ @name norm$
324
+ @methodOf Point#
325
+ @returns {Point} The unit vector pointing in the same direction as this vector.
326
+ ###
327
+ norm$: (length=1.0) ->
328
+ if m = @length()
329
+ @scale$(length/m)
330
+ else
331
+ this
332
+
333
+ ###*
334
+ Floor the x and y values, returning a new point.
335
+
336
+ point = Point(3.4, 5.8).floor()
337
+
338
+ point.x
339
+ # => 3
340
+
341
+ point.y
342
+ # => 5
343
+
344
+ @name floor
345
+ @methodOf Point#
346
+ @returns {Point} A new point, with x and y values each floored to the largest previous integer.
347
+ ###
348
+ floor: ->
349
+ @copy().floor$()
350
+
351
+ ###*
352
+ Floor the x and y values, returning a modified point.
353
+
354
+ point = Point(3.4, 5.8)
355
+ point.floor$()
356
+
357
+ point.x
358
+ # => 3
359
+
360
+ point.y
361
+ # => 5
362
+
363
+ @name floor$
364
+ @methodOf Point#
365
+ @returns {Point} A modified point, with x and y values each floored to the largest previous integer.
366
+ ###
367
+ floor$: ->
368
+ @x = @x.floor()
369
+ @y = @y.floor()
370
+
371
+ return this
372
+
373
+ ###*
374
+ Determine whether this point is equal to another point.
375
+
376
+ pointA = Point(2, 3)
377
+ pointB = Point(2, 3)
378
+ pointC = Point(4, 5)
379
+
380
+ pointA.equal(pointB)
381
+ # => true
382
+
383
+ pointA.equal(pointC)
384
+ # => false
385
+
386
+ @name equal
387
+ @methodOf Point#
388
+ @param {Point} other The point to check for equality.
389
+ @returns {Boolean} true if the other point has the same x, y coordinates, false otherwise.
390
+ ###
391
+ equal: (other) ->
392
+ @x == other.x && @y == other.y
393
+
394
+ ###*
395
+ Computed the length of this point as though it were a vector from (0,0) to (x,y).
396
+
397
+ point = Point(5, 7)
398
+
399
+ point.length()
400
+ # => 8.602325267042627
401
+
402
+ @name length
403
+ @methodOf Point#
404
+ @returns {Number} The length of the vector from the origin to this point.
405
+ ###
406
+ length: ->
407
+ Math.sqrt(@dot(this))
408
+
409
+ ###*
410
+ Calculate the magnitude of this Point (Vector).
411
+
412
+ point = Point(5, 7)
413
+
414
+ point.magnitude()
415
+ # => 8.602325267042627
416
+
417
+ @name magnitude
418
+ @methodOf Point#
419
+ @returns {Number} The magnitude of this point as if it were a vector from (0, 0) -> (x, y).
420
+ ###
421
+ magnitude: ->
422
+ @length()
423
+
424
+ ###*
425
+ Returns the direction in radians of this point from the origin.
426
+
427
+ point = Point(0, 1)
428
+
429
+ point.direction()
430
+ # => 1.5707963267948966 # Math.PI / 2
431
+
432
+ @name direction
433
+ @methodOf Point#
434
+ @returns {Number} The direction in radians of this point from the origin
435
+ ###
436
+ direction: ->
437
+ Math.atan2(@y, @x)
438
+
439
+ ###*
440
+ Calculate the dot product of this point and another point (Vector).
441
+ @name dot
442
+ @methodOf Point#
443
+ @param {Point} other The point to dot with this point.
444
+ @returns {Number} The dot product of this point dot other as a scalar value.
445
+ ###
446
+ dot: (other) ->
447
+ @x * other.x + @y * other.y
448
+
449
+ ###*
450
+ Calculate the cross product of this point and another point (Vector).
451
+ Usually cross products are thought of as only applying to three dimensional vectors,
452
+ but z can be treated as zero. The result of this method is interpreted as the magnitude
453
+ of the vector result of the cross product between [x1, y1, 0] x [x2, y2, 0]
454
+ perpendicular to the xy plane.
455
+
456
+ @name cross
457
+ @methodOf Point#
458
+ @param {Point} other The point to cross with this point.
459
+ @returns {Number} The cross product of this point with the other point as scalar value.
460
+ ###
461
+ cross: (other) ->
462
+ @x * other.y - other.x * @y
463
+
464
+ ###*
465
+ Compute the Euclidean distance between this point and another point.
466
+
467
+ pointA = Point(2, 3)
468
+ pointB = Point(9, 2)
469
+
470
+ pointA.distance(pointB)
471
+ # => 7.0710678118654755 # Math.sqrt(50)
472
+
473
+ @name distance
474
+ @methodOf Point#
475
+ @param {Point} other The point to compute the distance to.
476
+ @returns {Number} The distance between this point and another point.
477
+ ###
478
+ distance: (other) ->
479
+ Point.distance(this, other)
480
+
481
+ ###*
482
+ @name toString
483
+ @methodOf Point#
484
+ @returns {String} A string representation of this point.
485
+ ###
486
+ toString: ->
487
+ "Point(#{@x}, #{@y})"
488
+
489
+ ###*
490
+ Compute the Euclidean distance between two points.
491
+
492
+ pointA = Point(2, 3)
493
+ pointB = Point(9, 2)
494
+
495
+ Point.distance(pointA, pointB)
496
+ # => 7.0710678118654755 # Math.sqrt(50)
497
+
498
+ @name distance
499
+ @fieldOf Point
500
+ @param {Point} p1
501
+ @param {Point} p2
502
+ @returns {Number} The Euclidean distance between two points.
503
+ ###
504
+ Point.distance = (p1, p2) ->
505
+ Math.sqrt(Point.distanceSquared(p1, p2))
506
+
507
+ ###*
508
+ pointA = Point(2, 3)
509
+ pointB = Point(9, 2)
510
+
511
+ Point.distanceSquared(pointA, pointB)
512
+ # => 50
513
+
514
+ @name distanceSquared
515
+ @fieldOf Point
516
+ @param {Point} p1
517
+ @param {Point} p2
518
+ @returns {Number} The square of the Euclidean distance between two points.
519
+ ###
520
+ Point.distanceSquared = (p1, p2) ->
521
+ Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)
522
+
523
+ ###*
524
+ @name interpolate
525
+ @fieldOf Point
526
+
527
+ @param {Point} p1
528
+ @param {Point} p2
529
+ @param {Number} t
530
+ @returns {Point} A point along the path from p1 to p2
531
+ ###
532
+ Point.interpolate = (p1, p2, t) ->
533
+ p2.subtract(p1).scale(t).add(p1)
534
+
535
+ ###*
536
+ Construct a point on the unit circle for the given angle.
537
+
538
+ point = Point.fromAngle(Math.PI / 2)
539
+
540
+ point.x
541
+ # => 0
542
+
543
+ point.y
544
+ # => 1
545
+
546
+ @name fromAngle
547
+ @fieldOf Point
548
+ @param {Number} angle The angle in radians
549
+ @returns {Point} The point on the unit circle.
550
+ ###
551
+ Point.fromAngle = (angle) ->
552
+ Point(Math.cos(angle), Math.sin(angle))
553
+
554
+ ###*
555
+ If you have two dudes, one standing at point p1, and the other
556
+ standing at point p2, then this method will return the direction
557
+ that the dude standing at p1 will need to face to look at p2.
558
+
559
+ p1 = Point(0, 0)
560
+ p2 = Point(7, 3)
561
+
562
+ Point.direction(p1, p2)
563
+ # => 0.40489178628508343
564
+
565
+ @name direction
566
+ @fieldOf Point
567
+ @param {Point} p1 The starting point.
568
+ @param {Point} p2 The ending point.
569
+ @returns {Number} The direction from p1 to p2 in radians.
570
+ ###
571
+ Point.direction = (p1, p2) ->
572
+ Math.atan2(
573
+ p2.y - p1.y,
574
+ p2.x - p1.x
575
+ )
576
+
577
+ ###*
578
+ The centroid of a set of points is their arithmetic mean.
579
+
580
+ @name centroid
581
+ @methodOf Point
582
+ @param points... The points to find the centroid of.
583
+ ###
584
+ Point.centroid = (points...) ->
585
+ points.inject Point(0, 0), (sumPoint, point) ->
586
+ sumPoint.add(point)
587
+ .scale(1/points.length)
588
+
589
+ ###*
590
+ Generate a random point on the unit circle.
591
+
592
+ @returns {Point} A random point on the unit circle.
593
+ ###
594
+ Point.random = ->
595
+ Point.fromAngle(Random.angle())
596
+
597
+ ###*
598
+ @name ZERO
599
+ @fieldOf Point
600
+ @returns {Point} The point (0, 0)
601
+ ###
602
+ Point.ZERO = Point(0, 0)
603
+
604
+ ###*
605
+ @name LEFT
606
+ @fieldOf Point
607
+ @returns {Point} The point (-1, 0)
608
+ ###
609
+ Point.LEFT = Point(-1, 0)
610
+
611
+ ###*
612
+ @name RIGHT
613
+ @fieldOf Point
614
+ @returns {Point} The point (1, 0)
615
+ ###
616
+ Point.RIGHT = Point(1, 0)
617
+
618
+ ###*
619
+ @name UP
620
+ @fieldOf Point
621
+ @returns {Point} The point (0, -1)
622
+ ###
623
+ Point.UP = Point(0, -1)
624
+
625
+ ###*
626
+ @name DOWN
627
+ @fieldOf Point
628
+ @returns {Point} The point (0, 1)
629
+ ###
630
+ Point.DOWN = Point(0, 1)
631
+
632
+ if Object.freeze
633
+ Object.freeze Point.ZERO
634
+ Object.freeze Point.LEFT
635
+ Object.freeze Point.RIGHT
636
+ Object.freeze Point.UP
637
+ Object.freeze Point.DOWN
638
+
639
+ (exports ? this)["Point"] = Point
640
+ )()
641
+