计算出线在屏幕内的最长坐标

计算出线在屏幕内的最长坐标

/**
* 计算出线在屏幕内的最长坐标
* x1, y1 = 坐标1, x2, y2 = 坐标2
*/
private createAimLinePosArr(x1: number, y1: number, x2: number, y2: number) {
    let posX, posY;
    if (y2 == y1) y1 += 0.01
    if (x2 == x1) x1 += 0.01

    if (y2 < y1) {
        if (x2 < x1) {
            posX = 0
            posY = this.binaryEquationGetY(x1, y1, x2, y2, posX)
        } else {
            posX = Const.STAGE_WIDTH //最大屏幕宽度
            posY = this.binaryEquationGetY(x1, y1, x2, y2, posX)
        }
    } else if (y2 > y1) {
        if (x2 > x1) {
            posX = Const.STAGE_WIDTH //最大屏幕宽度
            posY = this.binaryEquationGetY(x1, y1, x2, y2, posX)
        } else {
            posX = 0
            posY = this.binaryEquationGetY(x1, y1, x2, y2, posX)
        }
    }

    if (posY < Const.STAGE_HEIGHT) {
        if (posY <= 0) {
            posY = 0
            posX = this.binaryEquationGetX(x1, y1, x2, y2, posY)
        }
        return [posX, posY]
    } else {
        posY = Const.STAGE_HEIGHT //最大屏幕高度
        posX = this.binaryEquationGetX(x1, y1, x2, y2, posY)
        if (isNaN(posX)) {
            if (x2 > x1) {
                posX = Const.STAGE_WIDTH
            } else {
                posX = 0
            }
            console.log("posX == NaN", x1, y1, x2, y2, posY)
        }
        return [posX, posY]
    }
}

 

/**
 * 二元一次方程:根据x求y
 */
public binaryEquationGetY(x1, y1, x2, y2, x) {
    let kbArr = this.binaryEquationGetKB(x1, y1, x2, y2)
    let k = kbArr[0]
    let b = kbArr[1]
    return k * x + b
}

 

/**
 * 二元一次方程:根据y求x
 */
public binaryEquationGetX(x1, y1, x2, y2, y) {
    let kbArr = this.binaryEquationGetKB(x1, y1, x2, y2)
    let k = kbArr[0]
    let b = kbArr[1]
    return (y - b) / k
}

 

/**
 * 求二元一次方程的系数
 * y1 = k * x1 + b => k = (y1 - b) / x1
 * y2 = k * x2 + b => y2 = ((y1 - b) / x1) * x2 + b
 */
private binaryEquationGetKB(x1, y1, x2, y2) {
    let k = (y1 - y2) / (x1 - x2)
    let b = (x1 * y2 - x2 * y1) / (x1 - x2)
    return [k, b]
}

 

posted @ 2018-12-03 15:33 HaoK 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值