比较Windows窗体中的两个点 [英] Compare two points in windows form
问题描述
我有两点需要比较两点
点p1 =新点(100,100);
点p2 =新点(105,103);
i如果(p1.Equals(p2))
和if(p1 == p2)
{
}
都没有工作
请告诉我如何保持一些近似并检查两点如p1.x +5和p1.y + 5或p1.x-5或p1.Y-5并与p2进行比较
I have two points I need to compare two points
point p1= new point(100,100);
point p2 = new point(105,103);
i tried if(p1.Equals(p2))
and if(p1==p2)
{
}
both didnt work
please hellp me how to keep some approximation and check two points like p1.x+5 and p1.y+5 or p1.x-5 or p1.Y-5 and compare with p2
推荐答案
我想到了两个想法。您正在寻找的其中一个是什么?
Two ideas come to my mind. Is one of these what you're looking for?
private bool IsManhattanDistanceWithinTolerance(Point p0, Point p1, float tolerance)
{
return(
Math.Abs(p1.X - p0.X) <= tolerance
&& Math.Abs(p1.Y - p1.Y) <= tolerance
);
}
private bool IsEuclidicDistanceWithinTolerance(Point p0, Point p1, float tolerance)
{
return( Math.Sqrt(((p1.X - p0.X) * (p1.X - p0.X)) + ((p1.Y - p0.Y) * (p1.Y - p0.Y))) <= tolerance)
}
您的代码比较了两个Point对象的实例(引用)。基本上,这两个引用都指向相同的内存位置吗?
你需要分别比较每个点的X和Y属性。
Your code compared instances (references) of two Point objects. Basically, are both references pointing to the same memory location?
You need to compare the X and Y properties of each point separately.
if ((p1.X == p2.X) && (p1.Y == p2.Y))
两个单独创建的点的相同值(所有值匹配)的直接比较内置于.NET。
在表单上放置一个TextBox,然后在某处执行这样的代码:
Direct comparison for identical values (all values match) of two separately created Points is built-in to .NET.
Put a TextBox on a Form, and then execute some code like this somewhere:
Point p1 = new Point(100, 100);
Point p2 = new Point(100, 100);
textBox1.Clear();
textBox1.AppendText((p1 == p2).ToString());
你会看到如果所有数字都相同,结果是'true,如果有任何数字是不一样,结果是'假。
你在这里想到了其他一些比较吗?
You'll see that if all numbers are the same, the result is 'true, and if any number is not the same, the result is 'false.
Was there some other kind of comparison you had in mind here ?
这篇关于比较Windows窗体中的两个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!