Search in which area contains a point based on coordinates

I have 4 points that form a quadrangle. The lines cannot intersect or something like that, it should be a square, rectangle, rhombus, parallelogram, etc.

The lines connecting them divide the field into 9 areas. With a square, it will look like a tic-tac-toe (#) board, but with other shapes the lines will be at angles.

A point accidentally falls into this 9-region field. I know the coordinates of a random point, as well as the coordinates of the four-sided 4 corners.

Is there a way to find which field contains a point without using line equations ?

I'm basically looking for something like

if(p.x > q1.x && p.x < q4.x && p.y < q3.y) {
    //It in the top left region
}
etc

I think this is not possible using oblique lines (rather than a square / rectangle) without solving linear equations. But I thought I would first borrow him with mathematicians. THANKS!

+1
source share
3 answers

Everything that you do effectively “uses line equations”, so I'm not sure what to do with this condition. I suppose you just want simple inequalities to check what area the random point is in (x,y), so I'll show you how to do it.

, , , , (0,0), (a,b), (c,d) (a+c,b+d), . , , (a,b) "" (0,0) (c,d) "" (0,0). "" -bx+ay=0 -bx+ay=-bc+ad, , , -bx+cy 0 -bc+ad:

// Assuming -bc+ad is positive
-bx+ay < 0           // it in the "bottom row"
0 < -bx+ay < -bc+ad // it in the "middle row"
-bc+ad < -bx+ay     // it in the "top row"

, "" dx-cy=0 dx-cy=da-bc, , , dx-cy 0 da-cb:

// Still assuming ad-bc is positive
dx-cy < 0           // it in the "left column"
0 < dx-cy < da-cb  // it in the "middle column"
da-cb < dx-cy      // it in the "right column"

, da-cb , " da-cb", " da-cb 0" ", 0". , , (x,y) , .

+2

" ", , , (0,0), (0,1), (1,0), (1,1). , .

+4

, , 9 . tic-tac-toe (#), .

, ( )?

For example, if the points are (-2, 0), (2, 0), (-1, 1), (1, 1), then the "lines ... at angles" will intersect, and it is not clear from which of "9 areas" you would like, say, (0, 10) to be:

http://imgur.com/NaGwJ.png

(please excuse my drawing skills ...)

Unless, of course, “lines cannot intersect or something like that”, do you mean that a quadrangle is always a parallelogram?

+1
source

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


All Articles