path_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2024 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package vector_test
  15. import (
  16. "testing"
  17. "github.com/hajimehoshi/ebiten/v2/vector"
  18. )
  19. func TestIsPointCloseToSegment(t *testing.T) {
  20. testCases := []struct {
  21. p vector.Point
  22. p0 vector.Point
  23. p1 vector.Point
  24. allow float32
  25. want bool
  26. }{
  27. {
  28. p: vector.Point{0.5, 0.5},
  29. p0: vector.Point{0, 0},
  30. p1: vector.Point{1, 0},
  31. allow: 1,
  32. want: true,
  33. },
  34. {
  35. p: vector.Point{0.5, 1.5},
  36. p0: vector.Point{0, 0},
  37. p1: vector.Point{1, 0},
  38. allow: 1,
  39. want: false,
  40. },
  41. {
  42. p: vector.Point{0.5, 0.5},
  43. p0: vector.Point{0, 0},
  44. p1: vector.Point{1, 1},
  45. allow: 0,
  46. want: true,
  47. },
  48. {
  49. p: vector.Point{0, 1},
  50. p0: vector.Point{0, 0},
  51. p1: vector.Point{1, 1},
  52. allow: 0.7,
  53. want: false,
  54. },
  55. {
  56. p: vector.Point{0, 1},
  57. p0: vector.Point{0, 0},
  58. p1: vector.Point{1, 1},
  59. allow: 0.8,
  60. want: true,
  61. },
  62. {
  63. // p0 and p1 are the same.
  64. p: vector.Point{0, 1},
  65. p0: vector.Point{0.5, 0.5},
  66. p1: vector.Point{0.5, 0.5},
  67. allow: 0.7,
  68. want: false,
  69. },
  70. {
  71. // p0 and p1 are the same.
  72. p: vector.Point{0, 1},
  73. p0: vector.Point{0.5, 0.5},
  74. p1: vector.Point{0.5, 0.5},
  75. allow: 0.8,
  76. want: true,
  77. },
  78. }
  79. for _, tc := range testCases {
  80. if got := vector.IsPointCloseToSegment(tc.p, tc.p0, tc.p1, tc.allow); got != tc.want {
  81. t.Errorf("got: %v, want: %v", got, tc.want)
  82. }
  83. }
  84. }