Skip to content

Instantly share code, notes, and snippets.

@jmacey
Created December 14, 2020 20:21
Show Gist options
  • Save jmacey/7ae62376aa54a3f933260eabeeb8beef to your computer and use it in GitHub Desktop.
Save jmacey/7ae62376aa54a3f933260eabeeb8beef to your computer and use it in GitHub Desktop.
Point Find
#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