Created
December 14, 2020 20:21
-
-
Save jmacey/7ae62376aa54a3f933260eabeeb8beef to your computer and use it in GitHub Desktop.
Point Find
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 <random> | |
#include <numeric> | |
std::random_device rd; | |
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() | |
std::uniform_real_distribution<float> dis(-100.0f, 100.0f); | |
struct Point | |
{ | |
Point() { x=dis(gen); y=dis(gen);} | |
float distanceSq() {return x*x+y*y;} | |
float x,y; | |
}; | |
int main() | |
{ | |
std::vector<Point> points(10); | |
size_t minIndex=0; | |
size_t maxIndex=0; | |
float min=std::numeric_limits<float>::max(); | |
float max=std::numeric_limits<float>::min(); | |
for(size_t i=0; i<points.size(); ++i) | |
{ | |
auto dist=points[i].distanceSq(); | |
std::cout<<points[i].x<<' '<<points[i].y<<' '<<dist<<'\n'; | |
if(dist <=min) | |
{ | |
min=dist; | |
minIndex=i; | |
} | |
if(dist >=max) | |
{ | |
max=dist; | |
maxIndex=i; | |
} | |
} | |
std::cout<<"MinPoint "<<points[minIndex].x<<','<<points[minIndex].y<<'\n'; | |
std::cout<<"MaxPoint "<<points[maxIndex].x<<','<<points[maxIndex].y<<'\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment