You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

XY.cs 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. namespace Preparation.Utility
  3. {
  4. public struct XY
  5. {
  6. public int x;
  7. public int y;
  8. public XY(int x, int y)
  9. {
  10. this.x = x;
  11. this.y = y;
  12. }
  13. public XY(double angle, double length)
  14. {
  15. this.x = (int)(length * Math.Cos(angle));
  16. this.y = (int)(length * Math.Sin(angle));
  17. }
  18. public XY(XY Direction, double length)
  19. {
  20. if (Direction.x == 0 && Direction.y == 0)
  21. {
  22. this.x = 0;
  23. this.y = 0;
  24. }
  25. else
  26. {
  27. this.x = (int)(length * Math.Cos(Direction.Angle()));
  28. this.y = (int)(length * Math.Sin(Direction.Angle()));
  29. }
  30. }
  31. public override string ToString()
  32. {
  33. return "(" + x.ToString() + "," + y.ToString() + ")";
  34. }
  35. public static int operator *(XY v1, XY v2)
  36. {
  37. return (v1.x * v2.x) + (v1.y * v2.y);
  38. }
  39. public static XY operator *(int a, XY v2)
  40. {
  41. return new XY(a * v2.x, a * v2.y);
  42. }
  43. public static XY operator *(XY v2, int a)
  44. {
  45. return new XY(a * v2.x, a * v2.y);
  46. }
  47. public static XY operator +(XY v1, XY v2)
  48. {
  49. return new XY(v1.x + v2.x, v1.y + v2.y);
  50. }
  51. public static XY operator -(XY v1, XY v2)
  52. {
  53. return new XY(v1.x - v2.x, v1.y - v2.y);
  54. }
  55. public static bool operator ==(XY v1, XY v2)
  56. {
  57. return v1.x == v2.x && v1.y == v2.y;
  58. }
  59. public static bool operator !=(XY v1, XY v2)
  60. {
  61. return v1.x != v2.x || v1.y != v2.y;
  62. }
  63. public static double DistanceFloor3(XY p1, XY p2)
  64. {
  65. long c = (((long)(p1.x - p2.x) * (p1.x - p2.x)) + ((long)(p1.y - p2.y) * (p1.y - p2.y))) * 1000000;
  66. long t = c / 2 + 1;
  67. while (t * t > c || (t + 1) * (t + 1) <= c)
  68. t = (c / t + t) / 2;
  69. return (double)t / 1000.0;
  70. }
  71. public static double DistanceCeil3(XY p1, XY p2)
  72. {
  73. long c = (((long)(p1.x - p2.x) * (p1.x - p2.x)) + ((long)(p1.y - p2.y) * (p1.y - p2.y))) * 1000000;
  74. long t = c / 2 + 1;
  75. while (t * t > c || (t + 1) * (t + 1) <= c)
  76. t = (c / t + t) / 2;
  77. if (t * t == c) return (double)t / 1000.0;
  78. else return (double)(t + 1) / 1000.0;
  79. }
  80. public double Length()
  81. {
  82. return Math.Sqrt(((long)x * x) + ((long)y * y));
  83. }
  84. public double Angle()
  85. {
  86. return Math.Atan2(y, x);
  87. }
  88. public override bool Equals(object? obj) => obj is null || obj is XY ? false : this == (XY)obj;
  89. public override int GetHashCode()
  90. {
  91. return this.x.GetHashCode() ^ this.y.GetHashCode();
  92. }
  93. }
  94. }