Skip to content

Instantly share code, notes, and snippets.

@MasazI
Created May 19, 2015 16:39
scale.cpp
//
// 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