线矩形碰撞检测 [英] line-rectangle collision detection

查看:83
本文介绍了线矩形碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条线(两点(x,y)(x1,y1))和一个具有焦点(rx,ry)的矩形。我需要帮助找出直线和矩形之间的碰撞点,C ++中的示例将为您提供帮助。

I have one line (two point (x,y) (x1,y1)) and a rectangle with focus point (rx,ry). I need help to find out collision point between line and rectangle, an example in C++ will be help.

推荐答案

我不了解如何仅用焦点表示矩形。您需要两个角点或一个角点,并具有一组宽度/高度/旋转数据。

I don't see how you can represent a rectangle with just a "focus point". You'll need either the two corner points or one corner point with a width/height/rotation set of data.

但是,一旦您拥有矩形,我只需将其分解为四行,然后在这四行与要检查的行之间进行拦截检查。

However, once you have a rectangle, I would simply break it down into four lines and do the intercept checks between each of those four lines and the line you want to check against.

在SO上搜索线交叉点会发现很多问题,包括这个,看起来很有希望。实际上,搜索线矩形交集会为您提供这一个,它似乎完全是您想要的东西。

Doing a search on SO for "line intersection" turns up numerous questions, including this one, which seems promising. In fact, searching for "line rectangle intersection" gives you this one, which seems to be exactly what you're after.

这篇关于线矩形碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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