Vector.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SuperTux Editor
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. namespace DataStructures
  18. {
  19. public struct Vector
  20. {
  21. public float X;
  22. public float Y;
  23. public Vector(float X, float Y)
  24. {
  25. this.X = X;
  26. this.Y = Y;
  27. }
  28. public float Norm()
  29. {
  30. return (float) Math.Sqrt(X*X + Y*Y);
  31. }
  32. public Vector Unit()
  33. {
  34. return this / Norm();
  35. }
  36. public static Vector operator +(Vector v1, Vector v2)
  37. {
  38. return new Vector(v1.X + v2.X, v1.Y + v2.Y);
  39. }
  40. public static Vector operator -(Vector v1, Vector v2)
  41. {
  42. return new Vector(v1.X - v2.X, v1.Y - v2.Y);
  43. }
  44. public static Vector operator -(Vector v)
  45. {
  46. return new Vector(-v.X, -v.Y);
  47. }
  48. public static Vector operator *(Vector v1, float s)
  49. {
  50. return new Vector(v1.X * s, v1.Y * s);
  51. }
  52. /// <summary>scalar product</summary>
  53. public static float operator *(Vector v1, Vector v2)
  54. {
  55. return v1.X * v2.X + v1.Y * v2.Y;
  56. }
  57. public static Vector operator /(Vector v1, float s)
  58. {
  59. return new Vector(v1.X / s, v1.Y / s);
  60. }
  61. public static bool operator ==(Vector v1, Vector v2)
  62. {
  63. return v1.X == v2.X && v1.Y == v2.Y;
  64. }
  65. public static bool operator !=(Vector v1, Vector v2)
  66. {
  67. return v1.X != v2.X || v1.Y != v2.Y;
  68. }
  69. public override bool Equals(object obj)
  70. {
  71. if (!(obj is Vector))
  72. return false;
  73. Vector ov = (Vector) obj;
  74. return this == ov;
  75. }
  76. public override int GetHashCode()
  77. {
  78. return base.GetHashCode();
  79. }
  80. public override string ToString()
  81. {
  82. return "[" + X + ";" + Y + "]";
  83. }
  84. }
  85. }
  86. /* EOF */