cornerstone-source 0.1.12 → 0.1.13
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.
- data/Gemfile +8 -0
- data/README.md +24 -6
- data/cornerstone-source.gemspec +1 -2
- data/game.js +3331 -0
- data/lib/cornerstone-source.rb +9 -2
- data/lib/cornerstone-source/version.rb +1 -1
- data/pixie.json +13 -0
- data/source/{object_extensions.js.coffee → _object_extensions.coffee} +0 -0
- data/source/{array_extensions.js.coffee → array_extensions.coffee} +54 -33
- data/source/{bindable.js.coffee → bindable.coffee} +0 -0
- data/source/{command_stack.js.coffee → command_stack.coffee} +0 -0
- data/source/{core_object.js.coffee → core_object.coffee} +0 -0
- data/source/{function_extensions.js.coffee → function_extensions.coffee} +12 -5
- data/source/{logging.js.coffee → logging.coffee} +0 -0
- data/source/{matrix.js.coffee → matrix.coffee} +0 -0
- data/source/{number_extensions.js.coffee → number_extensions.coffee} +69 -67
- data/source/{point.js.coffee → point.coffee} +88 -74
- data/source/{random.js.coffee → random.coffee} +0 -0
- data/source/{rectangle.js.coffee → rectangle.coffee} +0 -0
- data/source/{string_extensions.js.coffee → string_extensions.coffee} +0 -0
- data/test/array_extensions.coffee +45 -0
- data/test/function_extensions.coffee +7 -1
- metadata +65 -77
data/lib/cornerstone-source.rb
CHANGED
@@ -2,8 +2,15 @@ require 'sprockets'
|
|
2
2
|
|
3
3
|
module Cornerstone
|
4
4
|
module Source
|
5
|
-
|
5
|
+
if defined? ::Rails
|
6
|
+
class Engine < ::Rails::Engine
|
7
|
+
config.paths['app/assets'] = "source"
|
8
|
+
end
|
9
|
+
else
|
10
|
+
root_dir = File.expand_path(File.dirname(File.dirname(__FILE__)))
|
11
|
+
asset_dir = File.join(root_dir, "source")
|
6
12
|
|
7
|
-
|
13
|
+
::Sprockets.append_path asset_dir
|
14
|
+
end
|
8
15
|
end
|
9
16
|
end
|
data/pixie.json
ADDED
File without changes
|
@@ -23,7 +23,7 @@ Returns a copy of the array without null and undefined values.
|
|
23
23
|
@returns {Array} A new array that contains only the non-null values.
|
24
24
|
###
|
25
25
|
Array::compact = ->
|
26
|
-
|
26
|
+
@select (element) ->
|
27
27
|
element?
|
28
28
|
|
29
29
|
###*
|
@@ -46,7 +46,7 @@ the same objects.
|
|
46
46
|
@returns {Array} A new array that is a copy of the array
|
47
47
|
###
|
48
48
|
Array::copy = ->
|
49
|
-
|
49
|
+
@concat()
|
50
50
|
|
51
51
|
###*
|
52
52
|
Empties the array of its contents. It is modified in place.
|
@@ -61,7 +61,7 @@ Empties the array of its contents. It is modified in place.
|
|
61
61
|
@returns {Array} this, now emptied.
|
62
62
|
###
|
63
63
|
Array::clear = ->
|
64
|
-
|
64
|
+
@length = 0
|
65
65
|
|
66
66
|
return this
|
67
67
|
|
@@ -81,7 +81,7 @@ Flatten out an array of arrays into a single array of elements.
|
|
81
81
|
@returns {Array} A new array with all the sub-arrays flattened to the top.
|
82
82
|
###
|
83
83
|
Array::flatten = ->
|
84
|
-
|
84
|
+
@inject [], (a, b) ->
|
85
85
|
a.concat b
|
86
86
|
|
87
87
|
###*
|
@@ -101,7 +101,7 @@ and return a new array containing the results of the invocation.
|
|
101
101
|
@returns {Array} A new array containing the results of invoking the named method on each element.
|
102
102
|
###
|
103
103
|
Array::invoke = (method, args...) ->
|
104
|
-
|
104
|
+
@map (element) ->
|
105
105
|
element[method].apply(element, args)
|
106
106
|
|
107
107
|
###*
|
@@ -115,7 +115,7 @@ Randomly select an element from the array.
|
|
115
115
|
@returns {Object} A random element from an array
|
116
116
|
###
|
117
117
|
Array::rand = ->
|
118
|
-
this[rand(
|
118
|
+
this[rand(@length)]
|
119
119
|
|
120
120
|
###*
|
121
121
|
Remove the first occurrence of the given object from the array if it is
|
@@ -134,10 +134,10 @@ present. The array is modified in place.
|
|
134
134
|
@returns {Object} The removed object if present otherwise undefined.
|
135
135
|
###
|
136
136
|
Array::remove = (object) ->
|
137
|
-
index =
|
137
|
+
index = @indexOf(object)
|
138
138
|
|
139
139
|
if index >= 0
|
140
|
-
|
140
|
+
@splice(index, 1)[0]
|
141
141
|
else
|
142
142
|
undefined
|
143
143
|
|
@@ -156,7 +156,7 @@ Returns true if the element is present in the array.
|
|
156
156
|
@returns {Boolean} true if the element is in the array, false otherwise.
|
157
157
|
###
|
158
158
|
Array::include = (element) ->
|
159
|
-
|
159
|
+
@indexOf(element) != -1
|
160
160
|
|
161
161
|
###*
|
162
162
|
Call the given iterator once for each element in the array,
|
@@ -185,8 +185,8 @@ third argument.
|
|
185
185
|
@returns {Array} this to enable method chaining.
|
186
186
|
###
|
187
187
|
Array::each = (iterator, context) ->
|
188
|
-
if
|
189
|
-
|
188
|
+
if @forEach
|
189
|
+
@forEach iterator, context
|
190
190
|
else
|
191
191
|
for element, i in this
|
192
192
|
iterator.call context, element, i, this
|
@@ -234,7 +234,7 @@ Call the given iterator once for each pair of objects in the array.
|
|
234
234
|
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
|
235
235
|
###
|
236
236
|
Array::eachPair = (iterator, context) ->
|
237
|
-
length =
|
237
|
+
length = @length
|
238
238
|
i = 0
|
239
239
|
while i < length
|
240
240
|
a = this[i]
|
@@ -262,7 +262,7 @@ as the second argument. Additional arguments are passed similar to
|
|
262
262
|
@returns {Array} this
|
263
263
|
###
|
264
264
|
Array::eachWithObject = (object, iterator, context) ->
|
265
|
-
|
265
|
+
@each (element, i, self) ->
|
266
266
|
iterator.call context, element, object, i, self
|
267
267
|
|
268
268
|
return object
|
@@ -290,11 +290,11 @@ passed as in each.
|
|
290
290
|
###
|
291
291
|
Array::eachSlice = (n, iterator, context) ->
|
292
292
|
if n > 0
|
293
|
-
len = (
|
293
|
+
len = (@length / n).floor()
|
294
294
|
i = -1
|
295
295
|
|
296
296
|
while ++i < len
|
297
|
-
iterator.call(context,
|
297
|
+
iterator.call(context, @slice(i*n, (i+1)*n), i*n, this)
|
298
298
|
|
299
299
|
return this
|
300
300
|
|
@@ -312,10 +312,8 @@ is fed to the input of the second and so on until the final processed output is
|
|
312
312
|
@returns {Object} The result of processing the input by each function in the array.
|
313
313
|
###
|
314
314
|
Array::pipeline = (input) ->
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
return input
|
315
|
+
@inject input, (input, fn) ->
|
316
|
+
fn(input)
|
319
317
|
|
320
318
|
###*
|
321
319
|
Returns a new array with the elements all shuffled up.
|
@@ -334,7 +332,7 @@ Returns a new array with the elements all shuffled up.
|
|
334
332
|
Array::shuffle = ->
|
335
333
|
shuffledArray = []
|
336
334
|
|
337
|
-
|
335
|
+
@each (element) ->
|
338
336
|
shuffledArray.splice(rand(shuffledArray.length + 1), 0, element)
|
339
337
|
|
340
338
|
return shuffledArray
|
@@ -363,7 +361,7 @@ Returns the last element of the array, undefined if the array is empty.
|
|
363
361
|
@returns {Object} The last element, or undefined if the array is empty.
|
364
362
|
###
|
365
363
|
Array::last = ->
|
366
|
-
this[
|
364
|
+
this[@length - 1]
|
367
365
|
|
368
366
|
###*
|
369
367
|
Returns an object containing the extremes of this array.
|
@@ -376,13 +374,11 @@ Returns an object containing the extremes of this array.
|
|
376
374
|
@param {Function} [fn] An optional funtion used to evaluate each element to calculate its value for determining extremes.
|
377
375
|
@returns {Object} {min: minElement, max: maxElement}
|
378
376
|
###
|
379
|
-
Array::extremes = (fn) ->
|
380
|
-
fn ||= (n) -> n
|
381
|
-
|
377
|
+
Array::extremes = (fn=Function.identity) ->
|
382
378
|
min = max = undefined
|
383
379
|
minResult = maxResult = undefined
|
384
380
|
|
385
|
-
|
381
|
+
@each (object) ->
|
386
382
|
result = fn(object)
|
387
383
|
|
388
384
|
if min?
|
@@ -404,6 +400,31 @@ Array::extremes = (fn) ->
|
|
404
400
|
min: min
|
405
401
|
max: max
|
406
402
|
|
403
|
+
Array::maxima = (valueFunction=Function.identity) ->
|
404
|
+
@inject([-Infinity, []], (memo, item) ->
|
405
|
+
value = valueFunction(item)
|
406
|
+
[maxValue, maxItems] = memo
|
407
|
+
|
408
|
+
if value > maxValue
|
409
|
+
[value, [item]]
|
410
|
+
else if value is maxValue
|
411
|
+
[value, maxItems.concat(item)]
|
412
|
+
else
|
413
|
+
memo
|
414
|
+
).last()
|
415
|
+
|
416
|
+
Array::maximum = (valueFunction) ->
|
417
|
+
@maxima(valueFunction).first()
|
418
|
+
|
419
|
+
Array::minima = (valueFunction=Function.identity) ->
|
420
|
+
inverseFn = (x) ->
|
421
|
+
-valueFunction(x)
|
422
|
+
|
423
|
+
@maxima(inverseFn)
|
424
|
+
|
425
|
+
Array::minimum = (valueFunction) ->
|
426
|
+
@minima(valueFunction).first()
|
427
|
+
|
407
428
|
###*
|
408
429
|
Pretend the array is a circle and grab a new array containing length elements.
|
409
430
|
If length is not given return the element at start, again assuming the array
|
@@ -461,7 +482,7 @@ Array::partition = (iterator, context) ->
|
|
461
482
|
trueCollection = []
|
462
483
|
falseCollection = []
|
463
484
|
|
464
|
-
|
485
|
+
@each (element) ->
|
465
486
|
if iterator.call(context, element)
|
466
487
|
trueCollection.push element
|
467
488
|
else
|
@@ -479,7 +500,7 @@ Return the group of elements for which the return value of the iterator is true.
|
|
479
500
|
@returns {Array} An array containing the elements for which the iterator returned true.
|
480
501
|
###
|
481
502
|
Array::select = (iterator, context) ->
|
482
|
-
return
|
503
|
+
return @partition(iterator, context)[0]
|
483
504
|
|
484
505
|
###*
|
485
506
|
Return the group of elements that are not in the passed in set.
|
@@ -493,7 +514,7 @@ Return the group of elements that are not in the passed in set.
|
|
493
514
|
@returns {Array} An array containing the elements that are not passed in.
|
494
515
|
###
|
495
516
|
Array::without = (values) ->
|
496
|
-
|
517
|
+
@reject (element) ->
|
497
518
|
values.include(element)
|
498
519
|
|
499
520
|
###*
|
@@ -506,7 +527,7 @@ Return the group of elements for which the return value of the iterator is false
|
|
506
527
|
@returns {Array} An array containing the elements for which the iterator returned false.
|
507
528
|
###
|
508
529
|
Array::reject = (iterator, context) ->
|
509
|
-
|
530
|
+
@partition(iterator, context)[1]
|
510
531
|
|
511
532
|
###*
|
512
533
|
Combines all elements of the array by applying a binary operation.
|
@@ -518,7 +539,7 @@ value (memo) and the element.
|
|
518
539
|
@returns {Object} The result of a
|
519
540
|
###
|
520
541
|
Array::inject = (initial, iterator) ->
|
521
|
-
|
542
|
+
@each (element) ->
|
522
543
|
initial = iterator(initial, element)
|
523
544
|
|
524
545
|
return initial
|
@@ -534,7 +555,7 @@ Add all the elements in the array.
|
|
534
555
|
@returns {Number} The sum of the elements in the array.
|
535
556
|
###
|
536
557
|
Array::sum = ->
|
537
|
-
|
558
|
+
@inject 0, (sum, n) ->
|
538
559
|
sum + n
|
539
560
|
|
540
561
|
###*
|
@@ -548,7 +569,7 @@ Multiply all the elements in the array.
|
|
548
569
|
@returns {Number} The product of the elements in the array.
|
549
570
|
###
|
550
571
|
Array::product = ->
|
551
|
-
|
572
|
+
@inject 1, (product, n) ->
|
552
573
|
product * n
|
553
574
|
|
554
575
|
###*
|
@@ -562,7 +583,7 @@ Merges together the values of each of the arrays with the values at the correspo
|
|
562
583
|
@returns {Array} Array groupings whose values are arranged by their positions in the original input arrays.
|
563
584
|
###
|
564
585
|
Array::zip = (args...) ->
|
565
|
-
|
586
|
+
@map (element, index) ->
|
566
587
|
output = args.map (arr) ->
|
567
588
|
arr[index]
|
568
589
|
|
File without changes
|
File without changes
|
File without changes
|
@@ -11,11 +11,11 @@ Function::once = ->
|
|
11
11
|
return memo = func.apply(this, arguments)
|
12
12
|
|
13
13
|
###*
|
14
|
-
Calling a debounced function will postpone its execution until after
|
15
|
-
wait milliseconds have elapsed since the last time the function was
|
16
|
-
invoked. Useful for implementing behavior that should only happen after
|
17
|
-
the input has stopped arriving. For example: rendering a preview of a
|
18
|
-
Markdown comment, recalculating a layout after the window has stopped
|
14
|
+
Calling a debounced function will postpone its execution until after
|
15
|
+
wait milliseconds have elapsed since the last time the function was
|
16
|
+
invoked. Useful for implementing behavior that should only happen after
|
17
|
+
the input has stopped arriving. For example: rendering a preview of a
|
18
|
+
Markdown comment, recalculating a layout after the window has stopped
|
19
19
|
being resized...
|
20
20
|
|
21
21
|
lazyLayout = calculateLayout.debounce(300)
|
@@ -58,3 +58,10 @@ Function::delay = (wait, args...) ->
|
|
58
58
|
Function::defer = (args...) ->
|
59
59
|
this.delay.apply this, [1].concat(args)
|
60
60
|
|
61
|
+
Object.extend Function,
|
62
|
+
|
63
|
+
identity: (x) ->
|
64
|
+
x
|
65
|
+
|
66
|
+
noop: ->
|
67
|
+
|
File without changes
|
File without changes
|
@@ -1,4 +1,4 @@
|
|
1
|
-
###*
|
1
|
+
###*
|
2
2
|
Returns the absolute value of this number.
|
3
3
|
|
4
4
|
(-4).abs()
|
@@ -8,18 +8,16 @@ Returns the absolute value of this number.
|
|
8
8
|
@methodOf Number#
|
9
9
|
@returns {Number} The absolute value of the number.
|
10
10
|
###
|
11
|
-
Number::abs = () ->
|
12
|
-
Math.abs(this)
|
13
11
|
|
14
12
|
###*
|
15
13
|
Returns the mathematical ceiling of this number.
|
16
14
|
|
17
|
-
4.9.ceil()
|
15
|
+
4.9.ceil()
|
18
16
|
# => 5
|
19
|
-
|
17
|
+
|
20
18
|
4.2.ceil()
|
21
19
|
# => 5
|
22
|
-
|
20
|
+
|
23
21
|
(-1.2).ceil()
|
24
22
|
# => -1
|
25
23
|
|
@@ -27,18 +25,16 @@ Returns the mathematical ceiling of this number.
|
|
27
25
|
@methodOf Number#
|
28
26
|
@returns {Number} The number truncated to the nearest integer of greater than or equal value.
|
29
27
|
###
|
30
|
-
Number::ceil = ->
|
31
|
-
Math.ceil(this)
|
32
28
|
|
33
29
|
###*
|
34
30
|
Returns the mathematical floor of this number.
|
35
31
|
|
36
32
|
4.9.floor()
|
37
33
|
# => 4
|
38
|
-
|
34
|
+
|
39
35
|
4.2.floor()
|
40
36
|
# => 4
|
41
|
-
|
37
|
+
|
42
38
|
(-1.2).floor()
|
43
39
|
# => -2
|
44
40
|
|
@@ -46,15 +42,13 @@ Returns the mathematical floor of this number.
|
|
46
42
|
@methodOf Number#
|
47
43
|
@returns {Number} The number truncated to the nearest integer of less than or equal value.
|
48
44
|
###
|
49
|
-
Number::floor = ->
|
50
|
-
Math.floor(this)
|
51
45
|
|
52
46
|
###*
|
53
47
|
Returns this number rounded to the nearest integer.
|
54
48
|
|
55
49
|
4.5.round()
|
56
50
|
# => 5
|
57
|
-
|
51
|
+
|
58
52
|
4.4.round()
|
59
53
|
# => 4
|
60
54
|
|
@@ -62,14 +56,16 @@ Returns this number rounded to the nearest integer.
|
|
62
56
|
@methodOf Number#
|
63
57
|
@returns {Number} The number rounded to the nearest integer.
|
64
58
|
###
|
65
|
-
|
66
|
-
|
59
|
+
|
60
|
+
["abs", "ceil", "floor", "round"].each (method) ->
|
61
|
+
Number::[method] = ->
|
62
|
+
Math[method](this)
|
67
63
|
|
68
64
|
###*
|
69
65
|
Get a bunch of points equally spaced around the unit circle.
|
70
66
|
|
71
67
|
4.circularPoints (p) ->
|
72
|
-
|
68
|
+
|
73
69
|
# p gets Point(1, 0), Point(0, 1), Point(-1, 0), Point(0, -1)
|
74
70
|
|
75
71
|
@name circularPoint
|
@@ -130,10 +126,10 @@ Get the sign of this number as an integer (1, -1, or 0).
|
|
130
126
|
|
131
127
|
(-5).sign()
|
132
128
|
# => -1
|
133
|
-
|
129
|
+
|
134
130
|
0.sign()
|
135
131
|
# => 0
|
136
|
-
|
132
|
+
|
137
133
|
5.sign()
|
138
134
|
# => 1
|
139
135
|
|
@@ -146,7 +142,7 @@ Number::sign = ->
|
|
146
142
|
1
|
147
143
|
else if this < 0
|
148
144
|
-1
|
149
|
-
else
|
145
|
+
else
|
150
146
|
0
|
151
147
|
|
152
148
|
###*
|
@@ -154,51 +150,48 @@ Returns true if this number is even (evenly divisible by 2).
|
|
154
150
|
|
155
151
|
2.even()
|
156
152
|
# => true
|
157
|
-
|
153
|
+
|
158
154
|
3.even()
|
159
155
|
# => false
|
160
|
-
|
156
|
+
|
161
157
|
0.even()
|
162
|
-
# => true
|
158
|
+
# => true
|
163
159
|
|
164
160
|
@name even
|
165
161
|
@methodOf Number#
|
166
162
|
@returns {Boolean} true if this number is an even integer, false otherwise.
|
167
163
|
###
|
168
164
|
Number::even = ->
|
169
|
-
|
165
|
+
@mod(2) is 0
|
170
166
|
|
171
167
|
###*
|
172
168
|
Returns true if this number is odd (has remainder of 1 when divided by 2).
|
173
169
|
|
174
170
|
2.odd()
|
175
171
|
# => false
|
176
|
-
|
172
|
+
|
177
173
|
3.odd()
|
178
174
|
# => true
|
179
|
-
|
175
|
+
|
180
176
|
0.odd()
|
181
|
-
# => false
|
177
|
+
# => false
|
182
178
|
|
183
179
|
@name odd
|
184
180
|
@methodOf Number#
|
185
181
|
@returns {Boolean} true if this number is an odd integer, false otherwise.
|
186
182
|
###
|
187
183
|
Number::odd = ->
|
188
|
-
|
189
|
-
this % 2 == 1
|
190
|
-
else
|
191
|
-
this % 2 == -1
|
184
|
+
@mod(2) is 1
|
192
185
|
|
193
186
|
###*
|
194
|
-
Calls iterator the specified number of times, passing in the number of the
|
195
|
-
current iteration as a parameter: 0 on first call, 1 on the second call, etc.
|
187
|
+
Calls iterator the specified number of times, passing in the number of the
|
188
|
+
current iteration as a parameter: 0 on first call, 1 on the second call, etc.
|
196
189
|
|
197
190
|
output = []
|
198
|
-
|
191
|
+
|
199
192
|
5.times (n) ->
|
200
193
|
output.push(n)
|
201
|
-
|
194
|
+
|
202
195
|
output
|
203
196
|
# => [0, 1, 2, 3, 4]
|
204
197
|
|
@@ -217,15 +210,15 @@ Number::times = (iterator, context) ->
|
|
217
210
|
return i
|
218
211
|
|
219
212
|
###*
|
220
|
-
Returns the the nearest grid resolution less than or equal to the number.
|
213
|
+
Returns the the nearest grid resolution less than or equal to the number.
|
221
214
|
|
222
|
-
7.snap(8)
|
215
|
+
7.snap(8)
|
223
216
|
# => 0
|
224
|
-
|
225
|
-
4.snap(8)
|
217
|
+
|
218
|
+
4.snap(8)
|
226
219
|
# => 0
|
227
|
-
|
228
|
-
12.snap(8)
|
220
|
+
|
221
|
+
12.snap(8)
|
229
222
|
# => 8
|
230
223
|
|
231
224
|
@name snap
|
@@ -246,7 +239,7 @@ Floors the number for purposes of factorization.
|
|
246
239
|
|
247
240
|
60.primeFactors()
|
248
241
|
# => [2, 2, 3, 5]
|
249
|
-
|
242
|
+
|
250
243
|
37.primeFactors()
|
251
244
|
# => [37]
|
252
245
|
|
@@ -257,7 +250,7 @@ Floors the number for purposes of factorization.
|
|
257
250
|
Number::primeFactors = ->
|
258
251
|
factors = []
|
259
252
|
|
260
|
-
n =
|
253
|
+
n = @floor()
|
261
254
|
|
262
255
|
if n == 0
|
263
256
|
return undefined
|
@@ -283,15 +276,15 @@ Number::primeFactors = ->
|
|
283
276
|
return factors
|
284
277
|
|
285
278
|
###*
|
286
|
-
Returns the two character hexidecimal
|
279
|
+
Returns the two character hexidecimal
|
287
280
|
representation of numbers 0 through 255.
|
288
281
|
|
289
282
|
255.toColorPart()
|
290
283
|
# => "ff"
|
291
|
-
|
284
|
+
|
292
285
|
0.toColorPart()
|
293
286
|
# => "00"
|
294
|
-
|
287
|
+
|
295
288
|
200.toColorPart()
|
296
289
|
# => "c8"
|
297
290
|
|
@@ -300,7 +293,7 @@ representation of numbers 0 through 255.
|
|
300
293
|
@returns {String} Hexidecimal representation of the number
|
301
294
|
###
|
302
295
|
Number::toColorPart = ->
|
303
|
-
s = parseInt(
|
296
|
+
s = parseInt(@clamp(0, 255), 10).toString(16)
|
304
297
|
|
305
298
|
if s.length == 1
|
306
299
|
s = '0' + s
|
@@ -312,14 +305,14 @@ Returns a number that is maxDelta closer to target.
|
|
312
305
|
|
313
306
|
255.approach(0, 5)
|
314
307
|
# => 250
|
315
|
-
|
308
|
+
|
316
309
|
5.approach(0, 10)
|
317
310
|
# => 0
|
318
311
|
|
319
312
|
@name approach
|
320
313
|
@methodOf Number#
|
321
314
|
@returns {Number} A number maxDelta toward target
|
322
|
-
###
|
315
|
+
###
|
323
316
|
Number::approach = (target, maxDelta) ->
|
324
317
|
(target - this).clamp(-maxDelta, maxDelta) + this
|
325
318
|
|
@@ -332,9 +325,9 @@ Returns a number that is closer to the target by the ratio.
|
|
332
325
|
@name approachByRatio
|
333
326
|
@methodOf Number#
|
334
327
|
@returns {Number} A number toward target by the ratio
|
335
|
-
###
|
328
|
+
###
|
336
329
|
Number::approachByRatio = (target, ratio) ->
|
337
|
-
|
330
|
+
@approach(target, this * ratio)
|
338
331
|
|
339
332
|
###*
|
340
333
|
Returns a number that is closer to the target angle by the delta.
|
@@ -345,7 +338,7 @@ Returns a number that is closer to the target angle by the delta.
|
|
345
338
|
@name approachRotation
|
346
339
|
@methodOf Number#
|
347
340
|
@returns {Number} A number toward the target angle by maxDelta
|
348
|
-
###
|
341
|
+
###
|
349
342
|
Number::approachRotation = (target, maxDelta) ->
|
350
343
|
while target > this + Math.PI
|
351
344
|
target -= Math.TAU
|
@@ -358,7 +351,7 @@ Number::approachRotation = (target, maxDelta) ->
|
|
358
351
|
###*
|
359
352
|
Constrains a rotation to between -PI and PI.
|
360
353
|
|
361
|
-
(9/4 * Math.PI).constrainRotation()
|
354
|
+
(9/4 * Math.PI).constrainRotation()
|
362
355
|
# => 0.7853981633974483 # this is (1/4) * Math.PI
|
363
356
|
|
364
357
|
@name constrainRotation
|
@@ -376,6 +369,15 @@ Number::constrainRotation = ->
|
|
376
369
|
|
377
370
|
return target
|
378
371
|
|
372
|
+
# TODO Test and document
|
373
|
+
Number::truncate = ->
|
374
|
+
if this > 0
|
375
|
+
@floor()
|
376
|
+
else if this < 0
|
377
|
+
@ceil()
|
378
|
+
else
|
379
|
+
this
|
380
|
+
|
379
381
|
###*
|
380
382
|
The mathematical d operator. Useful for simulating dice rolls.
|
381
383
|
|
@@ -386,7 +388,7 @@ The mathematical d operator. Useful for simulating dice rolls.
|
|
386
388
|
Number::d = (sides) ->
|
387
389
|
sum = 0
|
388
390
|
|
389
|
-
|
391
|
+
@times ->
|
390
392
|
sum += rand(sides) + 1
|
391
393
|
|
392
394
|
return sum
|
@@ -396,7 +398,7 @@ Utility method to convert a number to a duration of seconds.
|
|
396
398
|
|
397
399
|
3.seconds
|
398
400
|
# => 3000
|
399
|
-
|
401
|
+
|
400
402
|
setTimout doSometing, 3.seconds
|
401
403
|
|
402
404
|
@name seconds
|
@@ -405,12 +407,12 @@ Utility method to convert a number to a duration of seconds.
|
|
405
407
|
###
|
406
408
|
unless 5.seconds
|
407
409
|
Object.defineProperty Number::, 'seconds',
|
408
|
-
get: ->
|
410
|
+
get: ->
|
409
411
|
this * 1000
|
410
412
|
|
411
413
|
unless 1.second
|
412
414
|
Object.defineProperty Number::, 'second',
|
413
|
-
get: ->
|
415
|
+
get: ->
|
414
416
|
this * 1000
|
415
417
|
|
416
418
|
###*
|
@@ -418,7 +420,7 @@ Utility method to convert a number to an amount of rotations.
|
|
418
420
|
|
419
421
|
0.5.rotations
|
420
422
|
# => 3.141592653589793
|
421
|
-
|
423
|
+
|
422
424
|
I.rotation = 0.25.rotations
|
423
425
|
|
424
426
|
@name rotations
|
@@ -427,12 +429,12 @@ Utility method to convert a number to an amount of rotations.
|
|
427
429
|
###
|
428
430
|
unless 5.rotations
|
429
431
|
Object.defineProperty Number::, 'rotations',
|
430
|
-
get: ->
|
432
|
+
get: ->
|
431
433
|
this * Math.TAU
|
432
434
|
|
433
435
|
unless 1.rotation
|
434
436
|
Object.defineProperty Number::, 'rotation',
|
435
|
-
get: ->
|
437
|
+
get: ->
|
436
438
|
this * Math.TAU
|
437
439
|
|
438
440
|
###*
|
@@ -440,9 +442,9 @@ Utility method to convert a number to an amount of rotations.
|
|
440
442
|
|
441
443
|
0.5.turns
|
442
444
|
# => 3.141592653589793
|
443
|
-
|
445
|
+
|
444
446
|
I.rotation = 0.25.turns
|
445
|
-
|
447
|
+
|
446
448
|
1.turn # => Math.TAU (aka 2 * Math.PI)
|
447
449
|
|
448
450
|
@name turns
|
@@ -452,12 +454,12 @@ Utility method to convert a number to an amount of rotations.
|
|
452
454
|
###
|
453
455
|
unless 5.turns
|
454
456
|
Object.defineProperty Number.prototype, 'turns',
|
455
|
-
get: ->
|
457
|
+
get: ->
|
456
458
|
this * Math.TAU
|
457
459
|
|
458
460
|
unless 1.turn
|
459
461
|
Object.defineProperty Number.prototype, 'turn',
|
460
|
-
get: ->
|
462
|
+
get: ->
|
461
463
|
this * Math.TAU
|
462
464
|
|
463
465
|
###*
|
@@ -465,7 +467,7 @@ Utility method to convert a number to an amount of degrees.
|
|
465
467
|
|
466
468
|
180.degrees
|
467
469
|
# => 3.141592653589793
|
468
|
-
|
470
|
+
|
469
471
|
I.rotation = 90.degrees
|
470
472
|
|
471
473
|
@name degrees
|
@@ -474,15 +476,15 @@ Utility method to convert a number to an amount of degrees.
|
|
474
476
|
###
|
475
477
|
unless 2.degrees
|
476
478
|
Object.defineProperty Number::, 'degrees',
|
477
|
-
get: ->
|
479
|
+
get: ->
|
478
480
|
this * Math.TAU / 360
|
479
481
|
|
480
482
|
unless 1.degree
|
481
483
|
Object.defineProperty Number::, 'degree',
|
482
|
-
get: ->
|
484
|
+
get: ->
|
483
485
|
this * Math.TAU / 360
|
484
486
|
|
485
|
-
###*
|
487
|
+
###*
|
486
488
|
The mathematical circle constant of 1 turn.
|
487
489
|
|
488
490
|
@name TAU
|