Javascript 如何求一个数的负倒数?

Javascript 如何求一个数的负倒数?,javascript,math,language-agnostic,logic,Javascript,Math,Language Agnostic,Logic,我有两个点A和B以及一组点,我想求出每个点到AB线的距离 我首先得到直线的斜率,然后是它的Y截距。接下来,在第二个阶段,我们需要斜率的负倒数 我想要一个函数,它接受任何数字,并返回它的负倒数。类似于let inverseOfSlope=getInverse(斜率)请,谢谢 getDistance(PointA: Object, PointB: Object, Target: Object) { // find slope of the line let slope = (Poin

我有两个点A和B以及一组点,我想求出每个点到AB线的距离

我首先得到直线的斜率,然后是它的Y截距。接下来,在第二个阶段,我们需要斜率的负倒数

我想要一个函数,它接受任何数字,并返回它的负倒数。类似于
let inverseOfSlope=getInverse(斜率)请,谢谢

getDistance(PointA: Object, PointB: Object, Target: Object)
{
    // find slope of the line
    let slope = (PointA.y - PointB.y) / (PointA.x - PointB.x)

    // find y intercept
    let yIntercept = PointA.y + ((-1 * slope) * PointA.x)

    // find inverse negative of the slope
    let newSlope = -1 * getInverse(slope);

    // find new y-intercept
    let NewYintercept = PointA.y + ((-1 * newSlope) * PointA.x)

    // get point of intersection between the two lines
    // by solving the two equations equal to each other

    // calculate distance between target and point of intersection
    // easy

    // return result
}

正如4castle评论的,“数字的负倒数”是一件小事,与您的问题没有明显的相关性。Wikipedia为您提供了计算点(x0,y0)到两点(x1,y1)和(x2,y2)定义的直线之间的距离的需求,并且该公式可以直接用任何语言实现,而不需要任何getInverse(slope)函数。

数字的负倒数应该是
-1/slope