#### Archived

This topic is now archived and is closed to further replies.

# Problem understanding an algorithm used in LOTR contest

This topic is 6012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello all, Im having problems understanding a piece of code in one of the entries by mirko teran revnikar (the winning entry). I was hoping some of you, who have looked through the source may have a better understanding of it. The effect right at the beginning whereby the screen splits into a series of blocks and changes is what''s causing me confusion. The code is in "UnityPaletteTemplate.h" the section of code I''m struggling with is: template Type UPaletteTemplate::Get( float position ) { Type result; if ( count==0 ) return result; // error: no entry if ( cur != count ) return result; // error: not full if ( position <= key[ 0 ] ) return data[ 0 ]; // before start index if ( position >= key[ count-1 ] ) return data[ count-1 ]; // after end index int pos1 = 0; // start position while ( key[ pos1 ] < position ) pos1++;// find end position int pos0 = pos1 - 1; // set start position float delta = absf(key[ pos1 ] - key[ pos0 ]);// set index distance position -= key[ pos0 ]; // get distance on segment const float bal = position / delta; // balance const float oneMinusBal = 1.0f - bal; // 1-balance result = ( data[ pos0 ] * oneMinusBal ) + ( data[ pos1 ] * bal ); return result; // return result } Could someone explain this to me, is is an implementation of a ''map''. What does the ''balance'' mean? Ive tried working it out on paper with a set of keys and data to try and figure out how it works, but i just dont understand what it is calcualting. Any ideas anyone? Any help much appreciated. Thanks in advance -cs

##### Share on other sites
''balance'' means something like interpolation factor or "lerp".

if you have 2 vectors
0,1,2 and 1,2,3
with interpoiation 0.5 you get:

result = ([0,1,2] * (1 - 0.5)) + ([1,2,3] * 0.5) // (=[0.5, 1.5, 2.5])

and it''s not implementation of map. It''s more like 1D texture. But you can store any type of data in it. Not just colors.

You should never let your fears become the boundaries of your dreams.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633324
• Total Posts
3011366
• ### Who's Online (See full list)

There are no registered users currently online

×