0

I want to make a function to check if a point is in range of a rhombus or not ,

Bool Conditional::InRange(Point P)
{
    if( (P.x > Position.x-100) && (P.x < Position.x+100) &&
        (P.y > Position.y-60)  && (P.y < Position.y+60))
        return true;
    else
        return false;
}

I end up with a rectangular range how to make this range be a rhombus ?? , where position is the center and the height =120 width =200

Nate
  • 1
  • 2
  • First a tip: any function like `if (foo) return true; else return false;` should be written more simply `return foo;`. – John Zwinck May 01 '13 at 08:13
  • @Nate please refer for mathematical explanation http://math.stackexchange.com/questions/312403/how-do-i-determine-if-a-point-is-within-a-rhombus – anshul garg May 01 '13 at 08:21
  • Are there any geometric restrictions on your rhombus? Does its 'bottom' edge sit flat to your plane? – Anti Earth May 01 '13 at 08:54

1 Answers1

0

Rhombus co-ordinates are A,B,C,D

and the point is P which we need to check.

Find Angles between

pa,pb - angle1

pb,pc - angle2

pc,pd - angle3

pd,pa - angle4

Now if sum of all angles is 360 or -360 then P is inside of ABCD else it lies outside.

Angle You can find using slope intercept form.

i.e. m = (y2-y1)/(x2-x1)

After You Found slope e.g. for pa line segment slope is m1 for pb line segment slope is m2 then angle can be calculated via tan(angle) = (m1-m2)/(1+m1*m2);

OR

you can use this link

http://en.wikipedia.org/wiki/Point_in_polygon
anshul garg
  • 473
  • 3
  • 7
  • Thanks for your reply , but how can I get the angle through the slope ?? – Nate May 01 '13 at 10:05
  • @Nate After You Found slope e.g. for pa line segment slope is m1 and for pb line segment slope is m2 then angle can be calculated via tan(angle) = (m1-m2)/(1+m1*m2); – anshul garg May 01 '13 at 10:25
  • Thanks again I hope am not disturbing you :) , but I need the angle so I get tan inverse for the answer ? – Nate May 01 '13 at 10:37