-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utils.h
63 lines (52 loc) · 1.46 KB
/
Utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* File: Utils.h
* Author: dude719
*
* Created on April 19, 2015, 7:40 PM
*/
#ifndef UTILS_H
#define UTILS_H
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
namespace Utils
{
static void CreateConsole(const char* name)
{
HANDLE lStdHandle = 0;
int hConHandle = 0;
FILE *fp = 0;
AllocConsole();
lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(PtrToUlong(lStdHandle), _O_TEXT);
SetConsoleTitle(name);
//SetConsoleTextAttribute(lStdHandle, FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_RED);
fp = _fdopen(hConHandle, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
}
static float RemapFloat(float x, float oMin, float oMax, float nMin, float nMax )
{
//check reversed input range
bool reverseInput = false;
float oldMin = oMin < oMax ? oMin : oMax;
float oldMax = oMin < oMax ? oMax : oMin;
if (oldMin == oMin)
reverseInput = true;
//check reversed output range
bool reverseOutput = false;
float newMin = nMin < nMax ? nMin : nMax;
float newMax = nMin < nMax ? nMax : nMin;
if (newMin == nMin)
reverseOutput = true;
float portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin);
if (reverseInput)
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin);
float result = portion + newMin;
if (reverseOutput)
result = newMax - portion;
return result;
}
}
#endif /* UTILS_H */