Last active
December 3, 2018 01:57
-
-
Save wdanxna/1b43353351caa8315aafb408475820b8 to your computer and use it in GitHub Desktop.
Scale a set of Points about a scale center using template c++
This file contains 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
#include <iostream> | |
#include <vector> | |
using namespace std; | |
template <class PT, class Iterable, class xGetter, class yGetter, class LAMBDA> | |
static void scalePoints(Iterable& points, PT& center, float x, float y, xGetter gx, yGetter gy, LAMBDA cb) { | |
int i = 0; | |
for (auto& p : points) { | |
float px = gx(p) - gx(center); | |
float py = gy(p) - gy(center); | |
px *= x; | |
py *= y; | |
px += gx(center); | |
py += gy(center); | |
cb(px, py, i++); | |
} | |
} | |
template <class PT, class Iterable, class xGetter, class yGetter, class LAMBDA> | |
static void scalePointsUniformly(Iterable& points, PT center, float scale, xGetter gx, yGetter gy, LAMBDA cb) { | |
scalePoints<PT, Iterable, xGetter, yGetter, LAMBDA>(points, center, scale, scale, gx, gy, cb); | |
} | |
struct Point { | |
float x, y; | |
}; | |
int main() | |
{ | |
cout<<"Hello World" << endl; | |
std::vector<Point> points{{0, 0}, {0, 1}, {1, 1}, {1, 0}}; | |
scalePointsUniformly( | |
points, | |
Point{0.5f, 0.5f}, | |
2.0f, | |
[](Point& p){return p.x;}, | |
[](Point& p){return p.y;}, | |
[](float x, float y, int i){ | |
cout << x << "," << y << std::endl; | |
}); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment