投影算法【js】

主要用途:计算控制器等比缩放时,鼠标在对角控制器投射位置

如下图,拖动(x2,y2)到m1,m2时,投影位置为(a,b):

相关算法:

const vx1 = x2-x1
const vy1 = y2-y1  
const vx2 = m1-x1
const vy2 = m2-y1  
const v = MathUtil.getVec2ShadowVector(vx2, vy2, vx1, vy1);
const a = v.x+x1
const b = v.y+y1

参考帮助类:

class MathUtil {
    /**
     * 计算向量vec1在vec2的分量
     * @param {Number} vec1
     * @param {Number} vec2
     */
    static getVec2Component(vec1X: number, vec1Y: number, vec2X: number, vec2Y: number) {
        const cross = this.vectorCross(vec1X, vec1Y, vec2X, vec2Y)
        const x = cross < 0 ? -vec2Y : vec2Y
        const y = cross < 0 ? vec2X : -vec2X
        const shadow = this.getVec2ShadowVector(vec1X, vec1Y, x, y)
        return shadow
    }
    /**
     * 计算向量vec1在vec2的投影
     * @param {Number} vec1X
     * @param {Number} vec1Y
     * @param {Number} vec2X
     * @param {Number} vec2Y
     /
    static getVec2ShadowVector(vec1X: number, vec1Y: number, vec2X: number, vec2Y: number) {
        const dot = this.dotCross(vec1X, vec1Y, vec2X, vec2Y)
        const length = this.getVecLength(vec2X, vec2Y)
        const dl = length == 0 ? 0 : dot / length
        const normalx = vec2X / length
        const normaly = vec2Y / length
        return { x: dl  normalx, y: dl * normaly }
    }
    /**
     * 计算投影长度
     * @param {Number} vec1X
     * @param {Number} vec1Y
     * @param {Number} vec2X
     * @param {Number} vec2Y
     * @returns
     /
    static getVec2ShadowLength(vec1X: number, vec1Y: number, vec2X: number, vec2Y: number) {
        const dot = this.dotCross(vec1X, vec1Y, vec2X, vec2Y)
        const length = this.getVecLength(vec2X, vec2Y)
        return length == 0 ? 0 : dot / length
    }

    static vectorCross(vec1X: number, vec1Y: number, vec2X: number, vec2Y: number) {
        return vec1X  vec2Y - vec1Y * vec2X
    }
    static dotCross(vec1X: number, vec1Y: number, vec2X: number, vec2Y: number) {
        return vec1X * vec2X + vec1Y * vec2Y
    }

    static getVecLength(x: number, y: number) {
        return Math.sqrt(x * x + y * y)
    }

    static getRateValue(value: number) {
        let result = value
        if (value <= 5) {
            result = 0.1 + (value / 5) * 0.9
        } else {
            result = 1 + ((value - 5) / 5) * 9
        }
        result = parseFloat(String(Math.round(result * 10) / 10))
        return result
    }
}
export default MathUtil

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值