2
0

Vector2.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // file: core/math/math_2d.h
  2. // commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
  3. // file: core/math/math_2d.cpp
  4. // commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
  5. // file: core/variant_call.cpp
  6. // commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
  7. using System;
  8. using System.Runtime.InteropServices;
  9. #if REAL_T_IS_DOUBLE
  10. using real_t = System.Double;
  11. #else
  12. using real_t = System.Single;
  13. #endif
  14. namespace Godot
  15. {
  16. [StructLayout(LayoutKind.Sequential)]
  17. public struct Vector2 : IEquatable<Vector2>
  18. {
  19. public real_t x;
  20. public real_t y;
  21. public real_t this[int index]
  22. {
  23. get
  24. {
  25. switch (index)
  26. {
  27. case 0:
  28. return x;
  29. case 1:
  30. return y;
  31. default:
  32. throw new IndexOutOfRangeException();
  33. }
  34. }
  35. set
  36. {
  37. switch (index)
  38. {
  39. case 0:
  40. x = value;
  41. return;
  42. case 1:
  43. y = value;
  44. return;
  45. default:
  46. throw new IndexOutOfRangeException();
  47. }
  48. }
  49. }
  50. internal void Normalize()
  51. {
  52. real_t length = x * x + y * y;
  53. if (length != 0f)
  54. {
  55. length = Mathf.Sqrt(length);
  56. x /= length;
  57. y /= length;
  58. }
  59. }
  60. public real_t Cross(Vector2 b)
  61. {
  62. return x * b.y - y * b.x;
  63. }
  64. public Vector2 Abs()
  65. {
  66. return new Vector2(Mathf.Abs(x), Mathf.Abs(y));
  67. }
  68. public real_t Angle()
  69. {
  70. return Mathf.Atan2(y, x);
  71. }
  72. public real_t AngleTo(Vector2 to)
  73. {
  74. return Mathf.Atan2(Cross(to), Dot(to));
  75. }
  76. public real_t AngleToPoint(Vector2 to)
  77. {
  78. return Mathf.Atan2(y - to.y, x - to.x);
  79. }
  80. public real_t Aspect()
  81. {
  82. return x / y;
  83. }
  84. public Vector2 Bounce(Vector2 n)
  85. {
  86. return -Reflect(n);
  87. }
  88. public Vector2 Ceil()
  89. {
  90. return new Vector2(Mathf.Ceil(x), Mathf.Ceil(y));
  91. }
  92. public Vector2 Clamped(real_t length)
  93. {
  94. var v = this;
  95. real_t l = Length();
  96. if (l > 0 && length < l)
  97. {
  98. v /= l;
  99. v *= length;
  100. }
  101. return v;
  102. }
  103. public Vector2 CubicInterpolate(Vector2 b, Vector2 preA, Vector2 postB, real_t t)
  104. {
  105. var p0 = preA;
  106. var p1 = this;
  107. var p2 = b;
  108. var p3 = postB;
  109. real_t t2 = t * t;
  110. real_t t3 = t2 * t;
  111. return 0.5f * (p1 * 2.0f +
  112. (-p0 + p2) * t +
  113. (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
  114. (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
  115. }
  116. public real_t DistanceSquaredTo(Vector2 to)
  117. {
  118. return (x - to.x) * (x - to.x) + (y - to.y) * (y - to.y);
  119. }
  120. public real_t DistanceTo(Vector2 to)
  121. {
  122. return Mathf.Sqrt((x - to.x) * (x - to.x) + (y - to.y) * (y - to.y));
  123. }
  124. public real_t Dot(Vector2 with)
  125. {
  126. return x * with.x + y * with.y;
  127. }
  128. public Vector2 Floor()
  129. {
  130. return new Vector2(Mathf.Floor(x), Mathf.Floor(y));
  131. }
  132. public bool IsNormalized()
  133. {
  134. return Mathf.Abs(LengthSquared() - 1.0f) < Mathf.Epsilon;
  135. }
  136. public real_t Length()
  137. {
  138. return Mathf.Sqrt(x * x + y * y);
  139. }
  140. public real_t LengthSquared()
  141. {
  142. return x * x + y * y;
  143. }
  144. public Vector2 LinearInterpolate(Vector2 b, real_t t)
  145. {
  146. var res = this;
  147. res.x += t * (b.x - x);
  148. res.y += t * (b.y - y);
  149. return res;
  150. }
  151. public Vector2 Normalized()
  152. {
  153. var result = this;
  154. result.Normalize();
  155. return result;
  156. }
  157. public Vector2 Project(Vector2 onNormal)
  158. {
  159. return onNormal * (Dot(onNormal) / onNormal.LengthSquared());
  160. }
  161. public Vector2 Reflect(Vector2 n)
  162. {
  163. return 2.0f * n * Dot(n) - this;
  164. }
  165. public Vector2 Rotated(real_t phi)
  166. {
  167. real_t rads = Angle() + phi;
  168. return new Vector2(Mathf.Cos(rads), Mathf.Sin(rads)) * Length();
  169. }
  170. public Vector2 Round()
  171. {
  172. return new Vector2(Mathf.Round(x), Mathf.Round(y));
  173. }
  174. public void Set(real_t x, real_t y)
  175. {
  176. this.x = x;
  177. this.y = y;
  178. }
  179. public void Set(Vector2 v)
  180. {
  181. x = v.x;
  182. y = v.y;
  183. }
  184. public Vector2 Slerp(Vector2 b, real_t t)
  185. {
  186. real_t theta = AngleTo(b);
  187. return Rotated(theta * t);
  188. }
  189. public Vector2 Slide(Vector2 n)
  190. {
  191. return this - n * Dot(n);
  192. }
  193. public Vector2 Snapped(Vector2 by)
  194. {
  195. return new Vector2(Mathf.Stepify(x, by.x), Mathf.Stepify(y, by.y));
  196. }
  197. public Vector2 Tangent()
  198. {
  199. return new Vector2(y, -x);
  200. }
  201. // Constants
  202. private static readonly Vector2 _zero = new Vector2(0, 0);
  203. private static readonly Vector2 _one = new Vector2(1, 1);
  204. private static readonly Vector2 _negOne = new Vector2(-1, -1);
  205. private static readonly Vector2 _inf = new Vector2(Mathf.Inf, Mathf.Inf);
  206. private static readonly Vector2 _up = new Vector2(0, -1);
  207. private static readonly Vector2 _down = new Vector2(0, 1);
  208. private static readonly Vector2 _right = new Vector2(1, 0);
  209. private static readonly Vector2 _left = new Vector2(-1, 0);
  210. public static Vector2 Zero { get { return _zero; } }
  211. public static Vector2 NegOne { get { return _negOne; } }
  212. public static Vector2 One { get { return _one; } }
  213. public static Vector2 Inf { get { return _inf; } }
  214. public static Vector2 Up { get { return _up; } }
  215. public static Vector2 Down { get { return _down; } }
  216. public static Vector2 Right { get { return _right; } }
  217. public static Vector2 Left { get { return _left; } }
  218. // Constructors
  219. public Vector2(real_t x, real_t y)
  220. {
  221. this.x = x;
  222. this.y = y;
  223. }
  224. public Vector2(Vector2 v)
  225. {
  226. x = v.x;
  227. y = v.y;
  228. }
  229. public static Vector2 operator +(Vector2 left, Vector2 right)
  230. {
  231. left.x += right.x;
  232. left.y += right.y;
  233. return left;
  234. }
  235. public static Vector2 operator -(Vector2 left, Vector2 right)
  236. {
  237. left.x -= right.x;
  238. left.y -= right.y;
  239. return left;
  240. }
  241. public static Vector2 operator -(Vector2 vec)
  242. {
  243. vec.x = -vec.x;
  244. vec.y = -vec.y;
  245. return vec;
  246. }
  247. public static Vector2 operator *(Vector2 vec, real_t scale)
  248. {
  249. vec.x *= scale;
  250. vec.y *= scale;
  251. return vec;
  252. }
  253. public static Vector2 operator *(real_t scale, Vector2 vec)
  254. {
  255. vec.x *= scale;
  256. vec.y *= scale;
  257. return vec;
  258. }
  259. public static Vector2 operator *(Vector2 left, Vector2 right)
  260. {
  261. left.x *= right.x;
  262. left.y *= right.y;
  263. return left;
  264. }
  265. public static Vector2 operator /(Vector2 vec, real_t scale)
  266. {
  267. vec.x /= scale;
  268. vec.y /= scale;
  269. return vec;
  270. }
  271. public static Vector2 operator /(Vector2 left, Vector2 right)
  272. {
  273. left.x /= right.x;
  274. left.y /= right.y;
  275. return left;
  276. }
  277. public static bool operator ==(Vector2 left, Vector2 right)
  278. {
  279. return left.Equals(right);
  280. }
  281. public static bool operator !=(Vector2 left, Vector2 right)
  282. {
  283. return !left.Equals(right);
  284. }
  285. public static bool operator <(Vector2 left, Vector2 right)
  286. {
  287. if (left.x.Equals(right.x))
  288. {
  289. return left.y < right.y;
  290. }
  291. return left.x < right.x;
  292. }
  293. public static bool operator >(Vector2 left, Vector2 right)
  294. {
  295. if (left.x.Equals(right.x))
  296. {
  297. return left.y > right.y;
  298. }
  299. return left.x > right.x;
  300. }
  301. public static bool operator <=(Vector2 left, Vector2 right)
  302. {
  303. if (left.x.Equals(right.x))
  304. {
  305. return left.y <= right.y;
  306. }
  307. return left.x <= right.x;
  308. }
  309. public static bool operator >=(Vector2 left, Vector2 right)
  310. {
  311. if (left.x.Equals(right.x))
  312. {
  313. return left.y >= right.y;
  314. }
  315. return left.x >= right.x;
  316. }
  317. public override bool Equals(object obj)
  318. {
  319. if (obj is Vector2)
  320. {
  321. return Equals((Vector2)obj);
  322. }
  323. return false;
  324. }
  325. public bool Equals(Vector2 other)
  326. {
  327. return x == other.x && y == other.y;
  328. }
  329. public override int GetHashCode()
  330. {
  331. return y.GetHashCode() ^ x.GetHashCode();
  332. }
  333. public override string ToString()
  334. {
  335. return String.Format("({0}, {1})", new object[]
  336. {
  337. x.ToString(),
  338. y.ToString()
  339. });
  340. }
  341. public string ToString(string format)
  342. {
  343. return String.Format("({0}, {1})", new object[]
  344. {
  345. x.ToString(format),
  346. y.ToString(format)
  347. });
  348. }
  349. }
  350. }