python判断点在矩形内_测试点是否在某个矩形中

本文介绍了如何通过2D向量叉乘判断一个点是否位于任意旋转矩形内部。首先确保矩形的顶点按顺时针顺序存储,然后计算点与矩形边界的两个向量的叉乘结果,根据结果的符号判断点的位置。如果点在所有四条边界内侧,则在矩形内;反之则在矩形外。此外,还讨论了避免对大量矩形进行逐个检查的方法,如使用quad-tree或B+树数据结构优化搜索。
摘要由CSDN通过智能技术生成

First, the micro problem. You have an

arbitrarily rotated rectangle, and a

point. Is the point inside the

rectangle?

There are many ways to do this. But

the best, I think, is using the 2d

vector cross product. First, make sure

the points of the rectangle are stored

in clockwise order. Then do the vector

cross product with 1) the vector

formed by the two points of the side

and 2) a vector from the first point

of the side to the test point. Check

the sign of the result - positive is

inside (to the right of) the side,

negative is outside. If it's inside

all four sides, it's inside the

rectangle. Or equivalently, if it's

outside any of the sides, it's outside

the rectangle. More explanation here.

This method will take 3 subtracts per

vector * times 2 vectors per side,

plus one cross product per side which

is three multiplies and two adds. 11

flops per side, 44 flops per

rectangle.

If you don't like the cross product,

then you could do something like:

figure out the inscribed and

circumscribed circles for each

rectangle, check if the point inside

the inscribed one. If so, it's in the

rectangle as well. If not, check if

it's outside the circumscribed

rectangle. If so, it's outside the

rectangle as well. If it falls between

the two circles, you're f****d and you

have to check it the hard way.

Finding if a point is inside a circle

in 2d takes two subtractions and two

squarings (= multiplies), and then you

compare distance squared to avoid

having to do a square root. That's 4

flops, times two circles is 8 flops -

but sometimes you still won't know.

Also this assumes that you don't pay

any CPU time to compute the

circumscribed or inscribed circles,

which may or may not be true depending

on how much pre-computation you're

willing to do on your rectangle set.

In any event, it's probably not a

great idea to test the point against

every rectangle, especially if you

have a hundred million of them.

Which brings us to the macro problem.

How to avoid testing the point against

every single rectangle in the set? In

2D, this is probably a quad-tree

problem. In 3d, what generic_handle

said - an octree. Off the top of my

head, I would probably implement it as

a B+ tree. It's tempting to use d = 5,

so that each node can have up to 4

children, since that maps so nicely

onto the quad-tree abstraction. But if

the set of rectangles is too big to

fit into main memory (not very likely

these days), then having nodes the

same size as disk blocks is probably

the way to go.

Watch out for annoying degenerate

cases, like some data set that has ten

thousand nearly identical rectangles

with centers at the same exact point.

:P

Why is this problem important? It's

useful in computer graphics, to check

if a ray intersects a polygon. I.e.,

did that sniper rifle shot you just

made hit the person you were shooting

at? It's also used in real-time map

software, like say GPS units. GPS

tells you the coordinates you're at,

but the map software has to find where

that point is in a huge amount of map

data, and do it several times per

second.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值