2

Have a look at below image.

enter image description here

Total image is around 300X300. Inside that 1 Diamond shape is there. I know its Points as below

    pointA = new Point(0, 183);
    pointB = new Point(183, 0);
    pointC = new Point(366, 183);
    pointD = new Point(183, 366);

If I touch on this whole image, how can I detect whether touched point is inside Diamond area or outside? I also had a look at this link but could not understand some points.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Braj
  • 2,164
  • 2
  • 26
  • 44

3 Answers3

2

create a Shape object from your Points and
check that point exists inside that Shpae
m not sure about this but it should be similar to this one...

Rectangle rect = new Rectangle();//use your points co-ordinates 
    if (rect.contains(x,y))
     {
       //isinside
     }
Michael Shrestha
  • 2,547
  • 19
  • 30
  • there is also `rect.contains(your_point)` method that can be use.... – CRUSADER Jun 24 '13 at 07:28
  • I tried this. I think its taking Rectangle from (0,0). But, in above image inside object is Diamond shape. So, its returning back wrong result – Braj Jun 24 '13 at 07:38
2

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.

dberm22
  • 3,187
  • 1
  • 31
  • 46
1

What you have to do is rotate the touched point and the diamond points by 45 degrees:

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

create a Rect from the points:

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

then use test if it contains the point:

r.contains(rotatePoint(ClickedPoint, centerPoint))

This will return true if the point is in the diamond.

string.Empty
  • 10,393
  • 4
  • 39
  • 67
  • There is no constructor for Rect with Point as parameter... – Braj Jun 24 '13 at 08:47
  • try `Rect r = new Rect(rotatePoint(pointA, centerPoint).x, rotatePoint(pointA, centerPoint).y, rotatePoint(pointC, centerPoint).x, rotatePoint(pointC, centerPoint).y);` – string.Empty Jun 24 '13 at 08:58
  • anyway its the concept, rotate the diamond into a square then test if the rotated point is in the square. – string.Empty Jun 24 '13 at 09:00
  • its not working :( anyway, thank u for d response – Braj Jun 24 '13 at 09:15
  • The angle given to `sin` and `cos` in this example is in degrees while `Math` expects them in radians. – Lii Jun 04 '14 at 13:24