看看下面的图片。
总图像约为 300X300。里面有 1 个菱形形状。我知道它的要点如下
pointA = new Point(0, 183);
pointB = new Point(183, 0);
pointC = new Point(366, 183);
pointD = new Point(183, 366);
如果我触摸整个图像,如何检测触摸点是在钻石区域内还是在外面?我也看过这个链接,但有些地方不明白。
What you are referring to is the L1 Norm, or Manhattan Distance. To test if your clicked point is inside your diamond (or less than an L1 norm of 183, all you need to do is do the following (in pseudo-code):
isInside(ClickedPoint)
{
X=abs(ClickedPoint.x-183);
Y=abs(Clickedpoint.y-183);
if (X+Y<=183) return inside
else return outside
}
Sorry for not including true Java code, but that shouldn't be too hard to code up.
从您创建一个Shape
对象Points
并
检查该点是否存在于Shpae
我不确定的内部,但它应该类似于这个...
Rectangle rect = new Rectangle();//use your points co-ordinates
if (rect.contains(x,y))
{
//isinside
}
您要做的就是将触摸点和菱形点旋转 45 度:
public Point rotatePoint(Point pt, Point center)
{
double cosAngle = Math.cos(45);
double sinAngle = Math.sin(45);
double dx = (pt.x-center.x);
double dy = (pt.y-center.y);
pt.x = center.x + (int) (dx*cosAngle-dy*sinAngle);
pt.y = center.y + (int) (dx*sinAngle+dy*cosAngle);
return pt;
}
从点创建一个 Rect:
Point centerPoint = new Point(183,183);
Rect r = new Rect(rotatePoint(pointA, centerPoint).x, rotatePoint(pointA, centerPoint).y, rotatePoint(pointC, centerPoint).x, rotatePoint(pointC, centerPoint).y);
然后使用 test 如果它包含该点:
r.contains(rotatePoint(ClickedPoint, centerPoint))
如果该点在菱形中,这将返回 true。