zoukankan      html  css  js  c++  java
  • 向量的运算

    向量运算

    向量的点乘

    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)
    
    
    
    
    
  • 相关阅读:
    Dart语言简介
    Flutter简介
    Flutter之环境配置详解for mac版本
    mac 安卓生成证书(uniapp项目安卓证书申请)
    IOS开发者账号申请流程以及开发证书与配置文件的使用
    解读typescript中 super关键字的用法
    解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed
    JS pc端和移动端共同实现复制到剪贴板功能实现
    Web前端接入人机识别验证码---腾讯防水墙
    Unity3D Demo项目开发记录
  • 原文地址:https://www.cnblogs.com/pluslius/p/13812366.html
Copyright © 2011-2022 走看看