【发布时间】:2020-12-17 07:06:36
【问题描述】:
我遇到以下问题:
给定:
- 具有定义的高度 (Y) 和宽度 (X) 的矩形
- 由点 A 和 B 给出的线段
- C 段内的一个点
找到点 D 和 E:
- 与矩形相交
- 形成一条穿过 C 的线段
- 形成一条垂直于线段 AB 的线段
为了解决这个问题,我首先尝试计算斜率并创建一个线函数,但我看到的所有获得线和多边形交点的答案都使用线段而不是线函数。我该如何解决这个问题?我是否错过了一种更好的方法来找到不需要函数的垂直线?
function getPerpendicular(ax,ay,bx,by,cx,cy,x,y){
let a=bx-ax;
let b=by-ay;
let slope;
let line;
// Because if a==0 the slope is infinite
if(a===0){
line=function(y){
return cx;
}
}else{
slope= (b)/(-a);
line=function(x){
return slope*x+cy-cx;
}
}
// Intersection with the line function?
}
【问题讨论】:
标签: javascript math geometry 2d