2015年5月20日水曜日

座標系スケール変換関数(template)

座標系のスケール変換を行う関数をテンプレート使って記述する。
//
// 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;
}
view raw scale.cpp hosted with ❤ by GitHub


0 件のコメント:

コメントを投稿