c++蛮力法解决最近对问题(可视化)

#include<iostream>
#include<math.h>
#include <graphics.h>
#include <conio.h>
using namespace std;
#define MAX 1.79769e+308
class ClosestPoints{
private:
 int n;
 double P[100][2];
 int x1,x2;
 int y1,y2;
public:
 void set_points();
 void do_points();
 void print();
};
void ClosestPoints::set_points()
{
 cin>>n;
 for(int i=0;i<n;i++)
  {
   cin>>P[i][0];
   cin>>P[i][1];
 }
}
void ClosestPoints::do_points()
{
 double mind = MAX;
 double dis;
 x1 = 0,y1 = 0;
 x2 = 0,y2 = 0;
 for(int i=0;i<n-1;i++)
  for(int j=i+1;j<n;j++)
  {
   dis = (P[j][0]-P[i][0])*(P[j][0]-P[i][0])+(P[j][1]-P[i][1])*(P[j][1]-P[i][1]);
   if(dis<mind)
   {
    mind = dis;
    x1 = P[i][0];
    y1 = P[i][1];
    x2 = P[j][0];
    y2 = P[j][1];
   }
  }
 cout<<sqrt(mind)<<" "<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
}
void ClosestPoints::print()
{
 initgraph(640, 480);
 setorigin(0,0);
 for(int i=0;i<n;i++)
 {
  fillcircle(P[i][0]*50,P[i][1]*50,2);
  Sleep(100);
 }
 for(int i=0;i<n;i++)
  for(int j=i+1;j<n;j++)
   {
    line(P[i][0]*50,P[i][1]*50,P[j][0]*50,P[j][1]*50);
    Sleep(100);
  }
 setlinecolor(RED);
 line(x1*50,y1*50,x2*50,y2*50);
 _getch();
    closegraph();
}
int main()
{
 ClosestPoints C1;
 C1.set_points();
 C1.do_points();
 C1.print();
 return 0;
}

采用蛮力法求出所有点之间的距离,然后进行比较找出第一个最近对
画图输出

在这里插入图片描述

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下为蛮力求凸包的C++代码及可视化部分的实现: ```c++ #include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <graphics.h> using namespace std; struct Point { double x, y; bool operator < (const Point& p) const { return x < p.x || (x == p.x && y < p.y); } }; vector<Point> convex_hull(vector<Point>& points) { int n = points.size(); if (n <= 1) return points; sort(points.begin(), points.end()); vector<Point> hull; for (int i = 0; i < n; i++) { while (hull.size() >= 2 && (hull.back().x - hull[hull.size()-2].x)*(points[i].y - hull.back().y) >= (hull.back().y - hull[hull.size()-2].y)*(points[i].x - hull.back().x)) { hull.pop_back(); } hull.push_back(points[i]); } return hull; } int main() { vector<Point> points = {{100, 100}, {200, 50}, {300, 150}, {150, 200}, {250, 250}, {150, 300}, {50, 250}, {100, 150}}; initwindow(500, 500, "Convex Hull"); setbkcolor(WHITE); cleardevice(); setcolor(BLACK); for (int i = 0; i < points.size(); i++) { circle(points[i].x, points[i].y, 3); delay(100); } vector<Point> hull = convex_hull(points); setcolor(RED); for (int i = 0; i < hull.size(); i++) { line(hull[i].x, hull[i].y, hull[(i+1)%hull.size()].x, hull[(i+1)%hull.size()].y); delay(100); } while (!kbhit()) { delay(200); } closegraph(); return 0; } ``` 其中,`Point` 结构体表示的坐标,`convex_hull` 函数计算蛮力求解凸包,`main` 函数中的 `vector<Point> points` 表示所有的坐标。可视化部分使用了 `graphics.h` 库,首先使用 `initwindow` 函数初始化窗口,然后使用 `setcolor` 函数设置颜色,使用 `circle` 函数画出所有的,使用 `line` 函数画出凸包。`delay` 函数用于延迟一定时间,以便观察过程。最后,使用 `kbhit` 函数等待按键,按下任意键后关闭窗口。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值