13,190
社区成员




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");
}
}
}
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;
}
}