Browse Source

Added angle calculation

Martin Edlund 5 years ago
parent
commit
4c6f436742

+ 0 - 17
SketchAssistant/SketchAssistantWPF/Angle.cs

@@ -1,17 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Windows;
-
-namespace SketchAssistantWPF
-{
-    public class Angle
-    {
-        public Angle(Point p0, Point p1)
-        {
-            //TODO: Add angle calculations
-        }
-    }
-}

+ 6 - 2
SketchAssistant/SketchAssistantWPF/MVP_Model.cs

@@ -107,7 +107,7 @@ namespace SketchAssistantWPF
 
         List<Point> currentLine = new List<Point>();
 
-
+        RedrawManager redrawMan;
 
         public MVP_Model(MVP_Presenter presenter)
         {
@@ -231,7 +231,7 @@ namespace SketchAssistantWPF
         {
             leftImageSize = new ImageDimension(width, height);
             rightImageSize = new ImageDimension(width, height);
-            new RedrawManager(listOfLines);
+            redrawMan = new RedrawManager(listOfLines);
             leftLineList = listOfLines;
             graphicLoaded = true;
             programPresenter.UpdateLeftLines(leftLineList);
@@ -400,6 +400,10 @@ namespace SketchAssistantWPF
             {
                 currentLine.Add(currentCursorPosition);
                 //programPresenter.UpdateCurrentLine(currentLine);
+                if (redrawMan != null)
+                {
+                    Console.WriteLine("ANGLE {0}", redrawMan.GetDirection(currentCursorPosition));
+                }
             }
             //Deleting
             if (!inDrawingMode && programPresenter.IsMousePressed())

+ 25 - 4
SketchAssistant/SketchAssistantWPF/RedrawLine.cs

@@ -18,6 +18,8 @@ namespace SketchAssistantWPF
 
         private HashSet<Point>[] detectionZones;
 
+        private int finishedIndex;
+
         /// <summary>
         /// Constructor of the RedrawLine.
         /// </summary>
@@ -50,7 +52,7 @@ namespace SketchAssistantWPF
                 dZones.Add(newZone);
             }
             detectionZones = dZones.ToArray();
-
+            finishedIndex = 0;
             Console.WriteLine("This line has {0} points ", line.GetPoints().Count());
             return false;
         }
@@ -60,12 +62,31 @@ namespace SketchAssistantWPF
             return points[0];
         }
 
-        public Angle GetDirection(Point p)
+        public double GetDirection(Point p)
         {
+            if(finishedIndex > points.Length - 1)
+            {
+                return -1;
+            }
+
+            if(detectionZones[finishedIndex + 1].Contains(p))
+            {
+                finishedIndex++;
+            }
+
+            double angle = 0;
+            var np = points[finishedIndex + 1];
+            Vector vector0 = new Vector(1,0);
+            Vector vector1 = new Vector(np.X-p.X, np.Y - p.Y);
+            angle = Math.Acos((vector0.X* vector1.X + vector0.Y * vector1.Y) / (vector0.Length * vector1.Length)) / Math.PI * 180;
+            /*double cross_prod = np.Y - p.Y;
+            double acute_angle = Math.Atan2(Math.Abs(cross_prod), np.X - p.Y)* 180 /Math.PI;
+            if (cross_prod < 0) { angle = 360 - acute_angle; }
+            else { angle = acute_angle; }
+            */
             //TODO: Calculate angles between p and the next n points of the line
             // Take average and return it.
-            return null;
+            return angle;
         }
-
     }
 }

+ 13 - 3
SketchAssistant/SketchAssistantWPF/RedrawManager.cs

@@ -16,9 +16,11 @@ namespace SketchAssistantWPF
 
         public int currentLine { get; private set; }
 
+        public bool finished { get; private set; }
+
         public RedrawManager(List<InternalLine> linesToRedraw)
         {
-            radius = 5;
+            radius = 20;
             redrawLines = new RedrawLine[linesToRedraw.Count];
             Task[] taskPool = new Task[linesToRedraw.Count];
             Console.WriteLine("STARTED THREAD CREATION");
@@ -39,6 +41,7 @@ namespace SketchAssistantWPF
             Task.WaitAll(taskPool);
             Console.WriteLine("FINISHED ALL THREADS");
 
+            finished = false;
             currentLine = 0;
         }
 
@@ -54,9 +57,16 @@ namespace SketchAssistantWPF
             }
         }
 
-        public Angle GetDirection(Point p)
+        public double GetDirection(Point p)
         {
-            return redrawLines[currentLine].GetDirection(p);
+            if (finished) return -1;
+            double dir = redrawLines[currentLine].GetDirection(p);
+            if(dir < 0)
+            {
+                currentLine++;
+                finished = true;
+            }
+            return dir;
         }
     }
 }

+ 0 - 1
SketchAssistant/SketchAssistantWPF/SketchAssistantWPF.csproj

@@ -75,7 +75,6 @@
       <SubType>Designer</SubType>
     </ApplicationDefinition>
     <Compile Include="ActionHistory.cs" />
-    <Compile Include="Angle.cs" />
     <Compile Include="CustomCanvas.cs" />
     <Compile Include="DebugData.cs" />
     <Compile Include="FileImporter.cs" />