matrix.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2016 The G3N 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 collision
  5. // Matrix is a triangular collision matrix indicating which pairs of bodies are colliding.
  6. type Matrix [][]bool
  7. // NewMatrix creates and returns a pointer to a new collision Matrix.
  8. func NewMatrix() Matrix {
  9. return make([][]bool, 0)
  10. }
  11. // Set sets whether i and j are colliding.
  12. func (m *Matrix) Set(i, j int, val bool) {
  13. var s, l int
  14. if i < j {
  15. s = i
  16. l = j
  17. } else {
  18. s = j
  19. l = i
  20. }
  21. diff := s + 1 - len(*m)
  22. if diff > 0 {
  23. for i := 0; i < diff; i++ {
  24. *m = append(*m, make([]bool, 0))
  25. }
  26. }
  27. for idx := range *m {
  28. diff = l + 1 - len((*m)[idx]) - idx
  29. if diff > 0 {
  30. for i := 0; i < diff; i++ {
  31. (*m)[idx] = append((*m)[idx], false)
  32. }
  33. }
  34. }
  35. (*m)[s][l-s] = val
  36. }
  37. // Get returns whether i and j are colliding.
  38. func (m *Matrix) Get(i, j int) bool {
  39. var s, l int
  40. if i < j {
  41. s = i
  42. l = j
  43. } else {
  44. s = j
  45. l = i
  46. }
  47. return (*m)[s][l-s]
  48. }
  49. // Reset clears all values.
  50. func (m *Matrix) Reset() {
  51. *m = make([][]bool, 0)
  52. }