Practical WPF Charts and Graphics notes

Practical WPF Charts and Graphics Note_00

About the section Creating Perpendicular Lines code understanding comments

  1. Note that the length of the line segment is half of the interface setting value:

double length = 0.5 * Convert.ToDouble(tbLength.Text);

  1. About the vector representation in the text:

Vector v1 = pt1-pt2;
should be:
Vector v21 = pt1-pt2;
For example:
Vector v12 = new Point(x2, y2) – new Point(x1, y1);

After v21 undergoes v1.Normalize(); normalization:
Insert picture description here
v21 undergoes v1 *= length; After length transformation:
Insert picture description here

pt3 = pt2 + new vector;

as we know :
pt1 = v21 + pt2
new vector = v21 * m1
the new point here pt3 of new vector comes from old point pt1, so:
pt3 = pt2 + v21 * m1;

This is the code in the figure below:

pt3 = pt2 + v1 * m1;

Insert picture description here
3. The calculation of pt4 is similar, except that the vector rotation direction is opposite.

Insert picture description here
4. The source code style is a bit messy and outdated. The code is sorted by the author and added two 45-degree straight lines as follows:

namespace PerpendicularLine
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Line line1;
        private Line line2;
        private Line line3;
        private Line line4;

        public MainWindow()
        {
            InitializeComponent();

            Rectangle rect = new Rectangle
            {
                Stroke = Brushes.Black,
                Width = canvas1.Width,
                Height = canvas1.Height
            };
            canvas1.Children.Add(rect);

            AddLines();
        }

        private void AddLines()
        {
            Point pt1 = new Point();
            Point pt2 = new Point();

            double length = 0.5 * Convert.ToDouble(tbLength.Text);

            line1 = new Line
            {
                Stroke = Brushes.Pink,
                StrokeThickness = 4
            };

            pt1.X = Convert.ToDouble(tbX1.Text);
            pt1.Y = Convert.ToDouble(tbY1.Text);
            pt2.X = Convert.ToDouble(tbX2.Text);
            pt2.Y = Convert.ToDouble(tbY2.Text);

            line1.X1 = pt1.X;
            line1.Y1 = pt1.Y;
            line1.X2 = pt2.X;
            line1.Y2 = pt2.Y;

            canvas1.Children.Add(line1);

            Canvas.SetLeft(tbPoint1, pt1.X);
            Canvas.SetTop(tbPoint1, pt1.Y);
            Canvas.SetLeft(tbPoint2, pt2.X);
            Canvas.SetTop(tbPoint2, pt2.Y);
            tbPoint1.Text = "Pt1(" + pt1.ToString() + ")";
            tbPoint2.Text = "Pt2(" + pt2.ToString() + ")";

            Vector v21 = pt1 - pt2;
            v21.Normalize();
            v21 *= length;


            Matrix TempMatrix = new Matrix();
            TempMatrix.Rotate(-90);
            Point pt3 = pt2 + v21 * TempMatrix;

            TempMatrix = new Matrix();
            TempMatrix.Rotate(90);
            Point pt4 = pt2 + v21 * TempMatrix;

            TempMatrix = new Matrix();
            TempMatrix.Rotate(-45);
            Point pt23 = pt2 + v21 * TempMatrix;

            TempMatrix = new Matrix();
            TempMatrix.Rotate(45);
            Point pt24 = pt2 + v21 * TempMatrix;

            line3 = new Line
            {
                Stroke = Brushes.Green,
                X1 = pt2.X,
                Y1 = pt2.Y,
                X2 = pt23.X,
                Y2 = pt23.Y
            };

            line4 = new Line
            {
                Stroke = Brushes.Green,
                X1 = pt2.X,
                Y1 = pt2.Y,
                X2 = pt24.X,
                Y2 = pt24.Y
            };

            line2 = new Line
            {
                Stroke = Brushes.Green,
                StrokeThickness = 4,
                StrokeDashArray = DoubleCollection.Parse("3, 1"),

                X1 = pt3.X,
                Y1 = pt3.Y,
                X2 = pt4.X,
                Y2 = pt4.Y
            };

            canvas1.Children.Add(line2);
            canvas1.Children.Add(line3);
            canvas1.Children.Add(line4);

            Canvas.SetLeft(tbPoint3, pt3.X);
            Canvas.SetTop(tbPoint3, pt3.Y);
            Canvas.SetLeft(tbPoint4, pt4.X);
            Canvas.SetTop(tbPoint4, pt4.Y);
            pt3.X = Math.Round(pt3.X, 0);
            pt3.Y = Math.Round(pt3.Y, 0);
            pt4.X = Math.Round(pt4.X, 0);
            pt4.Y = Math.Round(pt4.Y, 0);
            tbPoint3.Text = "Pt3(" + pt3.ToString() + ")";
            tbPoint4.Text = "Pt4(" + pt4.ToString() + ")";
        }

        public void BtnApply_Click(object sender, EventArgs e)
        {
            if (line1 != null)
                canvas1.Children.Remove(line1);
            if (line2 != null)
                canvas1.Children.Remove(line2);
            if (line3 != null)
                canvas1.Children.Remove(line3);
            if (line4 != null)
                canvas1.Children.Remove(line4);
            AddLines();
        }
        public void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}

The results are as follows:

Insert picture description here

Guess you like

Origin blog.csdn.net/lm393485/article/details/108888574