计算一条线在哪一侧 [英] Calculate on which side of a line a point is

查看:135
本文介绍了计算一条线在哪一侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要弄清楚如何计算一条线的哪一侧。我正在寻找一种非常快速和简单的碰撞算法,因为我只需知道对象在哪一侧定义碰撞状态。

I need to figure out how to calculate on which side of a line a point is. I'm searching a really fast and simple collision algorithm because I just need to know on what side a object is to define a collision state.

就像:

if(x > line.x)
    return EnumSide.LEFT;

但该行必须是对角线。任何想法?

But the line needs to be diagonally. Any ideas?

推荐答案

给定从点p0(x0,y0)到p1(x1,y1)的有向直线,你可以使用以下条件来确定点p2(x2,y2)是在行的左侧,右侧还是在同一行:

Given a directed line from point p0(x0, y0) to p1(x1, y1), you can use the following condition to decide whether a point p2(x2, y2) is on the left of the line, on the right, or on the same line:

value = (x1 - x0)(y2 - y0) - (x2 - x0)(y1 - y0)

value = (x1 - x0)(y2 - y0) - (x2 - x0)(y1 - y0)

如果值> 0,则p2位于该行的左侧。

如果值= 0,则p2在同一行。

如果值< 0,p2位于该行的右侧。

if value > 0, p2 is on the left side of the line.
if value = 0, p2 is on the same line.
if value < 0, p2 is on the right side of the line.

这是一个可以解释这一切的数字:

And here's a figure to explain it all:

这篇关于计算一条线在哪一侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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