Last active
July 21, 2019 12:09
-
-
Save masterzorag/3ce2ba98abcfe1633d1a105a9d0ba86b to your computer and use it in GitHub Desktop.
compute affine transform
This file contains 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
/* clang affine.c -Wextra */ | |
/* | |
./a.out 1 | |
1.000000 in [-1 1] = 255 in [0 255] | |
./a.out 0 | |
0.000000 in [-1 1] = 127 in [0 255] | |
./a.out -1 | |
-1.000000 in [-1 1] = 0 in [0 255] | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
/* for x in [a, b] compute y in [c, d] */ | |
uint8_t affine(float x, float a, float b, float c, float d) | |
{ | |
return (x - a) * ( (d - c) / (b - a) ) + c; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if(!argv[1]) goto fail; | |
float x; | |
sscanf(argv[1],"%f", &x); | |
if(x < -1.0f || x > 1.0f) goto fail; | |
uint8_t ret = affine(x, -1, 1, 0, 255); | |
printf("%f in [%d %d] = %d in [%d %d]\n", x, -1, 1, ret, 0, 255); | |
return ret; | |
fail: | |
puts("pass a number in range [-1, 1]"); return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment