github.com/coming-chat/gomobile@v0.0.0-20220601074111-56995f7d7aba/exp/f32/vec4.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 Vec4 [4]float32 10 11 func (v Vec4) String() string { 12 return fmt.Sprintf("Vec4[% 0.3f, % 0.3f, % 0.3f, % 0.3f]", v[0], v[1], v[2], v[3]) 13 } 14 15 func (v *Vec4) Normalize() { 16 sq := v.Dot(v) 17 inv := 1 / Sqrt(sq) 18 v[0] *= inv 19 v[1] *= inv 20 v[2] *= inv 21 v[3] *= inv 22 } 23 24 func (v *Vec4) Sub(v0, v1 *Vec4) { 25 v[0] = v0[0] - v1[0] 26 v[1] = v0[1] - v1[1] 27 v[2] = v0[2] - v1[2] 28 v[3] = v0[3] - v1[3] 29 } 30 31 func (v *Vec4) Add(v0, v1 *Vec4) { 32 v[0] = v0[0] + v1[0] 33 v[1] = v0[1] + v1[1] 34 v[2] = v0[2] + v1[2] 35 v[3] = v0[3] + v1[3] 36 } 37 38 func (v *Vec4) Mul(v0, v1 *Vec4) { 39 v[0] = v0[0] * v1[0] 40 v[1] = v0[1] * v1[1] 41 v[2] = v0[2] * v1[2] 42 v[3] = v0[3] * v1[3] 43 } 44 45 func (v *Vec4) Dot(v1 *Vec4) float32 { 46 return v[0]*v1[0] + v[1]*v1[1] + v[2]*v1[2] + v[3]*v1[3] 47 }