计算数学高手请:多边形内哪一点到所有顶点的距离和最小?算法如何实现?

杨哥儿 2008-12-13 02:51:14
我有一系列多边形(4边、5边、6边甚至7边),不能分辨或标识是不是凸凹多边形。
现在我要求一点,要求到多边形所有顶点间的距离和最小。
我的算法:
坐标x=∑(Xi)/n
坐标y=∑(Yi)/n
在凸多边形中检查是最小值,但在凹多边形中就不是?
请高手说明下原理和算法,不给代码也行,有数学公式就可以了!!
...全文
1480 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
grearo 2008-12-18
  • 打赏
  • 举报
回复
算法没有优化,最基本的算法
grearo 2008-12-18
  • 打赏
  • 举报
回复
这个是.cs的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NearestPoint
{
public partial class Form1 : Form
{
public Point[] allPoint = new Point[10];
DateTime startTime = new DateTime();
public Form1()
{
InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{

//int pCount=
}



/// <summary>
/// 遍历区域
/// </summary>
/// <param name="maxP"></param>
/// <param name="minP"></param>
/// <param name="PointList"></param>
/// <returns></returns>
private Point BianliArea(Point maxP, Point minP, Point[] PointList)
{
Point pTemp = new Point();
double lenghTemp = 9999999999;
for (int x = minP.X; x <= maxP.X; x++)
{
for (int y = minP.Y; y <= maxP.Y; y++)
{
double getlength = GetLength(new Point(x, y), PointList);
if (getlength < lenghTemp)
{
lenghTemp = getlength;
pTemp.X = x;
pTemp.Y = y;
listBox1.Items.Add(pTemp.X.ToString() + ":" + pTemp.Y.ToString() + " L:" + lenghTemp.ToString());
}
}
}
return pTemp;
}

/// <summary>
/// 获取某点到各点的距离
/// </summary>
/// <param name="p"></param>
/// <param name="PointList"></param>
/// <returns></returns>
private double GetLength(Point p, Point[] PointList)
{
double lengthSum = 0;
foreach (Point p0 in PointList)
{
lengthSum += Math.Sqrt((p0.X - p.X) * (p0.X - p.X) + (p0.Y - p.Y) * (p0.Y - p.Y));
}
return lengthSum;
}

private void button2_Click(object sender, EventArgs e)
{
startTime = DateTime.Now;
allPoint[0] = new Point(8, 7);
allPoint[1] = new Point(5, 8);
allPoint[2] = new Point(2, 2);
allPoint[3] = new Point(1, 0);
allPoint[4] = new Point(9, 4);
allPoint[5] = new Point(3, 2);
allPoint[2] = new Point(12, 35);
allPoint[3] = new Point(34, 33);
allPoint[4] = new Point(62, 56);
allPoint[5] = new Point(45, 32);
Point nearestPoint = BianliArea(new Point(62, 56), new Point(1, 0), allPoint);
DateTime endTime = DateTime.Now;
listBox1.Items.Add("NearestPoint:"+nearestPoint.X.ToString() + ":" + nearestPoint.Y.ToString() );

listBox1.Items.Add("StartTime:" + startTime.ToString());
listBox1.Items.Add("EndTime:" + endTime.ToString());
listBox1.Items.Add("UsedTime:" + (endTime.Millisecond - startTime.Millisecond).ToString() + "ms");

}
}
}
grearo 2008-12-18
  • 打赏
  • 举报
回复
找到代码了
这个是.design.cs的

namespace NearestPoint
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.listBox2 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(12, 12);
this.listBox1.Name = "listBox1";
this.listBox1.ScrollAlwaysVisible = true;
this.listBox1.Size = new System.Drawing.Size(313, 220);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(421, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 50);
this.button1.TabIndex = 1;
this.button1.Text = "Find It From listBox";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox2
//
this.listBox2.FormattingEnabled = true;
this.listBox2.ItemHeight = 12;
this.listBox2.Items.AddRange(new object[] {
"8,7",
"5,8",
"2,2",
"1,0",
"9,4",
"3,2",
"12,35",
"34,33",
"62,56",
"45,32"});
this.listBox2.Location = new System.Drawing.Point(331, 11);
this.listBox2.Name = "listBox2";
this.listBox2.ScrollAlwaysVisible = true;
this.listBox2.Size = new System.Drawing.Size(84, 220);
this.listBox2.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(421, 68);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 49);
this.button2.TabIndex = 3;
this.button2.Text = "Find It From Point[]";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(508, 243);
this.Controls.Add(this.button2);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Button button2;
}
}



grearo 2008-12-18
  • 打赏
  • 举报
回复
还打算给你代码的来着,一直都没人,也不结贴,也不加分的~~```汗,失望啊~··O(∩_∩)O哈哈~
webjishu001 2008-12-15
  • 打赏
  • 举报
回复
http://coder.webjishu.cn/Html/?14315.html


webjishu001 2008-12-15
  • 打赏
  • 举报
回复
我已经根据Grearo的方法写出来了。
杨哥儿 2008-12-15
  • 打赏
  • 举报
回复
没人理我.明天就结了.只能这样了.
谢谢grearo!!
bjwoxp 2008-12-15
  • 打赏
  • 举报
回复
中国星幻科技有限公司是一家专门为企业和个人量身定制短信平台的专业公司.专业做行业短信群发 行业短信接口 网站短信接口 节日短信群发 客户短信群发的公司,整合多条正规的绿色短信行业群发通道,支持短信群发、定时发送、支持短信自动接收、短信自动回复、转发、短信发送智能断点续发;可以集成在网站、客户管理系统、进销存系统、OA、生产管理系统等等、提供HTTP网站短信接口、WEBSERVICE短信接口、适应各种语言所有网站都可以接入.用于企业的客户关系维护,产品以及企业活动的宣传,适用于大中小型企业,商场,酒店等星幻短信平台是面向广大企事业单位开发的手机短信收发系统。以特有的高效、准确、实用、便捷和经济,为各企、事业单位和机关团体提供一个崭新、实用的即时信息发布平台,架起了企业与用户之间信息沟通的桥梁。可以让您的老客户不走,新客户不断! 平台具备全国全网,四网合一(移动、联通、电信、网通)的短信服务提供商。同时覆盖全国移动、联通、小灵通等多个网络用户,轻松实现短信上行和下行 星幻短信网关多条通道发送;支持上下行支持HTTP GET/POST WebService接口;发送速度快;延时小.可以在线发短信。不用下载软件. 短信网关,短信接口,网站短信.群发短信。价格便宜,质量保证。多条通道供你选择。移动联通106直连通道已接入。只能发行业。有需要的大客户速联系。价格优惠。每秒在100-200条. 星幻短信 移动、联通直连全网短信网关,提供网站短信接口.群发短信。价格便宜,质量保证,多条通道供你选择.四网合一,支持回复. www.woxp.cn 中国星幻科技有限公司 接口地址: http://www.xhsms.com/sms_gate.htm
※同样的价格我们比质量※ ※同样的质量我们比服务※ ※同样的服务我们比信誉※ 真诚期待与您携手合作!
grearo 2008-12-15
  • 打赏
  • 举报
回复
别听他瞎忽悠,那是我原来的老代码,没有求整运算,也就是说有算法优化的;
10点0-1000以内整数需要0-100MS,平均16MS;
好好看看我的说明,自己写吧。
按照我说的优化后的算法,应该计算复杂度为log2n*4;而原来算法复杂度为:n*n (注:n为最大数与最小数的差的绝对值)
杨哥儿 2008-12-15
  • 打赏
  • 举报
回复
能给下源代码不?
要多少分你说?
杨哥儿 2008-12-14
  • 打赏
  • 举报
回复
还希望能有更多的高手给点思路!
杨哥儿 2008-12-14
  • 打赏
  • 举报
回复
grearo的方法论 没明白.还有其它高手的算法否?
grearo 2008-12-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 uself 的回复:]
看到了你的帖子:
http://topic.csdn.net/u/20081212/12/c87c8a23-a327-4dbb-a466-bf0a3e8f5462.html
呵呵!!
[/Quote]
看到之后,试验了一下,不小心全捐给朋友了~~·
杨哥儿 2008-12-13
  • 打赏
  • 举报
回复
grearo 2008-12-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 uself 的回复:]
谢谢grearo!
我还是有点没明白!!
怎么给可用分.你说方法.我给你500可用分.
[/Quote]
不用
你怎么知道我没有可用分啊?O(∩_∩)O哈哈~
杨哥儿 2008-12-13
  • 打赏
  • 举报
回复
谢谢grearo!
我还是有点没明白!!
怎么给可用分.你说方法.我给你500可用分.
grearo 2008-12-13
  • 打赏
  • 举报
回复
当然你可以对2取整,取多次,log2(max(x,y))
grearo 2008-12-13
  • 打赏
  • 举报
回复
注意,如果不会求S1,可以直接用{S1:Xmin<=x<=Xmax,Ymin<=y<=Ymax}
以上算法适用于坐标在0-99以内多边形状;你可以更改取整值,当然可以多次取整操作
grearo 2008-12-13
  • 打赏
  • 举报
回复
多边形(多点)最短距离点求法----区域点遍历法 O(∩_∩)O哈哈~
第一步:取整数分步,将所有点坐标10倍取整( 例如:P1=(34,56)10倍取证后(3.4,5.6)-> (4,6) ),获得去整后各点组成的遍历区域S1;
第二步:遍历S1内的各点,寻找最短距离点Pmin(Xm,Ym);
第三步:用第2步取到的点,获取遍历区域II{S2:Xm*10-10<=x<=Xm*10,Ym*10-10<=y<=Ym*10};
第四步:用第二步遍历区域S2,获取最近点;


杨哥儿 2008-12-13
  • 打赏
  • 举报
回复
多边形重心?
加载更多回复(3)

13,190

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 分析与设计
社区管理员
  • 分析与设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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