vec3.go 962 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. package f32
  5. import "fmt"
  6. type Vec3 [3]float32
  7. func (v Vec3) String() string {
  8. return fmt.Sprintf("Vec3[% 0.3f, % 0.3f, % 0.3f]", v[0], v[1], v[2])
  9. }
  10. func (v *Vec3) Normalize() {
  11. sq := v.Dot(v)
  12. inv := 1 / Sqrt(sq)
  13. v[0] *= inv
  14. v[1] *= inv
  15. v[2] *= inv
  16. }
  17. func (v *Vec3) Sub(v0, v1 *Vec3) {
  18. v[0] = v0[0] - v1[0]
  19. v[1] = v0[1] - v1[1]
  20. v[2] = v0[2] - v1[2]
  21. }
  22. func (v *Vec3) Add(v0, v1 *Vec3) {
  23. v[0] = v0[0] + v1[0]
  24. v[1] = v0[1] + v1[1]
  25. v[2] = v0[2] + v1[2]
  26. }
  27. func (v *Vec3) Mul(v0, v1 *Vec3) {
  28. v[0] = v0[0] * v1[0]
  29. v[1] = v0[1] * v1[1]
  30. v[2] = v0[2] * v1[2]
  31. }
  32. func (v *Vec3) Cross(v0, v1 *Vec3) {
  33. v[0] = v0[1]*v1[2] - v0[2]*v1[1]
  34. v[1] = v0[2]*v1[0] - v0[0]*v1[2]
  35. v[2] = v0[0]*v1[1] - v0[1]*v1[0]
  36. }
  37. func (v *Vec3) Dot(v1 *Vec3) float32 {
  38. return v[0]*v1[0] + v[1]*v1[1] + v[2]*v1[2]
  39. }