点在二维轴对齐的矩形内,没有分支 [英] Point inside 2D axis aligned rectangle, no branches

查看:16
本文介绍了点在二维轴对齐的矩形内,没有分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找最优化的方法来检测点是否在轴对齐的矩形内.

I'm searching for the most optimized method to detect whether a point is inside an axis aligned rectangle.

最简单的解决方案需要 4 个分支 (if),这对性能不利.

The easiest solution needs 4 branches (if) which is bad for performance.

推荐答案

给定一个段 [x0, x1],当 时,一个点 x 在段内>(x0 - x) * (x1 - x) <= 0.

Given a segment [x0, x1], a point x is inside the segment when (x0 - x) * (x1 - x) <= 0.

在二维情况下,你需要做两次,所以它需要两个条件.

In two dimensions case, you need to do it twice, so it requires two conditionals.

这篇关于点在二维轴对齐的矩形内,没有分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆