• 向量的运算


    向量运算

    向量的点乘

    a dot b  theta.                   direction
      > 0    0 < theta < 90.        同向
      0      theta = 90                 垂直
      < 0.   90 < theta < 180     反向
    
    
    class Vector {
        constructor(...components){
            this.components = components
        }
        zero() {
            // 0
            return new Vector(...new Array(this.components.length).fill(0))
        }
        mag() {
            // ||v|| = sqrt(sum(v))
            return Math.hypot(...this.components)
        }
        negative(){
            // v * -1
            return new Vector(...this.mult(-1).components)
        }
        mult(number){
            // v * number
            return new Vector(...this.components.map(component => component * number))
        }
        div(number){
            // v * number
            return new Vector(...this.components.map(component => component / number))
        }
        normalize(){
            // v / ||v||
            return new Vector(...this.div(this.mag()).components)
        }
        add({components}){
            // sum(v1,v2)
            return new Vector(
               ...components.map((component, index) => this.components[index] + component)
            )
        }
        sub({components}){
            // sub(v1,v2)
            return new Vector(
                ...components.map((component, index) => this.components[index] - component)
             )
        }
        dist(v1){
            // ||v2 - v1||
            return this.sub(v1).mag()
        }
        dot({components}){
            // sum(v1 * v2)
            return components.reduce((acc, component, index) => {
                return acc + component * this.components[index]
            },0)
        }
        toDegress(radians){
            // 转角度乘角度除弧度
            return (radians * 180) / Math.PI
        }
        toRadians(degress){
            // 转弧度乘弧度除以角度
            return (degress * Math.PI) / 180
        }
        angleBetween(other){
            // arccos(a dot b / ||a||||b||)
            return this.toDegress(
                Math.acos(
                    this.dot(other) / (this.mag() * other.mag())
                )
            )
        }
    }
    
    const log = console.log
    
    const one = new Vector(3,-2,7)
    const other = new Vector(0,4,-1)
    
    // log(one.dot(other))
    log(one.angleBetween(other))
    
    // const one = new Vector(5,0,0)
    // const other = new Vector(-1,8,0)
    
    // log(one.dist(other))
    
    // const one = new Vector(2, 3)
    // const other = new Vector(2, 1)
    // log(one.add(other))
    // // Vector { components: [ 4, 4 ] }
    // log(one.sub(other))
    // // Vector { components: [ 0, 2 ] }
    // const v1 = new Vector(12,-5,0)
    // log(v1.normalize())
    // const v1 = new Vector(5,-4,7)
    // log(v1)
    // log(v1.negative())
    // log(v1.mag())
    // log(v1.zero())
    // const v2 = v1.mult(2)
    // const v3 = v2.div(2)
    // log(v2)
    // log(v3)
    
    
    
    
    
  • 相关阅读:
    mybatis技术总结
    eclipse与idea部署项目的区别
    jQuery基础总结
    html页面加载顺序
    JavaScript高级技术总结
    JavaScript基础技术总结
    Node.js 从零开发 web server博客项目[数据存储]
    Node.js 从零开发 web server博客项目[koa2重构博客项目]
    Node.js 从零开发 web server博客项目[安全]
    Node.js 从零开发 web server博客项目[日志]
  • 原文地址:https://www.cnblogs.com/pluslius/p/13812366.html
Copyright © 2020-2023  润新知