Created
March 29, 2014 23:31
-
-
Save Gerjo/9864811 to your computer and use it in GitHub Desktop.
Integer factorisation, quick hack to find an optimal texture size given an amount of pixels.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vector2 primo(int num) { | |
std::deque<int> primes; | |
const int root = (int) std::sqrt(num); | |
std::function<void(int)> recurse; | |
// Mostly from: http://www.coderenaissance.com/2011/06/finding-prime-factors-in-javascript.html | |
recurse = [&primes, &recurse, root] (int num) -> void { | |
int x = 2; | |
// if not divisible by 2 | |
if(num % x) { | |
x = 3; // assign first odd | |
// iterate odds | |
while((num % x) && ((x = x + 2) < root)) { | |
; // nop | |
} | |
} | |
//if no factor found then num is prime | |
x = (x <= root) ? x : num; | |
if(x != num) { | |
recurse(num / x); | |
} | |
primes.push_back(x);//push latest prime factor | |
}; | |
recurse(num); | |
int x = 1; | |
int y = 1; | |
// Grow X until the upper limit is reached. | |
while( ! primes.empty() && x * primes.front() < 16384) { | |
x *= primes.front(); | |
primes.pop_front(); | |
} | |
// Pass the remaining primes to y. | |
while( ! primes.empty() ) { | |
y *= primes.front(); | |
primes.pop_front(); | |
} | |
if(x > 16384 || y > 16384) { | |
Exit("Primes don't work. Add padding bytes or more photons."); | |
} | |
return Vector2(x, y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment