查找垂直线及其与矩形的交点 [英] Find perpendicular line and its intersection to a rectangle

查看:92
本文介绍了查找垂直线及其与矩形的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3分A,B和C和一个矩形,如下所示.我想知道x,y坐标,来自A的射线将与矩形相交,因为它也必须垂直于BC的线.我知道如何在BC上找到射线将要截取的点,但我似乎无法弄清楚如何从那里延伸以找到它将截取矩形的点.插图:

I have three points A, B & C and a rectangle as shown below. I want to know the x,y coordinate where a ray from A would intersect the rectangle given that it must also be perpendicular to a line from BC. I know how to find the point on BC that the ray would intercept but I can't seem to figure out how to extend from there to find the point it would intercept the rectangle. Illustration:

这是我用来查找BC截距的代码.

Here is the code I'm using to find the BC intercept.

double k = ((By - Cy) * (Ax - Cx) - (Bx - Cx) * (Ay - Cy)) / ((By - Cy) * (By - Cy) + (Bx - Cx) * (Bx - Cx));
double Dx = Ax - k * (By - Cy);
double Dy = Ay + k * (Bx - Cx);

如何扩展Dx和Dy以拦截矩形?

How can I extend Dx and Dy out to intercept the rectangle?

推荐答案

首先弄清楚如何找到两条线的交点(请注意并行情况).

First figure out how to find the intersection of two lines (being wary of the parallel case).

然后如何找到相交点或带有线的射线(如果有的话).

Then how to find the intersection or a ray with a line, if there is one.

然后,如果有一条线,则如何找到射线与 segment 线的交点.

Then how to find the intersection of a ray with a line segment, if there is one.

然后将其应用于A和四个侧面中的每个光线,就完成了.

Then apply that to the ray from A and each of the four sides, and you're done.

提示:您可以将直线或射线表示为矢量和点.这样,您可以定义来自A的射线,而不必担心与BC线(可能不在B和C之间)相交的情况.

Hint: you can represent a line or ray as a vector and a point. This way you can define the ray from A without worrying about the intersection with the line BC (which might not be between B and C).

这篇关于查找垂直线及其与矩形的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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