fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. struct Point
  9. {
  10. double x;
  11. double y;
  12. };
  13.  
  14. Point pClosest(vector<vector<double>> pts)
  15. {
  16. multimap<double, double> mp;
  17. int k = 1;
  18. Point p;
  19.  
  20. for (int i = 0; i < pts.size(); i++)
  21. {
  22. double x = pts[i][0], y = pts[i][1];
  23. mp.insert({ (x * x) + (y * y) , i });
  24. }
  25.  
  26. for (auto it = mp.begin();
  27. it != mp.end() && k > 0;
  28. it++, k--)
  29. {
  30. p.x = pts[it->second][0];
  31. p.y = pts[it->second][1];
  32. }
  33.  
  34. return p;
  35. }
  36.  
  37. Point pFurthest(vector<vector<double>> pts)
  38. {
  39. double maxDistance = 0.0;
  40. int maxDistanceIdx = 0;
  41.  
  42. for (int i = 0; i < pts.size(); i++)
  43. {
  44. double x = pts[i][0], y = pts[i][1];
  45. double sum1 = 0 - x;
  46. double sum2 = 0 - y;
  47. double distance = sqrt(sum1 * sum1 + sum2 * sum2);
  48. if (distance > maxDistance)
  49. {
  50. maxDistance = distance;
  51. maxDistanceIdx = i;
  52. }
  53. }
  54.  
  55. Point p;
  56. p.x = pts.at(maxDistanceIdx)[0], p.y = pts.at(maxDistanceIdx)[1];
  57. return p;
  58. }
  59.  
  60. int main()
  61. {
  62. vector<vector<double>> points = { { 8.0, 9.0 },
  63. { 4.0, 7.5 },
  64. { 1.0, 2.0 },
  65. { 5.1, 8.7 },
  66. { 9.0, 2.0 },
  67. { 4.5, 1.0 }
  68. };
  69.  
  70. Point p1 = pClosest(points);
  71. Point p2 = pFurthest(points);
  72.  
  73. cout << "Najbliższy punkt: " << p1.x << ", " << p1.y << endl;
  74. cout << "Najdalszy punkty: " << p2.x << ", " << p2.y << endl;
  75. double a = p2.x - p1.x;
  76. double b = p2.y - p1.y;
  77. double res = sqrt(a * a + b * b) / 2;
  78. cout << "Promien okregu: " << res << endl;
  79. Point midPoint;
  80. double midX = (p1.x + p2.x) / 2;
  81. double midY = (p1.y + p2.y) / 2;
  82. midPoint.x = midX, midPoint.y = midY;
  83. cout << "Wspolrzedne srodka okregu: " << midPoint.x << ", " << midPoint.y << endl;
  84. return 0;
  85. }
Success #stdin #stdout 0s 5608KB
stdin
Standard input is empty
stdout
Najbliższy punkt: 1, 2
Najdalszy punkty: 8, 9
Promien okregu: 4.94975
Wspolrzedne srodka okregu: 4.5, 5.5