github.com/MrKrisYu/mobile@v0.0.0-20230923092425-9be92a9aeacc/exp/f32/vec3.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package f32
     6  
     7  import "fmt"
     8  
     9  type Vec3 [3]float32
    10  
    11  func (v Vec3) String() string {
    12  	return fmt.Sprintf("Vec3[% 0.3f, % 0.3f, % 0.3f]", v[0], v[1], v[2])
    13  }
    14  
    15  func (v *Vec3) Normalize() {
    16  	sq := v.Dot(v)
    17  	inv := 1 / Sqrt(sq)
    18  	v[0] *= inv
    19  	v[1] *= inv
    20  	v[2] *= inv
    21  }
    22  
    23  func (v *Vec3) Sub(v0, v1 *Vec3) {
    24  	v[0] = v0[0] - v1[0]
    25  	v[1] = v0[1] - v1[1]
    26  	v[2] = v0[2] - v1[2]
    27  }
    28  
    29  func (v *Vec3) Add(v0, v1 *Vec3) {
    30  	v[0] = v0[0] + v1[0]
    31  	v[1] = v0[1] + v1[1]
    32  	v[2] = v0[2] + v1[2]
    33  }
    34  
    35  func (v *Vec3) Mul(v0, v1 *Vec3) {
    36  	v[0] = v0[0] * v1[0]
    37  	v[1] = v0[1] * v1[1]
    38  	v[2] = v0[2] * v1[2]
    39  }
    40  
    41  func (v *Vec3) Cross(v0, v1 *Vec3) {
    42  	v[0] = v0[1]*v1[2] - v0[2]*v1[1]
    43  	v[1] = v0[2]*v1[0] - v0[0]*v1[2]
    44  	v[2] = v0[0]*v1[1] - v0[1]*v1[0]
    45  }
    46  
    47  func (v *Vec3) Dot(v1 *Vec3) float32 {
    48  	return v[0]*v1[0] + v[1]*v1[1] + v[2]*v1[2]
    49  }