迭代求两点之间的中点
比如有两点 A(100,50),B(20,600),可以求得点C((x1+x2)/2,(x1+x2)/2),可以求得AC中点D,和BC中点E,就这样一直求中点,满足相邻两点之间距离不小于5,小于5程序结束?我求不来迭代啊!! c# 两点中点 迭代 --------------------编程问答-------------------- point a,b,mid;double getdistance(point a,point b)
{
.....
.....
return dist;
}
point getmidpoint(point a,point b)
{
......
......
return mid;
}
mid=getmidpoint(a,b);
while(getdistance(a,mid)<5)
{
mid=getmidpoint(a,mid);
} --------------------编程问答-------------------- while(getdistance(a,mid)>=5) --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace qiuzhongdian
{
class Program
{
static void Main(string[] args)
{
point p1 = new point(1, 0);
point p2 = new point(10, 0);
point p3 = new point();
point p4 = new point();
p3= mathclass.zhongidan(p1, p2);
p4= mathclass.zhongidan(p2, p1);
Console.WriteLine(p3.X + "," + p3.Y);
Console.WriteLine(p4.X + "," + p4.Y);
Console.ReadKey();
}
}
struct point
{
private float x;
private float y;
public point(float a, float b)
{
this.x = a;
this.y = b;
}
public float X
{
get
{
return x;
}
set
{
this.x = value;
}
}
public float Y
{
get
{
return y;
}
set
{
this.y = value;
}
}
}
class mathclass
{
public static point zhongidan(point p1,point p2)
{
if (mathclass.PointLength(p1, p2) > 5)
{
return zhongidan(p1, new point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2));
}
else
{
return p2;
}
}
public static double PointLength(point p1, point p2)
{
return Math.Sqrt(Math.Pow((p1.X - p2.X), 2) + Math.Pow((p1.Y - p2.Y), 2));
}
}
}
--------------------编程问答--------------------
求中点的迭代?
貌似((x1+x2)/2,(y1+y2)/2)直接就是中点了……
真是没困难创造困难也要上啊……
思路:
1.两个方向步进,A到B和B到A一起进行,每次步进A和B之间的距离的1/3,简单点的计算就是(Xa-Xb)/3和(Ya-Yb)/3;
2.如果新的两个间的距离没达到要求,重新计算两点间的距离,重复第一步。
3.最终的两个点任选一个当结果吧。
补充:.NET技术 , C#