Bladeren bron

Added Functions to calulate circles.

- Added functions for filled and empty circles.
- Added function to display first and last point in line
Martin Edlund 6 jaren geleden
bovenliggende
commit
37241850af

+ 7 - 7
SketchAssistant/SketchAssistant.Tests/UnitTest1.cs

@@ -20,7 +20,7 @@ namespace Tests
             //Test point
             List<Point> expectedResult = new List<Point>();
             expectedResult.Add(new Point(1, 2));
-            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(1, 2), new Point(1, 2));
+            List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(1, 2), new Point(1, 2));
             Assert.AreEqual(1, actualResult.Count);
             for (int i = 0; i < actualResult.Count; i++)
             {
@@ -34,7 +34,7 @@ namespace Tests
             //Test line going from left to right
             List<Point> expectedResult = new List<Point>();
             for (int i = 1; i <= 6; i++) { expectedResult.Add(new Point(i, 2)); }
-            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(1, 2), new Point(6, 2));
+            List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(1, 2), new Point(6, 2));
             Assert.AreEqual(expectedResult.Count, actualResult.Count);
             for (int i = 0; i < actualResult.Count; i++)
             {
@@ -48,7 +48,7 @@ namespace Tests
             //Test line going from right to left
             List<Point> expectedResult = new List<Point>();
             for (int i = 6; i >= 1; i--) { expectedResult.Add(new Point(i, 2)); }
-            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(6, 2), new Point(1, 2));
+            List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(6, 2), new Point(1, 2));
             Assert.AreEqual(expectedResult.Count, actualResult.Count);
             for (int i = 0; i < actualResult.Count; i++)
             {
@@ -62,7 +62,7 @@ namespace Tests
             //Test line going from top to bottom
             List<Point> expectedResult = new List<Point>();
             for (int i = 5; i <= 25; i++) { expectedResult.Add(new Point(7, i)); }
-            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 5), new Point(7, 25));
+            List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(7, 5), new Point(7, 25));
             Assert.AreEqual(expectedResult.Count, actualResult.Count);
             for (int i = 0; i < actualResult.Count; i++)
             {
@@ -76,7 +76,7 @@ namespace Tests
             //Test line going from bottom to top
             List<Point> expectedResult = new List<Point>();
             for (int i = 25; i >= 5; i--) { expectedResult.Add(new Point(7, i)); }
-            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 25), new Point(7, 5));
+            List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(7, 25), new Point(7, 5));
             Assert.AreEqual(expectedResult.Count, actualResult.Count);
             for (int i = 0; i < actualResult.Count; i++)
             {
@@ -90,7 +90,7 @@ namespace Tests
             //Test exactly diagonal line from top left to bottom right
             List<Point> expectedResult = new List<Point>();
             for (int i = 5; i <= 25; i++) { expectedResult.Add(new Point(i + 2, i)); }
-            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 5), new Point(27, 25));
+            List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(7, 5), new Point(27, 25));
             Assert.AreEqual(expectedResult.Count, actualResult.Count);
             for (int i = 0; i < actualResult.Count; i++)
             {
@@ -104,7 +104,7 @@ namespace Tests
             //Test exactly diagonal line from bottom right to top left
             List<Point> expectedResult = new List<Point>();
             for (int i = 25; i >= 5; i--) { expectedResult.Add(new Point(i + 2, i)); }
-            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(27, 25), new Point(7, 5));
+            List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(27, 25), new Point(7, 5));
             Assert.AreEqual(expectedResult.Count, actualResult.Count);
             for (int i = 0; i < actualResult.Count; i++)
             {

+ 18 - 2
SketchAssistant/SketchAssistant/Form1.cs

@@ -91,9 +91,12 @@ namespace SketchAssistant
         /// </summary>
         Graphics rightGraph = null;
         /// <summary>
-        /// Deletion Matrixes for checking postions of lines in the image
+        /// Lookup Matrix for checking postions of lines in the image
         /// </summary>
         bool[,] isFilledMatrix;
+        /// <summary>
+        /// Lookup Matrix for getting line ids at a certain postions of the image
+        /// </summary>
         HashSet<int>[,] linesMatrix;
         /// <summary>
         /// Size of deletion area
@@ -614,6 +617,8 @@ namespace SketchAssistant
             {
                 l.DrawLine(Graphics.FromImage(leftImage));
             }
+            //temporary for testing.
+            //DisplayStartAndEnd(leftLineList[0], 30);
         }
 
         /// <summary>
@@ -625,10 +630,21 @@ namespace SketchAssistant
             MessageBox.Show(message);
         }
         
-        private void DisplayPointsInRightCanvas(Point p0, Point p1)
+        /// <summary>
+        /// Will display the start and endpoints of the given line on the right canvas.
+        /// </summary>
+        /// <param name="line">The line.</param>
+        /// <param name="size">The size of the circle with which the endpoints of the line are marked.</param>
+        private void DisplayStartAndEnd(Line line, int size)
         {
+            var circle0 = GeometryCalculator.FilledCircleAlgorithm(line.GetStartPoint(), size);
+            var circle1 = GeometryCalculator.FilledCircleAlgorithm(line.GetEndPoint(), size);
 
+            rightGraph = Graphics.FromImage(rightImage);
+            foreach(Point p in circle0) { rightGraph.FillRectangle(Brushes.Red, p.X, p.Y, 1, 1); }
+            foreach(Point p in circle1) { rightGraph.FillRectangle(Brushes.Blue, p.X, p.Y, 1, 1); }
 
+            SetAndRefreshRightImage(rightImage);
         }
 
         /********************************************/

+ 75 - 0
SketchAssistant/SketchAssistant/GeometryCalculator.cs

@@ -12,6 +12,81 @@ namespace SketchAssistant
     /// </summary>
     public static class GeometryCalculator
     {
+        /// <summary>
+        /// An implementation of the Bresenham Line Algorithm,
+        /// which calculates the points of a circle in a radius around a center point.
+        /// Implemented with the help of code examples on Wikipedia.
+        /// </summary>
+        /// <param name="center">The center of the circle.</param>
+        /// <param name="radius">The radius of the circle, 
+        /// when it is zero or less, only the midpoint is returned</param>
+        /// <returns>The HashSet containing all Points on the circle.</returns>
+        public static HashSet<Point> BresenhamCircleAlgorithm(Point center, int radius)
+        {
+            if(radius <= 0) { return new HashSet<Point> { center }; }
+
+            int x = radius - 1;
+            int y = 0;
+            int dx = 1;
+            int dy = 1;
+            int err = dx - (radius * 2);
+            HashSet<Point> returnSet = new HashSet<Point>();
+            
+            while (x >= y)
+            {
+                returnSet.Add(new Point(center.X + x, center.Y + y));
+                returnSet.Add(new Point(center.X + y, center.Y + x));
+                returnSet.Add(new Point(center.X - y, center.Y + x));
+                returnSet.Add(new Point(center.X - x, center.Y + y));
+                returnSet.Add(new Point(center.X - x, center.Y - y));
+                returnSet.Add(new Point(center.X - y, center.Y - x));
+                returnSet.Add(new Point(center.X + y, center.Y - x));
+                returnSet.Add(new Point(center.X + x, center.Y - y));
+
+                if (err <= 0)
+                {
+                    y++;
+                    err += dy;
+                    dy += 2;
+                }
+
+                if (err > 0)
+                {
+                    x--;
+                    dx += 2;
+                    err += dx - (radius * 2);
+                }
+            }
+            return returnSet;
+        }
+
+        /// <summary>
+        /// A simple algorithm that returns a filled circle with a radius and a center point.
+        /// </summary>
+        /// <param name="center">The center point of the alorithm </param>
+        /// <param name="radius">The radius of the circle, if its less or equal to 1 
+        /// only the center point is returned. </param>
+        /// <returns>All the points in or on the circle.</returns>
+        public static HashSet<Point> FilledCircleAlgorithm(Point center, int radius)
+        {
+            HashSet<Point> returnSet = new HashSet<Point> { center };
+            //Fill the circle
+            for (int x = 0; x < radius; x++)
+            {
+                for (int y = 0; y < radius; y++)
+                {
+                    //Check if point is on or in the circle
+                    if((x*x + y*y - radius * radius) <= 0)
+                    {
+                        returnSet.Add(new Point(center.X + x, center.Y + y));
+                        returnSet.Add(new Point(center.X - x, center.Y + y));
+                        returnSet.Add(new Point(center.X + x, center.Y - y));
+                        returnSet.Add(new Point(center.X - x, center.Y - y));
+                    }
+                }
+            }
+            return returnSet;
+        }
 
         /// <summary>
         /// An implementation of the Bresenham Line Algorithm, 

+ 1 - 2
SketchAssistant/SketchAssistant/Line.cs

@@ -75,10 +75,9 @@ namespace SketchAssistant
         /// <returns>The given Graphics element with the additional line</returns>
         public Graphics DrawLine(Graphics canvas)
         {
-            Pen thePen = new Pen(Color.Black);
             for(int i = 0; i < linePoints.Count - 1 ; i++)
             {
-                canvas.DrawLine(thePen, linePoints[i], linePoints[i + 1]);
+                canvas.DrawLine(Pens.Black, linePoints[i], linePoints[i + 1]);
             }
             //If there is only one point
             if(linePoints.Count == 1){ canvas.FillRectangle(Brushes.Black, linePoints[0].X, linePoints[0].Y, 1, 1); }