@@ -153,10 +153,28 @@ export class Line extends Shape {
153153 // 检查交点的y坐标是否在垂直线段的范围内
154154 return intersectionY >= Math . min ( yBottom , yTop ) && intersectionY <= Math . max ( yBottom , yTop ) ;
155155 }
156- // 更新 isIntersectingWithHorizontalLine 方法
156+ /**
157+ * 一个线段是否和一个水平线段相交
158+ * this line
159+ * xx
160+ * x
161+ * ├────xxx─────────┤
162+ * xxx
163+ * xxx
164+ * xLeft xxx xRight
165+ *
166+ * @param y
167+ * @param xLeft
168+ * @param xRight
169+ * @returns
170+ */
157171 getIntersectingWithHorizontalLine ( y : number , xLeft : number , xRight : number ) : IntersectionResult {
158- // 如果线段两端点的y坐标都在水平线的同一侧,则不可能相交
159- if ( ( this . start . y - y ) * ( this . end . y - y ) > 0 ) {
172+ // 如果两端点都在水平线段上下两侧区域,则不可能相交
173+ if ( ( this . start . y < y && this . end . y < y ) || ( this . start . y > y && this . end . y > y ) ) {
174+ return { intersects : false } ;
175+ }
176+ // 如果两端点都在水平线段左右两侧区域,则不可能相交
177+ if ( ( this . start . x < xLeft && this . end . x < xLeft ) || ( this . start . x > xRight && this . end . x > xRight ) ) {
160178 return { intersects : false } ;
161179 }
162180
@@ -179,10 +197,30 @@ export class Line extends Shape {
179197 return { intersects : false } ;
180198 }
181199
182- // 更新 isIntersectingWithVerticalLine 方法
200+ /**
201+ * 当前线段和垂直线段相交算法
202+ * start
203+ * x │yTop
204+ * x │
205+ * x │
206+ * x│
207+ * x end
208+ * │x
209+ * │
210+ * │yBottom
211+ * @param x
212+ * @param yBottom
213+ * @param yTop
214+ * @returns
215+ */
183216 getIntersectingWithVerticalLine ( x : number , yBottom : number , yTop : number ) : IntersectionResult {
184217 // 如果线段两端点的x坐标都在垂直线的同一侧,则不可能相交
185- if ( ( this . start . x - x ) * ( this . end . x - x ) > 0 ) {
218+ if ( ( this . start . x < x && this . end . x < x ) || ( this . start . x > x && this . end . x > x ) ) {
219+ return { intersects : false } ;
220+ }
221+ // 如果线段两端都在顶部或底部,则不可能相交
222+ // 这里注意y坐标的顺序,yTop在yBottom的上方
223+ if ( ( this . start . y > yBottom && this . end . y > yBottom ) || ( this . start . y < yTop && this . end . y < yTop ) ) {
186224 return { intersects : false } ;
187225 }
188226
0 commit comments