Browse Source

Minor Changes, bugfixes. Start US 13

- Made some seperation of converns related changes:
	- Moved Geometry related calculations (i.e. Bresenham) into
	static GeometryCalculator Class.
- Streamlined drawing and canvas code in Form1, by moving functionality
into helping functions.
- Fixed a bug where drawing, making the window smaller and then deleting
a line would update the pixeldensity of the canvas.
- Fixed unreliable deleting caused by the curserPositions queue always
being filled and emptied immediatly.
- Moved userstory.md files around, updated them
- Prepared for starting Userstory 13
Martin Edlund 6 years ago
parent
commit
910e89142c

+ 13 - 0
Finished Userstories/userstory12.md

@@ -0,0 +1,13 @@
+# Userstory 12  
+ 
+|**ID**|12|  
+|-|-|
+|**Name**|Darstellen einer Zeichnung in der linken Picture Box|
+|**Beschreibung**|In der Linken picture box kann ein Bild angezeigt werden, was Line Elemente des Programms verwendet.|
+|**Akzeptanzkriterium**|Die Userstory ist akzeptiert, wenn das Programm ein Bild in bestehend aus den internen Line Elementen in der linken picture box darstellen kann.|
+|Geschätzter Aufwand (Story Points)|6|
+|Entwickler|Vincenz|
+|Umgesetzt in Iteration|3|
+|Tatsächlicher Aufwand (Std.)|8|
+|Velocity (Std./Story Point)|1.333|
+|Bemerkungen|Keine|

+ 59 - 23
SketchAssistant/SketchAssistant/Form1.cs

@@ -320,18 +320,22 @@ namespace SketchAssistant
         //add a Point on every tick to the Drawpath
         private void mouseTimer_Tick(object sender, EventArgs e)
         {
+            if(cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
+            else { previousCursorPosition = currentCursorPosition; }
             cursorPositions.Enqueue(currentCursorPosition);
-            previousCursorPosition = cursorPositions.Dequeue();
+            //Drawing
             if (currentState.Equals(ProgramState.Draw) && mousePressed)
             {
+                rightGraph = Graphics.FromImage(rightImage);
                 currentLine.Add(currentCursorPosition);
                 Line drawline = new Line(currentLine);
                 drawline.DrawLine(rightGraph);
                 pictureBoxRight.Image = rightImage;
             }
+            //Deleting
             if (currentState.Equals(ProgramState.Delete) && mousePressed)
             {
-                List<Point> uncheckedPoints = Line.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
+                List<Point> uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
                 foreach (Point currPoint in uncheckedPoints)
                 {
                     HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionSize);
@@ -353,6 +357,20 @@ namespace SketchAssistant
         /*** HELPER FUNCTIONS START HERE ***/
         /***********************************/
 
+        /// <summary>
+        /// A function that returns a white canvas for a given width and height.
+        /// </summary>
+        /// <param name="width">The width of the canvas in pixels</param>
+        /// <param name="height">The height of the canvas in pixels</param>
+        /// <returns>The new canvas</returns>
+        private Image GetEmptyCanvas(int width, int height)
+        {
+            Image image = new Bitmap(width, height);
+            Graphics graph = Graphics.FromImage(image);
+            graph.FillRectangle(Brushes.White, 0, 0, width + 10, height + 10);
+            return image;
+        }
+
         /// <summary>
         /// Creates an empty Canvas
         /// </summary>
@@ -360,20 +378,12 @@ namespace SketchAssistant
         {
             if (leftImage == null)
             {
-                rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
-                rightGraph = Graphics.FromImage(rightImage);
-                rightGraph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
-                pictureBoxRight.Image = rightImage;
+                SetAndRefreshRightImage(GetEmptyCanvas(pictureBoxRight.Width, pictureBoxRight.Height));
             }
             else
             {
-                rightImage = new Bitmap(leftImage.Width, leftImage.Height);
-                rightGraph = Graphics.FromImage(rightImage);
-                rightGraph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
-                pictureBoxRight.Image = rightImage;
+                SetAndRefreshRightImage(GetEmptyCanvas(leftImage.Width, leftImage.Height));
             }
-            this.Refresh();
-            pictureBoxRight.Refresh();
         }
 
         /// <summary>
@@ -385,17 +395,12 @@ namespace SketchAssistant
         {
             if (width == 0)
             {
-                leftImage = new Bitmap(pictureBoxLeft.Width, pictureBoxLeft.Height);
+                SetAndRefreshLeftImage(GetEmptyCanvas(pictureBoxLeft.Width, pictureBoxLeft.Height));
             }
             else
             {
-                leftImage = new Bitmap(width, height);
+                SetAndRefreshLeftImage(GetEmptyCanvas(width, height));
             }
-            Graphics.FromImage(leftImage).FillRectangle(Brushes.White, 0, 0, pictureBoxLeft.Width + 10, pictureBoxLeft.Height + 10);
-            pictureBoxLeft.Image = leftImage;
-            
-            this.Refresh();
-            pictureBoxLeft.Refresh();
         }
 
         /// <summary>
@@ -403,17 +408,40 @@ namespace SketchAssistant
         /// </summary>
         private void RedrawRightImage()
         {
-            DrawEmptyCanvasRight();
+            var workingCanvas = GetEmptyCanvas(rightImage.Width, rightImage.Height);
+            var workingGraph = Graphics.FromImage(workingCanvas);
             foreach (Tuple<bool, Line> lineBoolTuple in rightLineList)
             {
                 if (lineBoolTuple.Item1)
                 {
-                    lineBoolTuple.Item2.DrawLine(rightGraph);
+                    lineBoolTuple.Item2.DrawLine(workingGraph);
                 }
             }
+            SetAndRefreshRightImage(workingCanvas);
+        }
+
+        /// <summary>
+        /// A function to set rightImage and to refresh the respective PictureBox with it.
+        /// </summary>
+        /// <param name="image">The new Image</param>
+        private void SetAndRefreshRightImage(Image image)
+        {
+            rightImage = image;
+            pictureBoxRight.Image = rightImage;
             pictureBoxRight.Refresh();
         }
 
+        /// <summary>
+        /// A function to set leftImage and to refresh the respective PictureBox with it.
+        /// </summary>
+        /// <param name="image">The new Image</param>
+        private void SetAndRefreshLeftImage(Image image)
+        {
+            leftImage = image;
+            pictureBoxLeft.Image = leftImage;
+            pictureBoxLeft.Refresh();
+        }
+
         /// <summary>
         /// Change the status of whether or not the lines are shown.
         /// </summary>
@@ -596,6 +624,16 @@ namespace SketchAssistant
         {
             MessageBox.Show(message);
         }
+        
+        private void DisplayPointsInRightCanvas(Point p0, Point p1)
+        {
+
+
+        }
+
+        /********************************************/
+        /*** TESTING RELATED FUNCTIONS START HERE ***/
+        /********************************************/
 
         /// <summary>
         /// returns all instance variables in the order of their definition for testing
@@ -617,7 +655,5 @@ namespace SketchAssistant
             DrawEmptyCanvasLeft(width, height);
             BindAndDrawLeftImage(newImage);
         }
-
-
     }
 }

+ 129 - 0
SketchAssistant/SketchAssistant/GeometryCalculator.cs

@@ -0,0 +1,129 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Drawing;
+
+namespace SketchAssistant
+{
+    /// <summary>
+    /// A class that contains all algorithms related to geometry.
+    /// </summary>
+    public static class GeometryCalculator
+    {
+
+        /// <summary>
+        /// An implementation of the Bresenham Line Algorithm, 
+        /// which calculates all points between two points in a straight line.
+        /// Implemented using the pseudocode on Wikipedia.
+        /// </summary>
+        /// <param name="p0">The start point</param>
+        /// <param name="p1">The end point</param>
+        /// <returns>All points between p0 and p1 (including p0 and p1)</returns>
+        public static List<Point> BresenhamLineAlgorithm(Point p0, Point p1)
+        {
+            int deltaX = p1.X - p0.X;
+            int deltaY = p1.Y - p0.Y;
+            List<Point> returnList;
+
+            if (Math.Abs(deltaY) < Math.Abs(deltaX))
+            {
+                if (p0.X > p1.X)
+                {
+                    returnList = GetLineLow(p1.X, p1.Y, p0.X, p0.Y);
+                    returnList.Reverse();
+                }
+                else
+                {
+                    returnList = GetLineLow(p0.X, p0.Y, p1.X, p1.Y);
+                }
+            }
+            else
+            {
+                if (p0.Y > p1.Y)
+                {
+                    returnList = GetLineHigh(p1.X, p1.Y, p0.X, p0.Y);
+                    returnList.Reverse();
+                }
+                else
+                {
+                    returnList = GetLineHigh(p0.X, p0.Y, p1.X, p1.Y);
+                }
+            }
+            return returnList;
+        }
+
+        /// <summary>
+        /// Helping function of the Bresenham Line algorithm,
+        /// under the assumption that abs(deltaY) is smaller than abs(deltX)
+        /// and x0 is smaller than x1
+        /// </summary>
+        /// <param name="x0">x value of point 0</param>
+        /// <param name="y0">y value of point 0</param>
+        /// <param name="x1">x value of point 1</param>
+        /// <param name="y1">y value of point 1</param>
+        /// <returns>All points on the line between the two points</returns>
+        private static List<Point> GetLineLow(int x0, int y0, int x1, int y1)
+        {
+            List<Point> returnList = new List<Point>();
+            int dx = x1 - x0;
+            int dy = y1 - y0;
+            int yi = 1;
+            if (dy < 0)
+            {
+                yi = -1;
+                dy = -dy;
+            }
+            int D = 2 * dy - dx;
+            int y = y0;
+            for (int x = x0; x <= x1; x++)
+            {
+                returnList.Add(new Point(x, y));
+                if (D > 0)
+                {
+                    y = y + yi;
+                    D = D - 2 * dx;
+                }
+                D = D + 2 * dy;
+            }
+            return returnList;
+        }
+
+        /// <summary>
+        /// Helping function of the Bresenham Line algorithm,
+        /// under the assumption that abs(deltaY) is larger or equal than abs(deltX)
+        /// and y0 is smaller than y1
+        /// </summary>
+        /// <param name="x0">x value of point 0</param>
+        /// <param name="y0">y value of point 0</param>
+        /// <param name="x1">x value of point 1</param>
+        /// <param name="y1">y value of point 1</param>
+        /// <returns>All points on the line between the two points</returns>
+        private static List<Point> GetLineHigh(int x0, int y0, int x1, int y1)
+        {
+            List<Point> returnList = new List<Point>();
+            int dx = x1 - x0;
+            int dy = y1 - y0;
+            int xi = 1;
+            if (dx < 0)
+            {
+                xi = -1;
+                dx = -dx;
+            }
+            int D = 2 * dx - dy;
+            int x = x0;
+            for (int y = y0; y <= y1; y++)
+            {
+                returnList.Add(new Point(x, y));
+                if (D > 0)
+                {
+                    x = x + xi;
+                    D = D - 2 * dy;
+                }
+                D = D + 2 * dx;
+            }
+            return returnList;
+        }
+    }
+}

+ 1 - 114
SketchAssistant/SketchAssistant/Line.cs

@@ -126,7 +126,7 @@ namespace SketchAssistant
                 for (int i = 0; i < linePoints.Count - 1; i++)
                 {
                     nullValue += linePoints[i + 1].X;
-                    List<Point> partialList = BresenhamLineAlgorithm(linePoints[i], linePoints[i + 1]);
+                    List<Point> partialList = GeometryCalculator.BresenhamLineAlgorithm(linePoints[i], linePoints[i + 1]);
                     tempList.AddRange(partialList);
                 }
                 Point nullPoint = new Point(nullValue, 0);
@@ -149,118 +149,5 @@ namespace SketchAssistant
                 linePoints = new List<Point>(newList);
             }
         }
-
-        /// <summary>
-        /// An implementation of the Bresenham Line Algorithm, 
-        /// which calculates all points between two points in a straight line.
-        /// Implemented using the pseudocode on Wikipedia.
-        /// </summary>
-        /// <param name="p0">The start point</param>
-        /// <param name="p1">The end point</param>
-        /// <returns>All points between p0 and p1 (including p0 and p1)</returns>
-        public static List<Point> BresenhamLineAlgorithm(Point p0, Point p1)
-        {
-            int deltaX = p1.X - p0.X;
-            int deltaY = p1.Y - p0.Y;
-            List<Point> returnList;
-
-            if (Math.Abs(deltaY) < Math.Abs(deltaX))
-            {
-                if(p0.X > p1.X)
-                {
-                    returnList = GetLineLow(p1.X, p1.Y, p0.X, p0.Y);
-                    returnList.Reverse();
-                }
-                else
-                {
-                    returnList = GetLineLow(p0.X, p0.Y, p1.X, p1.Y);
-                }
-            }
-            else
-            {
-                if (p0.Y > p1.Y)
-                {
-                    returnList = GetLineHigh(p1.X, p1.Y, p0.X, p0.Y);
-                    returnList.Reverse();
-                }
-                else
-                {
-                    returnList = GetLineHigh(p0.X, p0.Y, p1.X, p1.Y);
-                }
-            }
-            return returnList;
-        }
-
-        /// <summary>
-        /// Helping function of the Bresenham Line algorithm,
-        /// under the assumption that abs(deltaY) is smaller than abs(deltX)
-        /// and x0 is smaller than x1
-        /// </summary>
-        /// <param name="x0">x value of point 0</param>
-        /// <param name="y0">y value of point 0</param>
-        /// <param name="x1">x value of point 1</param>
-        /// <param name="y1">y value of point 1</param>
-        /// <returns>All points on the line between the two points</returns>
-        private static List<Point> GetLineLow(int x0, int y0, int x1, int y1)
-        {
-            List<Point> returnList = new List<Point>();
-            int dx = x1 - x0;
-            int dy = y1 - y0;
-            int yi = 1;
-            if(dy < 0)
-            {
-                yi = -1;
-                dy = -dy;
-            }
-            int D = 2 * dy - dx;
-            int y = y0;
-            for (int x = x0; x <= x1; x++)
-            {
-                returnList.Add(new Point(x, y));
-                if (D > 0)
-                {
-                    y = y + yi;
-                    D = D - 2 * dx;
-                }
-                D = D + 2 * dy;
-            }
-            return returnList;
-        }
-
-        /// <summary>
-        /// Helping function of the Bresenham Line algorithm,
-        /// under the assumption that abs(deltaY) is larger or equal than abs(deltX)
-        /// and y0 is smaller than y1
-        /// </summary>
-        /// <param name="x0">x value of point 0</param>
-        /// <param name="y0">y value of point 0</param>
-        /// <param name="x1">x value of point 1</param>
-        /// <param name="y1">y value of point 1</param>
-        /// <returns>All points on the line between the two points</returns>
-        private static List<Point> GetLineHigh(int x0, int y0, int x1, int y1)
-        {
-            List<Point> returnList = new List<Point>();
-            int dx = x1 - x0;
-            int dy = y1 - y0;
-            int xi = 1;
-            if (dx < 0)
-            {
-                xi = -1;
-                dx = -dx;
-            }
-            int D = 2 * dx - dy;
-            int x = x0;
-            for (int y = y0; y <= y1; y++)
-            {
-                returnList.Add(new Point(x, y));
-                if (D > 0)
-                {
-                    x = x + xi;
-                    D = D - 2 * dy;
-                }
-                D = D + 2 * dx;
-            }
-            return returnList;
-        }
     }
 }

+ 1 - 0
SketchAssistant/SketchAssistant/SketchAssistant.csproj

@@ -71,6 +71,7 @@
   <ItemGroup>
     <Compile Include="FileImporter.cs" />
     <Compile Include="FileImporterException.cs" />
+    <Compile Include="GeometryCalculator.cs" />
     <Compile Include="SketchAction.cs" />
     <Compile Include="ActionHistory.cs" />
     <Compile Include="Line.cs" />

+ 13 - 0
userstory13.md

@@ -0,0 +1,13 @@
+# Userstory 13 
+ 
+|**ID**|13|  
+|-|-|
+|**Name**|Start und Endpunkt anzeigen|
+|**Beschreibung**|Beim Nachzeichnen kann der Start- & Endpunkt einer nachzuzeichnenden Linie angzeigt werden.|
+|**Akzeptanzkriterium**|Sofern auf der linken Seite eine nachzeichenbare Grafik dargestellt wird, wird der Start- und Endpunkt der ersten Linie der linken Grafik an der richtigen Stelle auf der rechten Seite angezeigt.|
+|Geschätzter Aufwand (Story Points)|2|
+|Entwickler|Martin|
+|Umgesetzt in Iteration|keine|
+|Tatsächlicher Aufwand (Std.)|keine|
+|Velocity (Std./Story Point)|keine|
+|Bemerkungen|Keine|