Jump to content
  • Advertisement


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


Need an algorithm for converting 0-n to 0-255

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

distance is 0-n, I need to convert the distance to 0-255. If I could I would do it like this for example: increasement=256/max_distance;
But I can''t in this case. I need it to be somehow check with the current distance. Here''s what I''m trying to do with a circle:
if(x<sqrt(rad*rad-(y*y))) //if the current coordinate is within the circle (adjustable center not implemented yet, it''s 0,0 currently)

dist=(int)(sqrt((float)(y*y+x*x))); //dinstance from 0,0

red+= 255 - //<-distance modifier here = distance somehow converted to 0-255

// 255 - might also be 0 + depending on the way that the distance is converted

// I thought it would be easier to make it so that the dist. modifier would be 255 when distance is the max_distance(=the radius of the circle), hence 255 - dist_mod

I know this is an easy question, but my brains are seriously jammed atm and I can''t figure out it by myself right now. So any help would be appritiated. thank you

Share this post

Link to post
Share on other sites
double multiplier = 256/n;

now if n were say 128.....the multiplier would be 2
so any distance would be mutliplied by 2 to get the distance in terms of 0 to 255

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!