vector2.vala 935 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. [Compact]
  8. public struct Vector2
  9. {
  10. public double x;
  11. public double y;
  12. public Vector2(double x, double y)
  13. {
  14. this.x = x;
  15. this.y = y;
  16. }
  17. public Vector2.from_array(Gee.ArrayList<Value?> arr)
  18. {
  19. this.x = (double)arr[0];
  20. this.y = (double)arr[1];
  21. }
  22. public Gee.ArrayList<Value?> to_array()
  23. {
  24. Gee.ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  25. arr.add(this.x);
  26. arr.add(this.y);
  27. return arr;
  28. }
  29. public double dot(Vector2 b)
  30. {
  31. return this.x * b.x + this.y * b.y;
  32. }
  33. public double length_squared()
  34. {
  35. return dot(this);
  36. }
  37. public double length()
  38. {
  39. return Math.sqrt(length_squared());
  40. }
  41. public string to_string()
  42. {
  43. return "%f, %f".printf(x, y);
  44. }
  45. }
  46. public const Vector2 VECTOR2_ZERO = { 0.0, 0.0 };
  47. public const Vector2 VECTOR2_ONE = { 1.0, 1.0 };
  48. } /* namespace Crown */