Share:

Find Rectangle Path WPF | Saatody | Amit Padhiyar

using Core.Converter;
using Core.UI;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Shape
{
    public class PRectangle
    {
        public void CreateNewOne(Grid Parent)
        {
            Rectangle Rectangle = new Rectangle();
            Rectangle.HorizontalAlignment = HorizontalAlignment.Stretch;
            Rectangle.VerticalAlignment = VerticalAlignment.Stretch;
            Rectangle.Margin = new Thickness(10, 10, 10, 10);
            Rectangle.Stroke = Brushes.Orange;
            Rectangle.StrokeThickness = 1;
            Parent.Children.Add(Rectangle);
            MyRectangle = Rectangle;

            Rectangle.Loaded += (sender, e) =>
            {
                UpdatePoints();
            };
            CompositionTarget.Rendering += (sender, e) =>
            {
                UpdatePoints();
                //if (UIEvent.IsBusy == true)
                //{
                //    UpdatePoints();
                //    IsFirstTime = true;
                //}
                //else if (UIEvent.IsBusy == false && IsFirstTime == true)
                //{
                //    UpdatePoints();
                //    IsFirstTime = false;
                //}
            };
        }
        private bool IsFirstTime = true;

        private Rectangle MyRectangle { set; get; }
        private List<Ellipse> MyPoints = new List<Ellipse>();
        private int points = 0;
        public int Points
        {
            set
            {
                points = value;
                CreatePoints();
                UpdatePoints();
            }
            get
            {
                return (points);
            }
        }
        private void CreatePoints()
        {
            for (int i = 0; i < MyPoints.Count; i++)
            {
                ((Grid)MyRectangle.Parent).Children.Remove(MyPoints[i]);
            }
            MyPoints.Clear();

            for (int i = 0; i < points; i++)
            {
                Ellipse MyPoint = new Ellipse();
                MyPoint.HorizontalAlignment = HorizontalAlignment.Left;
                MyPoint.VerticalAlignment = VerticalAlignment.Top;
                MyPoint.Margin = new Thickness(0, 0, 0, 0);
                MyPoint.Width = 10;
                MyPoint.Height = 10;
                MyPoint.Fill = new SolidColorBrush(Color.FromRgb(255, 165, 0));
                MyPoint.Stroke = new SolidColorBrush(Color.FromRgb(255, 165, 0));
                MyPoint.StrokeThickness = 1;
                Canvas.SetZIndex(MyPoint, 2000);
                MyPoints.Add(MyPoint);

                ((Grid)MyRectangle.Parent).Children.Add(MyPoints[MyPoints.Count - 1]);
            }
        }
        private void UpdatePoints()
        {
            double X1 = MyRectangle.Margin.Left;
            double Y1 = MyRectangle.Margin.Top;
            double X2 = X1 + MyRectangle.ActualWidth;
            double Y2 = Y1 + MyRectangle.ActualHeight;

            double Length = (((X2 - X1) + (Y2 - Y1)) * (2));
            double Distance = (Length / (points - 0));

            Directions Direction = Directions.MoveToX2Y1;
            double Remain = 0.0;
            double X = 0.0;
            double Y = 0.0;

            //System.Console.WriteLine("++++++++++++++++++++++++++++++++++++++");
            //System.Console.WriteLine("X1: " + X1);
            //System.Console.WriteLine("Y1: " + Y1);
            //System.Console.WriteLine("X2: " + X2);
            //System.Console.WriteLine("Y2: " + Y2);
            //System.Console.WriteLine("Length: " + Length);
            //System.Console.WriteLine("Distance: " + Distance);
            //System.Console.WriteLine("===============================");
            //System.Console.WriteLine("Left :" + (Point.Margin.Left));
            //System.Console.WriteLine("Top :" + (Point.Margin.Top));
            //System.Console.WriteLine("X :" + (X));

            for (int i = 0; i < MyPoints.Count; i++)
            {
                MyPoints[i].Visibility = Visibility.Hidden;
            }
            // Border
            for (int i = 0; i < MyPoints.Count; i++)
            {
                if (Direction == Directions.MoveToX2Y1)
                {
                    if (Remain != 0)
                    {
                        X = (Distance - Remain);
                        Remain = 0.0;
                        System.Console.WriteLine("RedX: " + X);
                    }

                    Ellipse Point = MyPoints[i];
                    double Con1 = 0;
                    double Con2 = (X2 - X1);

                    if (X >= Con1 && X <= Con2)
                    {
                        System.Console.WriteLine("RedI: " + i);
                        Point.Fill = Brushes.Red;
                        Point.Margin = new Thickness(X, 0, 0, 0);
                        Point.Visibility = Visibility.Visible;
                        X += Distance;
                    }
                    else
                    {
                        Direction = Directions.MoveToX2Y2;
                        Remain = ((X2 - X1) - (X - Distance));
                    }
                }
                if (Direction == Directions.MoveToX2Y2)
                {
                    if (Remain != 0)
                    {
                        Y = (Distance - Remain);
                        Remain = 0.0;
                        System.Console.WriteLine("GreenY: " + Y);
                    }

                    Ellipse Point = MyPoints[i];
                    double Con1 = 0;
                    double Con2 = (Y2 - Y1);

                    if (Y >= Con1 && Y <= Con2)
                    {
                        System.Console.WriteLine("GreenI: " + i);
                        Point.Fill = Brushes.Green;
                        Point.Margin = new Thickness((X2 - X1), Y, 0, 0);
                        Point.Visibility = Visibility.Visible;
                        Y += Distance;
                    }
                    else
                    {
                        Direction = Directions.MoveToX1Y2;
                        Remain = ((Y2 - Y1) - (Y - Distance));
                    }
                }
                if (Direction == Directions.MoveToX1Y2)
                {
                    if (Remain != 0)
                    {
                        X = (X2 - X1) - (Distance - Remain);
                        Remain = 0.0;
                        System.Console.WriteLine("BlueX: " + X);
                    }

                    Ellipse Point = MyPoints[i];
                    double Con1 = 0;
                    double Con2 = (X2 - X1);

                    if (X >= Con1 && X <= Con2)
                    {
                        System.Console.WriteLine("BlueI: " + i);
                        Point.Fill = Brushes.Blue;
                        Point.Margin = new Thickness(X, (Y2 - Y1), 0, 0);
                        Point.Visibility = Visibility.Visible;
                        X -= Distance;
                    }
                    else
                    {
                        Direction = Directions.MoveToX1Y1;
                        Remain = (X + Distance);
                    }
                }
                if (Direction == Directions.MoveToX1Y1)
                {
                    if (Remain != 0)
                    {
                        Y = (Y2 - Y1) - (Distance - Remain);
                        Remain = 0.0;
                        System.Console.WriteLine("WhiteY: " + Y);
                    }

                    Ellipse Point = MyPoints[i];
                    double Con1 = 0;
                    double Con2 = (Y2 - Y1);

                    if (Y >= Con1 && Y <= Con2)
                    {
                        System.Console.WriteLine("WhiteI: " + i);
                        Point.Fill = Brushes.White;
                        Point.Margin = new Thickness(0, Y, 0, 0);
                        Point.Visibility = Visibility.Visible;
                        Y -= Distance;
                    }
                    else
                    {
                        Direction = Directions.MoveToX2Y1;
                        Remain = (Y + Distance);
                    }
                }
            }
        }
        private enum Directions
        {
            MoveToX1Y1,
            MoveToX2Y1,
            MoveToX1Y2,
            MoveToX2Y2,
        }
    }
}

Comments

Popular posts from this blog

Get Color From Pixel C# WPF | Saatody | Amit Padhiyar

Basic Audio Operations With MP3 And Wave Files Using NAudio C#

Create Drag And Drop Operation Between DevExpress GridControl And Custom WPF UI | Saatody | Amit Padhiyar