ShapeTool.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using PixiEditor.Models.Layers;
  2. using PixiEditor.Models.Position;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Windows.Media;
  7. namespace PixiEditor.Models.Tools
  8. {
  9. public abstract class ShapeTool : Tool
  10. {
  11. public override abstract ToolType ToolType { get; }
  12. public abstract override BitmapPixelChanges Use(Layer layer, Coordinates[] coordinates, Color color, int toolSize);
  13. public ShapeTool()
  14. {
  15. RequiresPreviewLayer = true;
  16. }
  17. protected Coordinates[] BresenhamLine(int x1, int y1, int x2, int y2)
  18. {
  19. List<Coordinates> coordinates = new List<Coordinates>();
  20. if(x1 == x2 && y1 == y2)
  21. {
  22. return new Coordinates[] { new Coordinates(x1, y1) };
  23. }
  24. int d, dx, dy, ai, bi, xi, yi;
  25. int x = x1, y = y1;
  26. if (x1 < x2)
  27. {
  28. xi = 1;
  29. dx = x2 - x1;
  30. }
  31. else
  32. {
  33. xi = -1;
  34. dx = x1 - x2;
  35. }
  36. if (y1 < y2)
  37. {
  38. yi = 1;
  39. dy = y2 - y1;
  40. }
  41. else
  42. {
  43. yi = -1;
  44. dy = y1 - y2;
  45. }
  46. coordinates.Add(new Coordinates(x, y));
  47. if (dx > dy)
  48. {
  49. ai = (dy - dx) * 2;
  50. bi = dy * 2;
  51. d = bi - dx;
  52. while (x != x2)
  53. {
  54. if (d >= 0)
  55. {
  56. x += xi;
  57. y += yi;
  58. d += ai;
  59. }
  60. else
  61. {
  62. d += bi;
  63. x += xi;
  64. }
  65. coordinates.Add(new Coordinates(x, y));
  66. }
  67. }
  68. else
  69. {
  70. ai = (dx - dy) * 2;
  71. bi = dx * 2;
  72. d = bi - dy;
  73. while (y != y2)
  74. {
  75. if (d >= 0)
  76. {
  77. x += xi;
  78. y += yi;
  79. d += ai;
  80. }
  81. else
  82. {
  83. d += bi;
  84. y += yi;
  85. }
  86. coordinates.Add(new Coordinates(x, y));
  87. }
  88. }
  89. return coordinates.ToArray();
  90. }
  91. protected DoubleCords CalculateCoordinatesForShapeRotation(Coordinates startingCords, Coordinates secondCoordinates)
  92. {
  93. Coordinates currentCoordinates = secondCoordinates;
  94. if (startingCords.X > currentCoordinates.X && startingCords.Y > currentCoordinates.Y)
  95. {
  96. return new DoubleCords(new Coordinates(currentCoordinates.X, currentCoordinates.Y), new Coordinates(startingCords.X, startingCords.Y));
  97. }
  98. else if (startingCords.X < currentCoordinates.X && startingCords.Y < currentCoordinates.Y)
  99. {
  100. return new DoubleCords(new Coordinates(startingCords.X, startingCords.Y), new Coordinates(currentCoordinates.X, currentCoordinates.Y));
  101. }
  102. else if (startingCords.Y > currentCoordinates.Y)
  103. {
  104. return new DoubleCords(new Coordinates(startingCords.X, currentCoordinates.Y), new Coordinates(currentCoordinates.X, startingCords.Y));
  105. }
  106. else if(startingCords.X > currentCoordinates.X && startingCords.Y <= currentCoordinates.Y)
  107. {
  108. return new DoubleCords(new Coordinates(currentCoordinates.X, startingCords.Y), new Coordinates(startingCords.X, currentCoordinates.Y));
  109. }
  110. else
  111. {
  112. return new DoubleCords(startingCords, secondCoordinates);
  113. }
  114. }
  115. }
  116. }