Browse Source

Implemented Bresenham Line Alg., added more bugs to fix later

Martin Edlund 5 years ago
parent
commit
4496edbd25
3 changed files with 111 additions and 14 deletions
  1. 1 0
      SketchAssistant/SketchAssistant/Form1.cs
  2. 110 1
      SketchAssistant/SketchAssistant/Line.cs
  3. 0 13
      userstory6.md

+ 1 - 0
SketchAssistant/SketchAssistant/Form1.cs

@@ -122,6 +122,7 @@ namespace SketchAssistant
         private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
         {
             currentCursorPosition = ConvertCoordinates(new Point(e.X, e.Y));
+            Console.WriteLine(currentCursorPosition.ToString());
         }
 
         //hold left mouse button to draw.

+ 110 - 1
SketchAssistant/SketchAssistant/Line.cs

@@ -84,7 +84,9 @@ namespace SketchAssistant
         private void CleanPoints()
         {
             List<Point> newList = new List<Point>();
+            List<Point> tempList = new List<Point>();
             Point nullPoint = new Point(-1, -1);
+            //Remove duplicate points
             for (int i = 1; i < linePoints.Count; i++)
             {
                 if ((linePoints[i].X == linePoints[i - 1].X) && (linePoints[i].Y == linePoints[i - 1].Y))
@@ -96,10 +98,117 @@ namespace SketchAssistant
             {
                 if (!(linepoint.X == -1))
                 {
-                    newList.Add(linepoint);
+                    tempList.Add(linepoint);
                 }
             }
+            //Fill the gaps between points
+            for (int i = 0; i < tempList.Count - 1; i++)
+            {
+                List<Point> partialList = BresenhamLineAlgorithm(tempList[i], tempList[i + 1]);
+                partialList.RemoveAt(partialList.Count - 1);
+                newList.AddRange(partialList);
+            }
+            newList.Add(tempList.Last<Point>());
             linePoints = newList;
         }
+
+        /// <summary>
+        /// An implementation of the Bresenham Line Algorithm, 
+        /// which calculates all points between two points in a straight line.
+        /// </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)
+        {
+            List<Point> returnList = new List<Point>();
+            int deltaX = p1.X - p0.X;
+            int deltaY = p1.Y - p0.Y;
+            if(deltaX != 0 && deltaY != 0)
+            {
+                //line is not horizontal or vertical
+                //Bresenham Implementation taken from Wikipedia
+                float deltaErr = Math.Abs(deltaY / deltaX);
+                float error = 0;
+                int y = p0.Y;
+                if (deltaX > 0)
+                {
+                    for (int x = p0.X; x <= p1.X; x++)
+                    {
+                        returnList.Add(new Point(x, y));
+                        error += deltaErr;
+                        if (error >= 0.5)
+                        {
+                            y = y + Math.Sign(deltaY) * 1;
+                            error -= 1;
+                        }
+                    }
+                }
+                else if(deltaX < 0)
+                {
+                    for (int x = p0.X; x >= p1.X; x--)
+                    {
+                        returnList.Add(new Point(x, y));
+                        error += deltaErr;
+                        if (error >= 0.5)
+                        {
+                            y = y + Math.Sign(deltaY) * 1;
+                            error -= 1;
+                        }
+                    }
+                }
+                return returnList;
+            }
+            else if(deltaX == 0 && deltaY != 0)
+            {
+                //line is vertical
+                if (deltaY < 0)
+                {
+                    //p1 is above of p0
+                    for (int i = p0.Y; i >= p1.Y; i--)
+                    {
+                        returnList.Add(new Point(p0.X, i));
+                    }
+                    return returnList;
+                }
+                else
+                {
+                    //p1 is below of p0
+                    for (int i = p0.Y; i <= p1.Y; i++)
+                    {
+                        returnList.Add(new Point(p0.X, i));
+                    }
+                    return returnList;
+                }
+            }
+            else if(deltaX != 0 && deltaY == 0)
+            {
+                //line is horizontal
+                if(deltaX < 0)
+                {
+                    //p1 is left of p0
+                    for(int i = p0.X; i >= p1.X; i--)
+                    {
+                        returnList.Add(new Point(i, p0.Y));
+                    }
+                    return returnList;
+                }
+                else
+                {
+                    //p1 is right of p0
+                    for (int i = p0.X; i <= p1.X; i++)
+                    {
+                        returnList.Add(new Point(i, p0.Y));
+                    }
+                    return returnList;
+                }
+            }
+            else
+            {
+                //both points are the same
+                returnList.Add(p0);
+                return returnList;
+            }
+        }
     }
 }

+ 0 - 13
userstory6.md

@@ -1,13 +0,0 @@
-# Userstory 6
-
-|**ID**|6|
-|-|-|
-|**Name**|Löschfunktionalität|
-|**Beschreibung**|Man kann einen Knopf in der UI drücken um in den Löschmodus zu kommen, und um wieder aus dem Löschmodus rauszukommen. Wenn man im Löschmodus ist, kann man auf einer Leinwand im rechten Bereich gezeichnete Elemente löschen indem man auf sie klickt.|
-|**Akzeptanzkriterium**|Der Knopf für den Löschmodus kann nur gedrückt werden, wenn im rechten Bereich eine Zeichennleinwand vorhanden ist. Wenn man den Knopf drückt ohne im Löschmodus zu sein, verändert sich der Knopf erkennbar und man kommt in den Löschmodus. Drückt man den Knopf, während man im Löschmodus ist, wird der Löschmodus beendet und der Knopf bekommt wieder sein urspüngliches Aussehen. Während man im Löschmodus ist, kann man auf der Leinwand im rechten Bereich Elemente löschen, wenn der Mauszeiger sie berührt während man die Maustaste gedrückt hält. Die Elemente die man löscht verschwinden. Drückt man den Löschknopf während man in einem anderen Modus ist (z.B. Zeichenmodus) wird der andere Modus beendet und man ist im Löschmodus.|
-|Geschätzter Aufwand (Story Points)|16|
-|Entwickler|Martin|
-|Umgesetzt in Iteration|keine|
-|Tatsächlicher Aufwand (Std.)|keine|
-|Velocity (Std./Story Point)|keine|
-|Bemerkungen|Keine|