The parallelogram contains a point

What is the fastest way to decide if a point is inside a parallelogram / rhomboid?

+3
source share
8 answers

Hi and thanks for all your answers. In the meantime, I myself came up with what I think would be pretty quick:

Suppose we have a parallelogram spanned by PQ and PR, where PQ and PR are vectors (P, Q and R are angles). In addition, we have a point that we want to check by the name A.

We know that the vector PA can be divided into two vectors parallel to PQ and PR:

PA=n*PQ+m*PR

Now we know that n and m MUST be in the interval [0; 1], we solve n and m:

n = -det(PA, PQ)/det(PQ, PR)
m = det(PA, PR)/det(PQ, PR)

Where det (PA, PQ) is the determinant of the vectors PA and PQ:

det(PA, PQ) = PA.x*PQ.y-PQ.x*PA.y

A , 0 <= n <= 1 0 <= m <= 1, :

var d:Number = det(PQ, PR);
if (0 <= -det(PA, PQ)/d <= 1 && 0 <= det(PA, PR)/d <= 1)
{
    //inside
}
else
{
    //outside
}
+10

, . , . , .

, , . , ActionScript .

, , , , , . , , .

+5

. , . , , , , (0,0), , .

EDIT: , . , PQ PR, P, Q R - . * . Q, , PQ PQ (.. Pq*PQ=0) PR*Pq>0 (, Q, Q P 90 ), R , PR*Pr=0 PQ*Pr>0. A , (0 < Pr*PA < Pr*PQ) && (0 < Pq*PA < Pq*PR).

+2

. , .

, AB AC.

T(a, b) = A + a * AB + b * AC

O D

R(t) = O + t * D

2 , T(a, b) == R(t)

O + t * D = A + a * AB + b * AC

a b , 0 1. . , , .

+1

, ( ) . : (x1, y1) (x2, y2), , (x3, y3), min (x1, x2) < x3 < max (x1, x2) min (y1, y2) < y3 < max (y1, y3).

. , .

, , . ( ) , . , .

, .

[]

, ( , ) ... , , . .

, . ( ), (), , "" . , "", "" , .

: (/) (point + epsilon) ( ).

- . min() max() x, , min() max() y . ... , MMORPG .. , , .

0

ax + bx + c = 0.. , ax + bx + c x, y, a, b, c , , , , , .

, a, b, c , , x, y . .

I.e., a, b, c ,

if ( ((a1*x+b1*y+c1)>0) && ((a2*x+b2*y+c2)<0) && 
        ((a3*x+b3*y+c3)<0) && ((a4*x+b4*y+c4)>0) {
    // it in!
}

.. , "" , .. . - 0,0 , , , , "c" , .

, , .. , , .

0

( , xD), , , , , 4 , .

, , , ( ):

, ?

, , - , , , , , , ( ).

0

y , . y , x. x y , x .

Edit:

, , :

if (y >= y1 && y <= y3) {
   var k = (x4 - x1) / (y4 - y1);
   var m = x1 - k * y1;
   if (x >= k * y + m) {
     k = (x3 - x2) / (y3 - y2);
     m = x2 - k * y2;
     if (x <= k * y + m) {
       // inside
     }
   }
}
-1
source

Source: https://habr.com/ru/post/1714136/


All Articles