• 向量的运算


    向量运算

    向量的点乘

    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)
    
    
    
    
    
  • 相关阅读:
    Bridge Design Pattern
    终于写了个自己的简单MVC框架!
    c 第1章的习题 列表 ! :)
    如何创建 linux 下的计划任务运行 php 文件?
    c 的开篇(自学 c 语言)
    一连串问题,都和.net framework有关
    信号与信号量的区别[转]
    永久设置SecureCRT的背景色和文字颜色方案[转]
    do...while(0)的妙用 【转】
    给Ubuntu软件升级命令 [转]
  • 原文地址:https://www.cnblogs.com/pluslius/p/13812366.html
Copyright © 2020-2023  润新知