Created
May 19, 2015 16:39
-
-
Save MasazI/d817b02e5e7de5cd8ac2 to your computer and use it in GitHub Desktop.
scale.cpp
This file contains hidden or 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
// | |
// rescale.cpp | |
// CplusplusPractice | |
// | |
// Created by masai on 2015/05/20. | |
// Copyright (c) 2015年 masai. All rights reserved. | |
// | |
#include <iostream> | |
template<typename T, typename U> | |
U rescale(T x, T src_min, T src_max, U dst_min, U dst_max) { | |
U value = static_cast<U>(((x - src_min) * (dst_max - dst_min)) / (src_max - src_min) + dst_min); | |
return std::min(dst_max, std::max(value, dst_min)); | |
} | |
int main(int argc, const char * argv[]) { | |
int a = 10; | |
int b = 1; | |
int c = 100; | |
int d = 300; | |
int e = 500; | |
int result = rescale(a, b, c, d, e); | |
std::cout << result << std::endl; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment