Created
March 22, 2015 21:16
-
-
Save reportbase/620e65b3ceac3b9885f3 to your computer and use it in GitHub Desktop.
split an array and normalize it
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
inline void extentsplit(float* e, int size, int width) | |
{ | |
int empty_slots = 0; | |
int awidth = 0; | |
for(int n = 0; n < size; ++n) | |
{ | |
if(e[n] < 1) | |
e[n] = width * std::abs(e[n]); | |
awidth += e[n]; | |
empty_slots += e[n] == 0 ? 1 : 0; | |
} | |
if(empty_slots == 0) | |
return; | |
int balance = width - awidth; | |
if(balance <= 0) | |
return; | |
int slot_extent = (int)((float)balance / (float)empty_slots); | |
int remainder = balance - (empty_slots * slot_extent); | |
float* g = e + (size - 1); | |
for(int n = 0; n < size; ++n, --g) | |
{ | |
if(*g) | |
continue; | |
int d = slot_extent; | |
if(remainder-- >= 1) | |
d++; | |
*g = d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment