Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Shapes;
- using System.Windows.Media.Imaging;
- namespace SilverlightApplication1
- {
- public partial class MainPage : UserControl
- {
- private bool PolygonStarted = false;
- private Line Line1, Line2;
- private int LineSwich;
- private Point pt, pt2, first;
- private Point[] Points = new Point[1000];
- int PointCounter = 0;
- public MainPage()
- {
- InitializeComponent();
- Loaded += new RoutedEventHandler(Page_Loaded);
- }
- void Page_Loaded(object sender, RoutedEventArgs e)
- {
- LayoutRoot.MouseMove += new MouseEventHandler(LayoutRoot_MouseMove);
- LayoutRoot.MouseLeftButtonDown += new MouseButtonEventHandler(LayoutRoot_MouseLeftButtonDown);
- }
- void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- pt = e.GetPosition(LayoutRoot);
- PolygonStarted = !PolygonFinished(first, pt);
- if (!PolygonStarted)
- {
- if (LineSwich == 2)
- {
- LayoutRoot.Children.Remove(Line2);
- Line2.X2 = first.X;
- Line2.Y2 = first.Y;
- LayoutRoot.Children.Add(Line2);
- }
- else
- {
- LayoutRoot.Children.Remove(Line1);
- Line1.X2 = first.X;
- Line1.Y2 = first.Y;
- LayoutRoot.Children.Add(Line1);
- }
- first.X = 0;
- first.Y = 0;
- }
- if ((first.X == 0 & first.Y == 0) | PointIsValid(pt))
- {
- SavePoint(pt);
- Line1 = new Line();
- Line2 = new Line();
- LineSwich = 1;
- }
- else
- pt = Points[PointCounter - 1];
- }
- bool PointIsValid(Point p)
- {
- if (first.X == 0 & first.Y == 0)
- {
- return(true);
- }
- for (int i = 0; i < PointCounter + 1; i++)
- {
- if (((Math.Sqrt((p.X - Points[i].X) * (p.X - Points[i].X) + (p.Y - Points[i].Y) * (p.Y - Points[i].Y)) < 10) &
- Points[i] != first))
- {
- return (false);
- }
- }
- return (true);
- }
- void SavePoint(Point p)
- {
- Points[PointCounter] = p;
- PointCounter++;
- }
- bool PolygonFinished(Point pt1, Point pt2)
- {
- if ((pt1.X == 0) & (pt1.Y == 0))
- return(false);
- double d = Math.Sqrt((pt1.X-pt2.X) * (pt1.X-pt2.X) + (pt1.Y-pt2.Y) * (pt1.Y-pt2.Y));
- if (d < 6)
- return (true);
- else
- return (false);
- }
- void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
- {
- if (PolygonStarted)
- {
- if ((first.X == 0) & (first.Y == 0))
- first = pt;
- pt2 = e.GetPosition(LayoutRoot);
- if (LineSwich == 1)
- {
- LineSwich = 2;
- LayoutRoot.Children.Remove(Line1);
- Line2.X1 = pt.X;
- Line2.Y1 = pt.Y;
- Line2.X2 = pt2.X;
- Line2.Y2 = pt2.Y;
- Line2.Stroke = new SolidColorBrush(Colors.Black);
- Line2.StrokeStartLineCap = PenLineCap.Round;
- Line2.StrokeEndLineCap = PenLineCap.Round;
- Line2.StrokeThickness = 1;
- LayoutRoot.Children.Add(Line2);
- }
- else
- {
- LineSwich = 1;
- LayoutRoot.Children.Remove(Line2);
- Line1.X1 = pt.X;
- Line1.Y1 = pt.Y;
- Line1.X2 = pt2.X;
- Line1.Y2 = pt2.Y;
- Line1.Stroke = new SolidColorBrush(Colors.Black);
- Line1.StrokeStartLineCap = PenLineCap.Round;
- Line1.StrokeEndLineCap = PenLineCap.Round;
- Line1.StrokeThickness = 1;
- LayoutRoot.Children.Add(Line1);
- }
- }
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- }
- public class SearchEngine
- {
- public bool SearchIsDone = false;
- public Point GlobalTrailPoint = new Point(0, 0);
- WriteableBitmap wb;
- int width, height;
- public void CreateBitmap(Canvas Can)
- {
- wb = new WriteableBitmap(Can, null);
- width = wb.PixelWidth;
- height = wb.PixelHeight;
- }
- public void IncGlobalTrailPoint()
- {
- if (GlobalTrailPoint.X < width - 1)
- GlobalTrailPoint.X += 1;
- else
- {
- if (GlobalTrailPoint.Y < height - 1)
- {
- GlobalTrailPoint.Y += 1;
- GlobalTrailPoint.X = 1;
- }
- else
- SearchIsDone = true;
- }
- }
- public bool PointIsWhite(Point p)
- {
- if (wb.Pixels[(int)GlobalTrailPoint.Y * wb.PixelWidth + (int)GlobalTrailPoint.X] != -1)
- return (false);
- else
- return (true);
- }
- }
- public void LookupVertex()
- {
- SearchEngine SE = new SearchEngine();
- SE.CreateBitmap(LayoutRoot);
- while (SE.PointIsWhite(SE.GlobalTrailPoint) & !SE.SearchIsDone)
- {
- SE.IncGlobalTrailPoint();
- }
- if (!SE.SearchIsDone)
- {
- MessageBox.Show("X" + SE.GlobalTrailPoint.X.ToString() + " Y" + SE.GlobalTrailPoint.Y.ToString());
- }
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- LookupVertex();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement