难题再发,请强人指导!!大家多多讨论,谢谢了!!!

jjduan185 2005-04-19 08:58:36
问题一:
给定一组随即点的纵横坐标(x[i],y[i]),再给定一个点P,计算P到这组点的最近距离,也就是求一个点Q,
使得P到其他所有点的距离都不小于P到Q的距离,主要是计算的速度,挨个查询效率很低。
问题二:给一组折线断和一个点P,计算P到这组线段的最近距离,也就是求一段线段和这段线段上的一个点Q。
使得P到其他线段上的点的距离都不小于P到Q的距离,并求出这段线段的起点到Q的距离
小弟不知如何优化,查询是太慢了,请指教!!!

typedef struct coordinate
{
double x_cd;
double y_cd;
}* Point;/*Using a struct with two members 'x_cd' and 'y_cd' to descripe a point */
struct Line
{
coordinate StartPoint;
coordinate EndPoint;
};
#define MAXSIZE 10000/* Maximum number of points*/
#define MAXDISTANCE 100000/*maximum distance between points */
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
Point Read_data_from(char* filename);/*read coordinate of points from finame and store it in
array PointsArray ,then return the address of the array*/
double Min_dis(Point PointsArray,coordinate KeyPoint);/*Calculate the distance between point 'Keypoint' and the other points,
then return the index of the point who is the nearest point to KeyPoint,meanwhile store the minimum diatance
in the first position of the array as PointsArray[0].y_cd */
int main(int argc,char*argv[])
{
coordinate KeyPoint;
coordinate* Points=Read_data_from("data.txt");/*read data from file and store it in the array */
int size=(Points[0].x_cd--);/*Get the size of Point array */
KeyPoint=Points[size];/*Read the keypoint lastly */
cout<<Min_dis(Points,KeyPoint)<<endl;
cout<<Points[0].y_cd<<endl;
return 0;
}
/***********************************************************************************************************/
Point Read_data_from(char *finame)
{
ifstream read(finame);
if(read==NULL)
{
cout<<"Error,file not exist\n";
exit(1);
}/*If the file can not be opened ,end the programe*/
int counter(0);/*To count the number of points read in*/
Point PointsArray=new coordinate[MAXSIZE];
while(read>>PointsArray[counter+1].x_cd && read>>PointsArray[counter+1].y_cd)
counter++;
PointsArray[0].x_cd=counter;/*We use the first position to store the number of points */
return PointsArray;
}
/*************************************************************************************************************/
double Min_dis(Point PointsArray,coordinate KeyPoint)
{
const length=PointsArray[0].x_cd;/*Get the size of array*/
double x=KeyPoint.x_cd,y=KeyPoint.y_cd;
int index(0);/* the number of the nearest point */
double MinDistance(MAXDISTANCE);/*initailize the minmun distance with a big number */
for(int i=1;i<=length;i++)/*Loop to calculate every distance*/
{
double temX=PointsArray[i].x_cd;
double temY=PointsArray[i].y_cd;
double dis=sqrt((x-temX)*(x-temX)+(y-temY)*(y-temY));
MinDistance= MinDistance > dis ? dis : MinDistance;/*if new distance is small than MinDistance,change
MinDistance with new distance 'dis',and then change the index of the nearest point */
index= MinDistance >= dis ? i:index;
}
PointsArray[0].y_cd=index;
return MinDistance;/*store the index of the nearest point as PointsArray[0].y_cd*/
}
这是我写的,但是从点的平面分布来说,可能有以下的优化方法:
比如:把平面划分成若干区域,应最先在已知点的最近邻近区域内找,找到了当然最近。
还有,就是以已知点为圆心搜索其周围,最先找到的应该为最优解,但是我不好实现!
还请各位指点一二,万分感谢!!!
...全文
213 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjduan185 2005-04-27
  • 打赏
  • 举报
回复
不好意思:是
duanjigang1983@126.com
jjduan185 2005-04-27
  • 打赏
  • 举报
回复
liqiuhua123(程序人生) :
谢过了;但是我的点的坐标是double 型的,不是整型的,因此查找时,得设一个步长,但是也可能遇到这样的情况,在加上某个步长之后,得到的点据当前点最近,但坐标不等(只差了一个极小的量),但我们却不能匹配到这个点,而且,这样的搜索,具有盲目性。另外,若减去当前点的坐标排序
这也需要很大的空间,8000多个点得用200多找兆空间。
我们把存储点信息的时间不算入最终的效率,只是看给定一个点时,在已知点中匹配最近点的时间,因此,在读入数据时,如果能够给其安排一个好的存储结构,便于匹配,则可以省下很多时间,如果纵横坐标为整数,我可以用十字链表来存储,在列上纵坐标递增,在行上,横坐标递增,查找应该比较容易,党委double型时,得确定一个差值,当一个点与当前点的坐标差小于差值时,视为同一个点。
这样可能会快些,但我想不出怎样实现更好,这样也不太好实现!!!
谢谢,请各位再多多出谋划策!!
发到我的邮箱 duanjigang1983@26.com
daidodo 2005-04-23
  • 打赏
  • 举报
回复
我想运算速度的主要限制在于求距离上。
有一种次优的解法,就是用纵横坐标差的绝对值之和代替准确的距离,速度会提高很多,而正确性对于随机生成的点应该不低。
liqiuhua123 2005-04-23
  • 打赏
  • 举报
回复
我觉得你提到的圆心的方法比较好,假设你P点的坐标为(m,n);从i=0开始,你检查横坐标为m+i、m-i的所有点,如果找到点的话,看它的纵坐标是不是n+i、n-i,如果是,最短的点找到了。
否则i++,继续重复上面的操作。

还有一种方法可以试试,你把所有点的坐标转化为以P点为圆心的相对坐标,即所有点的横纵坐标分别减m,n.(取绝对值)。得到新的一个数组,对它进行排序,先对横坐标排序,然后对纵坐标排序,排在最前面的就是最短距离的点。

可能行不通,希望能帮上一点忙。
xjp6688 2005-04-23
  • 打赏
  • 举报
回复
找找这样的资料
CgPanda_ 2005-04-23
  • 打赏
  • 举报
回复
up

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧