您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

135 行
2.9KB

  1. package util
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. // Point represents a 2 dimensional Point
  7. type Point struct {
  8. X, Y float64
  9. }
  10. // Abs returns a Point with the absolute value of X and Y.
  11. // Should not be confused with Magnitude due to its notation ||A||.
  12. func (p Point) Abs() Point {
  13. return Point{
  14. math.Abs(p.X),
  15. math.Abs(p.Y),
  16. }
  17. }
  18. // Add returns a Point with the value of Point p plus Point q.
  19. func (p Point) Add(q Point) Point {
  20. return Point{
  21. p.X + q.X,
  22. p.Y + q.Y,
  23. }
  24. }
  25. // Subtract returns a Point with the value of Point p minus Point q.
  26. func (p Point) Subtract(q Point) Point {
  27. return Point{
  28. X: p.X - q.X,
  29. Y: p.Y - q.Y,
  30. }
  31. }
  32. // Multiply returns a Point with the value of Point p multiplied by f.
  33. func (p Point) Multiply(f float64) Point {
  34. return Point{X: p.X * f, Y: p.Y * f}
  35. }
  36. // Divide returns a Point with the value of Point p divided by f.
  37. func (p Point) Divide(f float64) Point {
  38. return Point{X: p.X / f, Y: p.Y / f}
  39. }
  40. // Round returns a Point with the rounded value of Point p.
  41. func (p Point) Round() Point {
  42. return Point{X: math.Round(p.X), Y: math.Round(p.Y)}
  43. }
  44. // Floor returns a Point with the floored value of Point p.
  45. func (p Point) Floor() Point {
  46. return Point{X: math.Floor(p.X), Y: math.Floor(p.Y)}
  47. }
  48. // Ceil returns a Point with the rounded up value of Point p.
  49. func (p Point) Ceil() Point {
  50. return Point{X: math.Ceil(p.X), Y: math.Ceil(p.Y)}
  51. }
  52. // Scale returns a Point with the values of Point p scaled up by the values of Point q.
  53. func (p Point) Scale(q Point) Point {
  54. return Point{
  55. X: p.X * q.X,
  56. Y: p.Y * q.Y,
  57. }
  58. }
  59. // ScaleDown returns a Point with the values of Point p scaled down by the values of Point q.
  60. func (p Point) ScaleDown(q Point) Point {
  61. return Point{
  62. X: p.X / q.X,
  63. Y: p.Y / q.Y,
  64. }
  65. }
  66. // Min returns a Point with the smallest values of X and Y for Point p and q.
  67. func (p Point) Min(q Point) Point {
  68. return Point{
  69. X: math.Min(p.X, q.X),
  70. Y: math.Min(p.Y, q.Y),
  71. }
  72. }
  73. // Max returns a Point with the largest values of X and Y for Point p and q.
  74. func (p Point) Max(q Point) Point {
  75. return Point{
  76. X: math.Max(p.X, q.X),
  77. Y: math.Max(p.Y, q.Y),
  78. }
  79. }
  80. // String returns a string with comma separated values of Point p.
  81. func (p Point) String() string {
  82. return fmt.Sprintf("%v,%v", p.X, p.Y)
  83. }
  84. // Magnitude returns a float64 with the length/magnitude of Point p.
  85. func (p Point) Magnitude() float64 {
  86. return math.Sqrt(math.Pow(p.X, 2) + math.Pow(p.Y, 2))
  87. }
  88. func (p Point) Unit() Point {
  89. return p.Divide(p.Magnitude())
  90. }
  91. func (p Point) Rotate(a float64) Point {
  92. return Point{
  93. X: math.Cos(a)*p.X - math.Sin(a)*p.Y,
  94. Y: math.Sin(a)*p.X + math.Cos(a)*p.Y,
  95. }
  96. }
  97. func (p Point) Distance(q Point) float64 {
  98. return p.Subtract(q).Magnitude()
  99. }
  100. func (p Point) Above(l float64) Point {
  101. return p.Subtract(Point{Y: l})
  102. }
  103. func (p Point) Below(l float64) Point {
  104. return p.Add(Point{Y: l})
  105. }
  106. func (p Point) Right(l float64) Point {
  107. return p.Add(Point{X: l})
  108. }
  109. func (p Point) Left(l float64) Point {
  110. return p.Subtract(Point{X: l})
  111. }