WPF矩阵计算示例

WPF矩阵计算示例

界面代码如下:

<Window x:Class="Chapter3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Chapter3"
        mc:Ignorable="d"
        Title="Matrix OPeration" Height="400" Width="500">

    <Viewbox Stretch="Uniform">
        <Grid Width="430" Height="300" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"/>
                <ColumnDefinition Width="280"/>
            Grid.ColumnDefinitions>
            <Grid Width="140" Height="300" Margin="5 10 5 5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="70"/>
                Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                Grid.RowDefinitions>

                
                <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >X1TextBlock>
                <TextBox Name="tbX1" Grid.Row="0" Grid.Column="1" TextAlignment="Center">50TextBox>

                
                <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >Y1TextBlock>
                <TextBox Name="tbY1" Grid.Row="1" Grid.Column="1" TextAlignment="Center">200TextBox>

                
                <TextBlock Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >X2TextBlock>
                <TextBox Name="tbX2" Grid.Row="2" Grid.Column="1" TextAlignment="Center">150TextBox>

                
                <TextBlock Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >Y1TextBlock>
                <TextBox Name="tbY2" Grid.Row="3" Grid.Column="1" TextAlignment="Center">100TextBox>

                
                <TextBlock Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >LengthTextBlock>
                <TextBox Name="tbLine" Grid.Row="4" Grid.Column="1" TextAlignment="Center">100TextBox>

                
                <Button Name="btnApply" Grid.Row="5" Grid.Column="0" Margin="15 20 15 5" Height="25" 
                        Grid.ColumnSpan="2" Click="btnApply_Click">ApplyButton>


                
                <Button Name="btnClose" Grid.Row="6" Grid.Column="0" Margin="15 5 15 5" Height="25" 
                        Grid.ColumnSpan="2" Click="btnClose_Click">CloseButton>

            Grid>
            <Canvas Name="canvas1" Grid.Column="1" Margin="10" ClipToBounds="True" Width="270" Height="280" MouseWheel="canvas1_MouseWheel">
                <TextBlock Name="tbPoint1" Canvas.Top="10">Point1TextBlock>
                <TextBlock Name="tbPoint2" Canvas.Top="25">Point2TextBlock>
                <TextBlock Name="tbPoint3" Canvas.Top="40">Point3TextBlock>
                <TextBlock Name="tbPoint4" Canvas.Top="55">Point4TextBlock>
            Canvas>
        Grid>
    Viewbox>            
Window>

后台代码如下:

#region NameSpaces
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
#endregion

namespace Chapter3
{
	/// 
	/// MainWindow.xaml 的交互逻辑
	/// 
	public partial class MainWindow : Window
	{
		private Line line1;
		private Line line2;
		
	
		

		public MainWindow()
		{
			InitializeComponent();
			Rectangle rect = new Rectangle();
			rect.Stroke = Brushes.Black;
			rect.Width = canvas1.Width;
			rect.Height = canvas1.Height;
			canvas1.Children.Add(rect);
			line1 = new Line();
			line2 = new Line();
			AddLines();
		}

		/// 
		/// Generate line
		/// 
		private void AddLines()
		{
			Point pt1 = new Point();
			Point pt2 = new Point();
			// pt1
			pt1.X = Convert.ToDouble(tbX1.Text);
			pt1.Y = Convert.ToDouble(tbY1.Text);

			// pt2
			pt2.X = Convert.ToDouble(tbX2.Text);
			pt2.Y = Convert.ToDouble(tbY2.Text);

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

			// line1
			line1.X1 = pt1.X;
			line1.Y1 = pt1.Y;
			line1.X2 = pt2.X;
			line1.Y2 = pt2.Y;
			line1.Stroke = Brushes.Gray;
			line1.StrokeThickness = 4;

			// add to canvas1
			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 v1 = pt1 - pt2;
			Matrix m1 = new Matrix();
			Point pt3 = new Point();
			Point pt4 = new Point();

			m1.Rotate(-90);
			v1.Normalize();
			v1 *= length;
			line2 = new Line();
			line2.Stroke = Brushes.Gray;
			line2.StrokeThickness = 4;
			line2.StrokeDashArray = DoubleCollection.Parse("3,1");

			pt3 = pt2 + v1 * m1;
			m1 = new Matrix();
			m1.Rotate(90);
			pt4 = pt2 + v1 * m1;
			line2.X1 = pt3.X;
			line2.Y1 = pt3.Y;
			line2.X2 = pt4.X;
			line2.Y2 = pt4.Y;
			canvas1.Children.Add(line2);
			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() + ")";





		}

		/// 
		/// Apply click method
		/// 
		/// 
		/// 
		private void btnApply_Click(object sender, RoutedEventArgs e)
		{
			if (line1 != null)
			{
				this.canvas1.Children.Remove(line1);
				this.canvas1.Children.Remove(line2);
				AddLines();
			}
			
		}

		/// 
		/// Close click method
		/// 
		/// 
		/// 
		private void btnClose_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		
	}
}


效果如下:
WPF矩阵计算示例_第1张图片

你可能感兴趣的:(WPF学习笔记)