简体   繁体   English

线和矩形之间的交点

[英]Points of intersection between line and rectangle

概要

I have a given line R defined by an angle α. 我有一个由角度α定义的给定线R. R goes through the origin of my plane. R经过我飞机的起源。 I also do have an rectangle, with known width and height. 我也有一个宽度和高度已知的矩形。 The rectangle has its bottom left corner on the origin. 矩形的左下角位于原点上。

A new line, parallel to R, is defined by a distance L from R (take A, B, and C as examples). 与R平行的新线由距离R的距离L定义(以A,B和C为例)。 I would like to find out the points where the new line intersects the rectangle (like P1 and P2 for the line A, P3 and P4 for B, and P5 and P6 for C). 我想找出新线与矩形相交的点(如A线的P1和P2,B的P3和P4,C的P5和P6)。

What is the best way to find it? 找到它的最佳方法是什么?

Use this page http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/ 使用此页面http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

it gives formula for intersection of two lines. 它给出了两条线交叉的公式。 Intersect with every one of the 4 lines that make up rectangle separately, and then check that u_a (the place of intersection parametrized by a rectangle line) is between the correct bounds, to make sure that your line doesnt intersect it outside of the rectangle. 与分别构成矩形的4条线中的每条线相交,然后检查u_a(由矩形线参数化的交点)是否在正确的边界之间,以确保您的线与矩形之外的线不相交。

Note you'll need actual points not angles for this, but it is very easy to compute them. 请注意,您需要实际的点而不是角度,但计算它们非常容易。 Line going through origin is simply (0,0)->(cos(a), sin(a)) 穿过原点的线只是(0,0) - >(cos(a),sin(a))

Line x distance away from it, parallel is (0,0) + x*(sin(a),-cos(a)) -> (cos(a),sin(a)) + x*(sin(a),-cos(a)) 线x距离它的距离,平行是(0,0)+ x *(sin(a), - cos(a)) - >(cos(a),sin(a))+ x *(sin(a) ,-cos(a))的

because as you can notice, (sin(a), -cos(a)) is just a unit length vector that is perpendicular to your line, so you just add it on top of both points that form your original line. 因为你可以注意到,(sin(a), - cos(a))只是一个垂直于你的直线的单位长度向量,所以你只需将它添加到形成原始直线的两个点的顶部。

  1. knowing R(x) and distance L you can easily get function for B(x) 知道R(x)和距离L你很容易得到B(x)的函数
  2. rectangle can be represented as 4 lines, ie 4 simple functions R1(x), R2(x), R3(x), R4(x) 矩形可以表示为4行,即4个简单函数R1(x),R2(x),R3(x),R4(x)
  3. you have to solve 4 combined equitations: {A(x);R1(x)}, {A(x);R2(x)}, etc 你必须解决4个组合的等式:{A(x); R1(x)},{A(x); R2(x)}等
  4. check found intersections with lines whether they are in bounds of rectangle using rectangle's base point, width and height (and angle of inclination in general case) 使用矩形的基点,宽度和高度(以及一般情况下的倾斜角度)检查是否有线条的交叉点是否在矩形的边界内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM