Skip to content

Instantly share code, notes, and snippets.

@masterzorag
Last active July 21, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masterzorag/3ce2ba98abcfe1633d1a105a9d0ba86b to your computer and use it in GitHub Desktop.
Save masterzorag/3ce2ba98abcfe1633d1a105a9d0ba86b to your computer and use it in GitHub Desktop.
compute affine transform
/* 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