ShapeTool.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using PixiEditor.Models.Layers;
  2. using PixiEditor.Models.Position;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows.Input;
  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. Cursor = Cursors.Cross;
  17. }
  18. protected Coordinates[] BresenhamLine(int x1, int y1, int x2, int y2)
  19. {
  20. List<Coordinates> coordinates = new List<Coordinates>();
  21. if(x1 == x2 && y1 == y2)
  22. {
  23. return new Coordinates[] { new Coordinates(x1, y1) };
  24. }
  25. int d, dx, dy, ai, bi, xi, yi;
  26. int x = x1, y = y1;
  27. if (x1 < x2)
  28. {
  29. xi = 1;
  30. dx = x2 - x1;
  31. }
  32. else
  33. {
  34. xi = -1;
  35. dx = x1 - x2;
  36. }
  37. if (y1 < y2)
  38. {
  39. yi = 1;
  40. dy = y2 - y1;
  41. }
  42. else
  43. {
  44. yi = -1;
  45. dy = y1 - y2;
  46. }
  47. coordinates.Add(new Coordinates(x, y));
  48. if (dx > dy)
  49. {
  50. ai = (dy - dx) * 2;
  51. bi = dy * 2;
  52. d = bi - dx;
  53. while (x != x2)
  54. {
  55. if (d >= 0)
  56. {
  57. x += xi;
  58. y += yi;
  59. d += ai;
  60. }
  61. else
  62. {
  63. d += bi;
  64. x += xi;
  65. }
  66. coordinates.Add(new Coordinates(x, y));
  67. }
  68. }
  69. else
  70. {
  71. ai = (dx - dy) * 2;
  72. bi = dx * 2;
  73. d = bi - dy;
  74. while (y != y2)
  75. {
  76. if (d >= 0)
  77. {
  78. x += xi;
  79. y += yi;
  80. d += ai;
  81. }
  82. else
  83. {
  84. d += bi;
  85. y += yi;
  86. }
  87. coordinates.Add(new Coordinates(x, y));
  88. }
  89. }
  90. return coordinates.ToArray();
  91. }
  92. protected DoubleCords CalculateCoordinatesForShapeRotation(Coordinates startingCords, Coordinates secondCoordinates)
  93. {
  94. Coordinates currentCoordinates = secondCoordinates;
  95. if (startingCords.X > currentCoordinates.X && startingCords.Y > currentCoordinates.Y)
  96. {
  97. return new DoubleCords(new Coordinates(currentCoordinates.X, currentCoordinates.Y), new Coordinates(startingCords.X, startingCords.Y));
  98. }
  99. else if (startingCords.X < currentCoordinates.X && startingCords.Y < currentCoordinates.Y)
  100. {
  101. return new DoubleCords(new Coordinates(startingCords.X, startingCords.Y), new Coordinates(currentCoordinates.X, currentCoordinates.Y));
  102. }
  103. else if (startingCords.Y > currentCoordinates.Y)
  104. {
  105. return new DoubleCords(new Coordinates(startingCords.X, currentCoordinates.Y), new Coordinates(currentCoordinates.X, startingCords.Y));
  106. }
  107. else if(startingCords.X > currentCoordinates.X && startingCords.Y <= currentCoordinates.Y)
  108. {
  109. return new DoubleCords(new Coordinates(currentCoordinates.X, startingCords.Y), new Coordinates(startingCords.X, currentCoordinates.Y));
  110. }
  111. else
  112. {
  113. return new DoubleCords(startingCords, secondCoordinates);
  114. }
  115. }
  116. }
  117. }