@@ -335,26 +335,48 @@ function moveBoundingBox(scope, previous_x, previous_y, x_offset, y_offset, left
335
335
}
336
336
337
337
/**
338
+ * Primes are used for factoring.
339
+ * Store any that have been found for future use.
340
+ */
341
+ global_primes = new Set ( ) ;
342
+
343
+ /**
344
+ * Creates a list of all primes less than n.
345
+ * Stores primes in a set for better performance in the future.
346
+ * If some primes have been found, start with that list,
347
+ * and check the remaining numbers up to n.
338
348
* @param {any number } n
339
349
* @returns the list of all primes less than n
340
350
*/
341
351
function primesUpTo ( n ) {
352
+ n = Math . floor ( n ) ;
353
+ if ( Array . from ( global_primes ) . pop ( ) >= n ) { // All primes already found
354
+ return Array . from ( global_primes ) . filter ( x => { return x < n } ) ;
355
+ }
356
+ start = 2 ; // 0 and 1 can't be prime
342
357
primes = [ ...Array ( n + 1 ) . keys ( ) ] ; // List from 0 to n
343
- primes = primes . slice ( 2 , primes . length - 1 ) ; // 0 and 1 divide nothing and everything
344
- primes . forEach ( p => {
358
+ if ( Array . from ( global_primes ) . length ) { // Some primes already found
359
+ start = Array . from ( global_primes ) . pop ( ) + 1 ;
360
+ primes = primes . slice ( start , primes . length - 1 ) ;
361
+ primes = Array . from ( global_primes ) . concat ( primes ) ;
362
+ } else {
363
+ primes = primes . slice ( start , primes . length - 1 ) ;
364
+ }
365
+ primes . forEach ( p => { // Sieve of Eratosthenes method of prime factoring
345
366
primes = primes . filter ( test => { return ( test % p != 0 ) || ( test == p ) } ) ;
346
- } ) ;
347
- primes . forEach ( p => {
348
367
global_primes . add ( p ) ;
349
- } )
368
+ } ) ;
350
369
return primes ;
351
370
}
352
371
353
372
/**
373
+ * Every integer is either prime,
374
+ * is the product of some list of primes.
354
375
* @param {integer to factor } n
355
376
* @returns the list of prime factors of n
356
377
*/
357
378
function primeFactorsOf ( n ) {
379
+ n = Math . floor ( n ) ;
358
380
factors = [ ] ;
359
381
primes = primesUpTo ( n ) ;
360
382
primes . push ( n ) ;
@@ -373,6 +395,9 @@ function primeFactorsOf(n) {
373
395
* From the pixels per second of the project,
374
396
* Find a number of frames between each ruler mark,
375
397
* such that the tick marks remain at least 50px apart.
398
+ *
399
+ * Increases the number of frames by factors of FPS.
400
+ * This way each tick should land neatly on a second mark
376
401
* @param {Pixels per second } pps
377
402
* @param fps_num
378
403
* @param fps_den
@@ -384,15 +409,9 @@ function framesPerTick(pps, fps_num, fps_den) {
384
409
seconds = ( ) => { return frames / fps } ;
385
410
pixels = ( ) => { return seconds ( ) * pps } ;
386
411
factors = primeFactorsOf ( Math . round ( fps ) ) ;
387
- while ( pixels ( ) < 35 ) {
412
+ while ( pixels ( ) < 40 ) {
388
413
frames *= factors . shift ( ) || 2 ;
389
414
}
390
415
391
416
return frames ;
392
- }
393
-
394
- /**
395
- * Primes are used for factoring.
396
- * Store any that have been found for future use.
397
- */
398
- global_primes = new Set ( ) ;
417
+ }
0 commit comments