Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.31 KB | None | 0 0
  1. ## Planner.cs
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace KortstePad
  10. {
  11.     public class Planner
  12.     {
  13.         private int width;
  14.         private int height;
  15.  
  16.         public Point start;
  17.         public Point end;
  18.  
  19.         public bool done = false;
  20.  
  21.         public List<Point> points = new List<Point>();
  22.         public List<Line> lines = new List<Line>();
  23.         public List<Rectangle> obstacles = new List<Rectangle>();
  24.  
  25.         public Planner(int width, int height, Point start, Point end, List<Rectangle> obstacles)
  26.         {
  27.             this.width = width;
  28.             this.height = height;
  29.             this.start = start;
  30.             this.end = end;
  31.             this.obstacles = obstacles;
  32.  
  33.             points.Add(start);
  34.         }
  35.  
  36.         public void calculateNextPoint() {
  37.             // Kijken of we een direct pad naar het einde kunnen vinden of niet
  38.             Line direct_path_to_end = findDirectLine(end, points);
  39.  
  40.             if (direct_path_to_end != null)
  41.             {
  42.                 lines.Add(direct_path_to_end);
  43.                 done = true;
  44.             }
  45.             else
  46.             {
  47.                 // Random punt kiezen
  48.                 Random rand = new Random();
  49.                 int x = rand.Next(width);
  50.                 int y = rand.Next(height);
  51.                 Point randomPoint = new Point(x, y);
  52.  
  53.                 // Dichtstbijzijnde punt zoeken
  54.                 Point nearestPoint = findNearestPoint(randomPoint, points);
  55.  
  56.                 // Indien geen obstakel lijntje maken
  57.                 if (!collidesWithObstacles(nearestPoint, randomPoint))
  58.                 {
  59.                     points.Add(randomPoint);
  60.                     lines.Add(new Line(nearestPoint, randomPoint));
  61.                 }
  62.             }
  63.         }
  64.  
  65.         public List<Line> getPath()
  66.         {
  67.             List<Line> path = new List<Line>();
  68.  
  69.             // We zoeken het laatste segment van het pad
  70.             Line line = lines.Find(l => l.b == end);
  71.             path.Add(line);
  72.  
  73.             // Zolang het startpunt van een segment niet gelijk is aan ons startpunt van het pad blijven we het pad terugvolgen
  74.             while (line.a != start)
  75.             {
  76.                 // We zoeken een segment waarvan het eindpunt gelijk is aan het beginpunt van het volgende segment (want we keren terug)
  77.                 line = lines.Find(l => l.b == line.a);
  78.                 path.Add(line);
  79.             }
  80.  
  81.             return path;
  82.         }
  83.  
  84.         private Line findDirectLine(Point p, List<Point> points)
  85.         {
  86.             for (int i = 0; i < points.Count; i++)
  87.             {
  88.                 // Als geen obstakel geven we het pad
  89.                 if (!collidesWithObstacles(p, points[i]))
  90.                 {
  91.                     return new Line(points[i], p);
  92.                 }
  93.             }
  94.  
  95.             return null;
  96.         }
  97.  
  98.         private bool collidesWithObstacles(Point a, Point b)
  99.         {
  100.             foreach (Rectangle rect in obstacles)
  101.             {
  102.                 if (LineIntersectsRect(a, b, rect)) return true;
  103.             }
  104.  
  105.             return false;
  106.         }
  107.  
  108.         private Point findNearestPoint(Point p, List<Point> points)
  109.         {
  110.             Point nearestPoint = points[0];
  111.  
  112.             // Pythagoras afstand
  113.             int tempDistance = (p.X - points[0].X) * (p.X - points[0].X) + (p.Y - points[0].Y) * (p.Y - points[0].Y);
  114.  
  115.             for (int i = 1; i < points.Count; i++)
  116.             {
  117.                 int distance = (p.X - points[i].X) * (p.X - points[i].X) + (p.Y - points[i].Y) * (p.Y - points[i].Y);
  118.                 if (distance < tempDistance)
  119.                 {
  120.                     tempDistance = distance;
  121.                     nearestPoint = points[i];
  122.                 }
  123.             }
  124.  
  125.             return nearestPoint;
  126.         }
  127.  
  128.         // Source: https://stackoverflow.com/questions/5514366/how-to-know-if-a-line-intersects-a-rectangle
  129.         private bool LineIntersectsRect(Point p1, Point p2, Rectangle r)
  130.         {
  131.             return LineIntersectsLine(p1, p2, new Point(r.X, r.Y), new Point(r.X + r.Width, r.Y)) ||
  132.                    LineIntersectsLine(p1, p2, new Point(r.X + r.Width, r.Y), new Point(r.X + r.Width, r.Y + r.Height)) ||
  133.                    LineIntersectsLine(p1, p2, new Point(r.X + r.Width, r.Y + r.Height), new Point(r.X, r.Y + r.Height)) ||
  134.                    LineIntersectsLine(p1, p2, new Point(r.X, r.Y + r.Height), new Point(r.X, r.Y)) ||
  135.                    (r.Contains(p1) && r.Contains(p2));
  136.         }
  137.  
  138.         private bool LineIntersectsLine(Point l1p1, Point l1p2, Point l2p1, Point l2p2)
  139.         {
  140.             float q = (l1p1.Y - l2p1.Y) * (l2p2.X - l2p1.X) - (l1p1.X - l2p1.X) * (l2p2.Y - l2p1.Y);
  141.             float d = (l1p2.X - l1p1.X) * (l2p2.Y - l2p1.Y) - (l1p2.Y - l1p1.Y) * (l2p2.X - l2p1.X);
  142.  
  143.             if (d == 0)
  144.             {
  145.                 return false;
  146.             }
  147.  
  148.             float r = q / d;
  149.  
  150.             q = (l1p1.Y - l2p1.Y) * (l1p2.X - l1p1.X) - (l1p1.X - l2p1.X) * (l1p2.Y - l1p1.Y);
  151.             float s = q / d;
  152.  
  153.             if (r < 0 || r > 1 || s < 0 || s > 1)
  154.             {
  155.                 return false;
  156.             }
  157.  
  158.             return true;
  159.         }
  160.     }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement