Posts Tagged ‘math’

Fixed Point

Saturday, November 14th, 2009

Fixed point is used to represent a floating point number.
It could be used to speed up the multiplication and division operations on systems without a floating point processor.

Floating point is represented by scaling an integer.
In C/C++ one would shift the number by a scale…

const int SHIFT_AMT = 16;
const int INTEGER_MASK = 0xFF;
const int DECIMAL_MASK = 0xFF;

It is easiest for me to think of fixed point as a number multiplied by a scale.
I find it easier because it makes thinking about operations on the fixed point easier.

MATH

fixedpoint = (number * scale)

C/C++

typedef signed int fixedpoint;
int i = 10;
fixedpoint fp = i << SHIFT_AMT;

Addition and Subtraction are trivial with fixed point, Multiplication and Division are not...
Well... they are just not as trivial, there is one more step involved, scaling the result.

For Multiplication, you need to divide by the scale.
For Division, you need to multiply by the scale.

Multiplication
MATH

fixedpoint * fixedpoint =

(number1 * scale) * (number2 * scale) =

((number1 * number2) * scale^2) =

(((number1 * number2) * scale^2) / scale) =

((number1 * number2) * scale)

C/C++

fixedpoint number1 = 10 << SHIFT_AMT;
fixedpoint number2 = 10 << SHIFT_AMT;
fixedpoint result = (number1 * number2) >> SHIFT_AMT;

Division
MATH

fixedpoint * fixedpoint =

(number1 * scale) / (number2 * scale) =

(number1 / number2) =

(((number1 / number2)) * scale)

C/C++

fixedpoint number1 = 10 << SHIFT_AMT;
fixedpoint number2 = 10 << SHIFT_AMT;
fixedpoint result = (number1 / number2) << SHIFT_AMT;

Other useful operations on fixed point could be:
Modulus.
Square-root.
Sine.
Cosine.

Convert Color Spaces (RGB->HSV)

Monday, November 23rd, 2009

Math

{r}\in[0,1] 
{g}\in[0,1] 
{b}\in[0,1] 
max=\max(r,g,b) 
min=\min(r,g,b) 

 h = \begin{cases}     {0}, & if {max}={min} \\     ({60}\times\frac{{g}-{b}}{{max}-{min}}+{360})\mod{360}, & if {max}={r} \\     ({60}\times\frac{{b}-{r}}{{max}-{min}}+{120}), & if {max}={g} \\     ({60}\times\frac{{r}-{g}}{{max}-{min}}+{240}), & if {max}-{b} \end{cases} 

 s = \begin{cases}     0, & if {max}={0} \\     {1}-\frac{min}{max}, & otherwise \end{cases} 

v=max 

C/C++


pixeltype RGBtoHSV(pixeltype palette_color){
	//0.0f <= r <= 1.0f
	//0.0f <= g <= 1.0f
	//0.0f <= b <= 1.0f
    double r = (double)(RED_OF(palette_color));
    double g = (double)(GREEN_OF(palette_color));
    double b = (double)(BLUE_OF(palette_color));
    double max, min, h, s, v;

	if(r > g){
		max = r;
	}else{
		max = g;
	}
	if(b > max){
		max = b;
	}

	if(r < g){
		min = r;
	}else{
		min = g;
	}
	if(b < min){
		min = b;
	}

    if(max == min){
        h = 0.0f;
    }else if(max == r){
        h = ((60.0f * ((g - b) / (max - min)) + 360) % 360.0f);
    }else if(max == g){
        h = ((60.0f * ((b - r) / (max - min)) + 120));
    }else if(max == b){
        h = ((60.0f * ((r - g) / (max - min)) + 240));
    }

    if(max == 0){
        s = 0.0f;
    }else{
        s = 1.0f - (min / max);
    }
    v = max;

	//0.0f <= h <= 360.0f
	//0.0f <= s <= 1.0f
	//0.0f <= v <= 1.0f
    return HSV_OF(h, s, v);
}

Convert Color Spaces (RGB->HSL)

Sunday, November 29th, 2009

Math

{r}\in[0,1] 
{g}\in[0,1] 
{b}\in[0,1] 
max=\max(r,g,b) 
min=\min(r,g,b) 

 h = \begin{cases}     {0}, & if {max}={min} \\     ({60}\times\frac{{g}-{b}}{{max}-{min}}+{360})\mod{360}, & if {max}={r} \\     ({60}\times\frac{{b}-{r}}{{max}-{min}}+{120}), & if {max}={g} \\     ({60}\times\frac{{r}-{g}}{{max}-{min}}+{240}), & if {max}-{b} \end{cases} 

\frac{1}{2}({{max}+{min}}) 

 s = \begin{cases}     0, & if {max}={min} \\     \frac{{max}-{min}}{2l}, & if l\le{\frac{1}{2}} \\     \frac{{max}-{min}}{{2}-2l}, & if l>{\frac{1}{2}} \end{cases} 

C/C++

pixeltype RGBtoHSV(pixeltype palette_color){
	//0.0f <= r <= 1.0f
	//0.0f <= g <= 1.0f
	//0.0f <= b <= 1.0f
    double r = (double)(RED_OF(palette_color));
    double g = (double)(GREEN_OF(palette_color));
    double b = (double)(BLUE_OF(palette_color));
    double max, min, h, s, l;

	if(r > g){
		max = r;
	}else{
		max = g;
	}
	if(b > max){
		max = b;
	}

	if(r < g){
		min = r;
	}else{
		min = g;
	}
	if(b < min){
		min = b;
	}

    if(max == min){
        h = 0.0f;
    }else if(max == r){
        h = ((60.0f * ((g - b) / (max - min)) + 360) % 360.0f);
    }else if(max == g){
        h = ((60.0f * ((b - r) / (max - min)) + 120));
    }else if(max == b){
        h = ((60.0f * ((r - g) / (max - min)) + 240));
    }

	l = (max + min) / 2.0f;

	if(max == min){
		s = 0;
	}else if(l <= 0.5f){
		s = ((max - min) / (2 * l));
	}else if(l > 0.5f){
		s = ((max - min) / (2 - (2 * l)));
	}

	//0.0f <= h <= 360.0f
	//0.0f <= s <= 1.0f
	//0.0f <= l <= 1.0f
    return HSL_OF(h, s, l);
}
}

Convert Color Spaces (HSL->RGB)

Thursday, December 3rd, 2009

Math

{h}\in[0,360) 

{s}\in[0,1] 

{l}\in[0,1] 

 {q}= \begin{cases}     {{l}\times({1}+{s})}, & if\quad{l}<\frac{1}{2} \\     {{l}+{s}-({l}\times{s})}, & if\quad{l}\ge\frac{1}{2} \end{cases} 

{2}\times{l}-{q} 

h_k=\frac{h}{360} 

t_R=(h_k+\frac{1}{3})\mod{1} 

t_G=(h_k)\mod{1} 

t_B=(h_k-\frac{1}{3})\mod{1} 

foreach\quad{t_C}\in\{{t_R}, {t_G}, {t_B}\} 
 {t_C}= \begin{cases}     {{p}+(({q}-{p})\times{6}\times{t_C}}, & if\quad{t_C <\frac{1}{6}} \\     {q}, & if\quad{\frac{1}{6}\le{t_C}<\frac{1}{2}} \\     {{p}+(({q}-{p})\times{6}\times{\frac{2}{3}-t_C}}, & if\quad{\frac{1}{2}\le{t_C}<\frac{2}{3}} \\     {p}, & otherwise \end{cases} 

{{t_R}\equiv{r}}\in[0,1] 

{{t_G}\equiv{g}}\in[0,1] 

{{t_B}\equiv{b}}\in[0,1] 

C/C++

pixeltype HSLtoRGB(pixeltype palette_color){
	//0.0f <= h <  360.0f
	//0.0f <= s <= 1.0f
	//0.0f <= l <= 1.0f
	double h = (double)(HUE_OF(palette_color));
    double s = (double)(SATURATION_OF(palette_color));
    double l = (double)(LIGHTNESS_OF(palette_color));
	double q, p, h_k;
	double rgb[3];

	if(l < 0.5f){
		q = l * (1 + s);
	}else{
		q = l + s - (l * s);
	}
	p = 2 * l - q;
	h_k = h / 360.0f;
	rgb[0] = h_k + (1 / 3);
	rgb[1] = h_k;
	rgb[2] = h_k - (1 / 3);
	for(size_t i = 0; i < 3; i++){
		if(rgb[i] < (1 / 6)){
			rgb[i] = p + ((q - p) * 6.0f * rgb[i]);
		}else if(rgb[i] >= (1 / 6) && rgb[i] < (1 / 2)){
			rgb[i] = q;
		}else if(rgb[i] >= (1 / 2) && rgb[i] < (2 / 3)){
			rgb[i] = p + ((q - p) * 6.0f * ((2 / 3) - rgb[i]));
		}else{
			rgb[i] = p;
		}
	}
	//0.0f <= rgb[0] <= 1.0f
	//0.0f <= rgb[1] <= 1.0f
	//0.0f <= rgb[2] <= 1.0f
	return RGB_OF(rgb[0], rgb[1], rgb[2]);
}
Get Adobe Flash playerPlugin by wpburn.com wordpress themes

GameProgrammerArt is Digg proof thanks to caching by WP Super Cache