Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I know the coordinates of the 4 rhombus' vertices. I also have the coordinates of another arbitrary point (the result of a click on the screen).

How do I determine if that point is within the rhombus?

share|improve this question
    
Are you working in $\mathbb R^2$? (Also, since this is really an implementation problem, you might want to try this question on some of the programming-oriented stack-exchanges instead.) –  Potato Feb 23 '13 at 23:45

4 Answers 4

The interior of a square, rhombus, rectangle, parallelogram, are given in $(x,y)$ coordinates by inequalities. One pair of parallel edges gives $$ E \leq A x + B y \leq F, $$ the other pair of parallel edges $$ G \leq C x + D y \leq H, $$ for constants $A,B,C,D,E,F,G,H.$ If all four inequalities are strict, the new point is strictly inside the figure, if one of the four indicated inequalities is actually an equation the point is on an edge.

Meanwhile, the four boundary equations such as $Ax+By=E$ are just the lines between consectutive vertices of the figure. You need to exercise care in choosing $\pm$ signs throughout, as writing it this way necessitates $E<F$ and $G <H.$

share|improve this answer

Here is one way of doing it, perhaps not the most elegant. First note that the rhombus is the intersection of 4 half planes determined by the lines that bound the rhombus. If you know the 4 points then you can figure out the 4 equations of the lines that bound the rhombus and therefore the 4 inequalities that define the desired half-planes. Therefore a point $p$ will be in the rhombus iff it is in all of the half-planes that determine the rhombus.

share|improve this answer

Let's give the names $A$, $B$, $C$, $D$ to the four vertices of the rhombus. Let $Q$ be the center of the rhombus. You can calculate $Q$ as either $\tfrac{1}{2}(A+C)$ or $\tfrac{1}{2}(B+D)$. Also, let $a = \tfrac{1}{2}\Vert C - A \Vert$ and $b = \tfrac{1}{2}(\Vert D - B \Vert$. We're going to work in a coordinate system that has its origin at $Q$, its $x$-axis along the line $AC$, and its $y$-axis along the line $BD$. Let $U$ and $V$ denote unit vectors along these $x$ and $y$ axes respectively. In fact, $U = (C - A)/2a$ and $V = (D - B)/2b$. All of this is shown in the figure below:

enter image description here

In our chosen coordinate system, $C$ has coordinates $(a,0)$ and $D$ has coordinates $(0,b)$, so the line $DC$ has equation

$$ \frac{x}{a} + \frac{y}{b} = 1 $$

Using symmetry, the other three sides have similar eqautions. I think it's fairly clear that the point $(x,y)$ is inside the rhombus if (and only if):

$$ \frac{\vert x \vert}{a} + \frac{\vert y \vert}{b} \le 1 $$

In your code, you should first pre-compute (once):

Q = 0.5*(A + C);           // center point
a = 0.5*distance(A,C);     // half-width (in the x-direction)
b = 0.5*distance(B,D);     // half-height (y-direction)
U = (C - A)/(2*a);         // unit vector in x-direction
V = (D - B)/(2*b);         // unit vector in y-direction

Then, given a point $P$, the inclusion test is as follows:

W = P - Q;
xabs = abs(W*U);    // here W*U is the dot product of W and U
yabs = abs(W*V);    // here W*V is the dot product of W and V
if (xabs/a + yabs/b <= 1) then inside = true;

So, pretty efficient -- you can decide inclusion just by doing a vector subtraction and two dot products. All of the above assumes you have functions for doing basic vector arithmetic, of course (addition, subtraction, dot products).

share|improve this answer

I would do this with some vector math. Take two edges of your rhombus and consider them unit vectors of a custom coordinate system. Transform the point to this system. If the point has its x coordinate between 0 and 1 as well as its y coordinate between 0 and 1, it is inside.

This should work well for any true rhombus. (Including exactly vertical and horizontal edges, but excluding things with an area of zero)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.