Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using System.Windows.Shapes;
  7. using System.Windows.Media.Imaging;
  8.  
  9. namespace SilverlightApplication1
  10. {
  11.     public partial class MainPage : UserControl
  12.     {
  13.         private bool PolygonStarted = false;
  14.         private Line Line1, Line2;
  15.         private int LineSwich;
  16.         private Point pt, pt2, first;
  17.         private Point[] Points = new Point[1000];
  18.         int PointCounter = 0;
  19.         public MainPage()
  20.         {
  21.             InitializeComponent();
  22.             Loaded += new RoutedEventHandler(Page_Loaded);
  23.         }
  24.  
  25.         void Page_Loaded(object sender, RoutedEventArgs e)
  26.         {
  27.             LayoutRoot.MouseMove += new MouseEventHandler(LayoutRoot_MouseMove);
  28.             LayoutRoot.MouseLeftButtonDown += new MouseButtonEventHandler(LayoutRoot_MouseLeftButtonDown);
  29.         }
  30.  
  31.  
  32.         void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  33.         {
  34.             pt = e.GetPosition(LayoutRoot);
  35.             PolygonStarted = !PolygonFinished(first, pt);
  36.  
  37.             if (!PolygonStarted)
  38.             {
  39.                 if (LineSwich == 2)
  40.                 {
  41.                     LayoutRoot.Children.Remove(Line2);
  42.                     Line2.X2 = first.X;
  43.                     Line2.Y2 = first.Y;
  44.                     LayoutRoot.Children.Add(Line2);
  45.                 }
  46.                 else
  47.                 {
  48.                     LayoutRoot.Children.Remove(Line1);
  49.                     Line1.X2 = first.X;
  50.                     Line1.Y2 = first.Y;
  51.                     LayoutRoot.Children.Add(Line1);
  52.                 }
  53.                 first.X = 0;
  54.                 first.Y = 0;
  55.             }
  56.             if ((first.X == 0 & first.Y == 0) | PointIsValid(pt))
  57.             {
  58.                 SavePoint(pt);
  59.                 Line1 = new Line();
  60.                 Line2 = new Line();
  61.                 LineSwich = 1;
  62.             }
  63.             else
  64.                 pt = Points[PointCounter - 1];
  65.  
  66.         }
  67.  
  68.         bool PointIsValid(Point p)
  69.         {
  70.             if (first.X == 0 & first.Y == 0)
  71.             {
  72.                 return(true);
  73.             }
  74.  
  75.  
  76.             for (int i = 0; i < PointCounter + 1; i++)
  77.             {
  78.                 if (((Math.Sqrt((p.X - Points[i].X) * (p.X - Points[i].X) + (p.Y - Points[i].Y) * (p.Y - Points[i].Y)) < 10) &
  79.                     Points[i] != first))
  80.                 {
  81.                     return (false);
  82.                 }
  83.             }
  84.             return (true);
  85.         }
  86.  
  87.         void SavePoint(Point p)
  88.         {
  89.             Points[PointCounter] = p;
  90.             PointCounter++;
  91.         }
  92.  
  93.         bool PolygonFinished(Point pt1, Point pt2)
  94.         {
  95.             if ((pt1.X == 0) & (pt1.Y == 0))
  96.                 return(false);
  97.  
  98.             double d = Math.Sqrt((pt1.X-pt2.X) * (pt1.X-pt2.X) + (pt1.Y-pt2.Y) * (pt1.Y-pt2.Y));
  99.             if (d < 6)
  100.                 return (true);
  101.             else
  102.                 return (false);
  103.         }
  104.  
  105.         void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
  106.         {
  107.             if (PolygonStarted)
  108.             {
  109.                 if ((first.X == 0) & (first.Y == 0))
  110.                     first = pt;
  111.                 pt2 = e.GetPosition(LayoutRoot);
  112.                 if (LineSwich == 1)
  113.                 {
  114.                     LineSwich = 2;
  115.                     LayoutRoot.Children.Remove(Line1);
  116.                     Line2.X1 = pt.X;
  117.                     Line2.Y1 = pt.Y;
  118.                     Line2.X2 = pt2.X;
  119.                     Line2.Y2 = pt2.Y;
  120.                     Line2.Stroke = new SolidColorBrush(Colors.Black);
  121.                     Line2.StrokeStartLineCap = PenLineCap.Round;
  122.                     Line2.StrokeEndLineCap = PenLineCap.Round;
  123.                     Line2.StrokeThickness = 1;
  124.                     LayoutRoot.Children.Add(Line2);
  125.                 }
  126.                 else
  127.                 {
  128.                     LineSwich = 1;
  129.                     LayoutRoot.Children.Remove(Line2);
  130.                     Line1.X1 = pt.X;
  131.                     Line1.Y1 = pt.Y;
  132.                     Line1.X2 = pt2.X;
  133.                     Line1.Y2 = pt2.Y;
  134.                     Line1.Stroke = new SolidColorBrush(Colors.Black);
  135.                     Line1.StrokeStartLineCap = PenLineCap.Round;
  136.                     Line1.StrokeEndLineCap = PenLineCap.Round;
  137.                     Line1.StrokeThickness = 1;
  138.                     LayoutRoot.Children.Add(Line1);
  139.                 }
  140.             }
  141.         }
  142.  
  143.         private void UserControl_Loaded(object sender, RoutedEventArgs e)
  144.         {
  145.  
  146.         }
  147.  
  148.         public class SearchEngine
  149.         {
  150.             public bool SearchIsDone = false;
  151.             public Point GlobalTrailPoint = new Point(0, 0);
  152.             WriteableBitmap wb;
  153.             int width, height;
  154.  
  155.             public void CreateBitmap(Canvas Can)
  156.             {
  157.                 wb = new WriteableBitmap(Can, null);
  158.                 width = wb.PixelWidth;
  159.                 height = wb.PixelHeight;
  160.             }
  161.  
  162.             public void IncGlobalTrailPoint()
  163.             {
  164.                 if (GlobalTrailPoint.X < width - 1)
  165.                     GlobalTrailPoint.X += 1;
  166.                 else
  167.                 {
  168.                     if (GlobalTrailPoint.Y < height - 1)
  169.                     {
  170.                         GlobalTrailPoint.Y += 1;
  171.                         GlobalTrailPoint.X = 1;
  172.                     }
  173.                     else
  174.                         SearchIsDone = true;
  175.                 }
  176.             }
  177.  
  178.             public bool PointIsWhite(Point p)
  179.             {
  180.                 if (wb.Pixels[(int)GlobalTrailPoint.Y * wb.PixelWidth + (int)GlobalTrailPoint.X] != -1)
  181.                     return (false);
  182.                 else
  183.                     return (true);
  184.             }
  185.  
  186.         }
  187.  
  188.         public void LookupVertex()
  189.         {
  190.             SearchEngine SE = new SearchEngine();
  191.             SE.CreateBitmap(LayoutRoot);
  192.             while (SE.PointIsWhite(SE.GlobalTrailPoint) & !SE.SearchIsDone)
  193.             {
  194.                 SE.IncGlobalTrailPoint();
  195.             }
  196.            
  197.  
  198.  
  199.             if (!SE.SearchIsDone)
  200.             {
  201.                 MessageBox.Show("X" + SE.GlobalTrailPoint.X.ToString() + " Y" + SE.GlobalTrailPoint.Y.ToString());
  202.             }
  203.  
  204.  
  205.         }
  206.  
  207.         private void button1_Click(object sender, RoutedEventArgs e)
  208.         {
  209.             LookupVertex();
  210.         }
  211.  
  212.  
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement