CoordinatesCalculator.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace PixiEditorDotNetCore3.Models
  5. {
  6. public static class CoordinatesCalculator
  7. {
  8. /// <summary>
  9. /// Calculates center of thickness * thickness rectangle
  10. /// </summary>
  11. /// <param name="startPosition">Top left position of rectangle</param>
  12. /// <param name="thickness">Thickness of rectangle</param>
  13. /// <returns></returns>
  14. public static DoubleCords CalculateThicknessCenter(Coordinates startPosition, int thickness)
  15. {
  16. int x1, x2, y1, y2;
  17. if (thickness % 2 == 0)
  18. {
  19. x2 = startPosition.X + thickness / 2;
  20. y2 = startPosition.Y + thickness / 2;
  21. x1 = x2 - thickness;
  22. y1 = y2 - thickness;
  23. }
  24. else
  25. {
  26. x2 = startPosition.X + (((thickness - 1) / 2) + 1);
  27. y2 = startPosition.Y + (((thickness - 1) / 2) + 1);
  28. x1 = x2 - thickness;
  29. y1 = y2 - thickness;
  30. }
  31. return new DoubleCords(new Coordinates(x1, y1), new Coordinates(x2, y2));
  32. }
  33. public static Coordinates[] RectangleToCoordinates(int x1, int y1, int x2, int y2)
  34. {
  35. List<Coordinates> coordinates = new List<Coordinates>();
  36. for (int y = y1; y < y1 + (y2 - y1); y++)
  37. {
  38. for (int x = x1; x < x1 + (x2 - x1); x++)
  39. {
  40. coordinates.Add(new Coordinates(x, y));
  41. }
  42. }
  43. return coordinates.ToArray();
  44. }
  45. }
  46. }