PointEx.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. namespace FF8
  4. {
  5. /// <summary>
  6. /// class to add offset to point
  7. /// </summary>
  8. public static class PointEx
  9. {
  10. #region Methods
  11. public static Point Offset(this ref Point source, Point offset)
  12. {
  13. source = (source.ToVector2() + offset.ToVector2()).ToPoint();
  14. return source;
  15. }
  16. public static Point Offset(this ref Point source, Vector2 offset)
  17. {
  18. source = (source.ToVector2() + offset).ToPoint();
  19. return source;
  20. }
  21. public static Point Transform(this Point point, Matrix matrix)
  22. {
  23. point = Vector2.Transform(point.ToVector2(), Matrix.Invert(matrix)).RoundedPoint();
  24. return point;
  25. }
  26. public static Rectangle Scale(this Rectangle source, Matrix matrix)
  27. {
  28. Vector2 scale = Memory.Scale();
  29. Vector2 loc = source.Location.ToVector2();
  30. source.Offset(matrix.Translation.X, matrix.Translation.Y);
  31. source.Location = Vector2.Transform(loc, Matrix.Invert(matrix)).RoundedPoint();
  32. source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
  33. return source;
  34. }
  35. public static Rectangle Scale(this Rectangle source, Vector2 scale)
  36. {
  37. source.Location = (source.Location.ToVector2() * scale).RoundedPoint();
  38. source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
  39. return source;
  40. }
  41. public static Rectangle Scale(this Rectangle source)
  42. {
  43. Vector2 scale = Memory.Scale();
  44. source.Location = (source.Location.ToVector2() * scale).RoundedPoint();
  45. source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
  46. return source;
  47. }
  48. public static Point Scale(this Point source, Vector2 scale)
  49. {
  50. source = (source.ToVector2() * scale).RoundedPoint();
  51. return source;
  52. }
  53. public static Point Scale(this Point source)
  54. {
  55. Vector2 scale = Memory.Scale();
  56. source = (source.ToVector2() * scale).RoundedPoint();
  57. return source;
  58. }
  59. public static Point RoundedPoint(this Vector2 v) => new Point((int)Math.Round(v.X), (int)Math.Round(v.Y));
  60. public static Point CeilingPoint(this Vector2 v) => v.Ceiling().ToPoint();
  61. public static Point FloorPoint(this Vector2 v) => v.Floor().ToPoint();
  62. public static Vector2 Round(this Vector2 v) => new Vector2 ((float)Math.Round(v.X), (float)Math.Round(v.Y));
  63. public static Vector2 Ceiling(this Vector2 v) => new Vector2((float)Math.Ceiling(v.X), (float)Math.Ceiling(v.Y));
  64. public static Vector2 Floor(this Vector2 v) => new Vector2((float)Math.Floor(v.X), (float)Math.Floor(v.Y));
  65. public static Vector2 FloorOrCeiling(this Vector2 v, Vector2 target)
  66. {
  67. float X, Y;
  68. X = v.X < target.X ? (float)Math.Ceiling(v.X) : (float)Math.Floor(v.X);
  69. Y = v.Y < target.Y ? (float)Math.Ceiling(v.Y) : (float)Math.Floor(v.Y);
  70. return new Vector2(X, Y);
  71. }
  72. #endregion Methods
  73. }
  74. }