Vector Angles : Vector « Graphics « Silverlight






Vector Angles

Vector Angles
    

<UserControl x:Class='SilverlightApplication3.MainPage'
    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' 
    mc:Ignorable='d' 
    d:DesignWidth='640' d:DesignHeight='480'>

  <Canvas x:Name="LayoutRoot" Background="White" Width="800" Height="600">
    <TextBlock Text="TextBlock" TextWrapping="Wrap" Width="228.695" Height="85.537" Canvas.Top="506.463" Canvas.Left="560.305" x:Name="msgBlock"/>
    <Path Height="1" Width="91.109" Canvas.Left="365" Canvas.Top="527" Data="M160,167 L384.10934,167" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" x:Name="testLine">
      <Path.RenderTransform>
        <TransformGroup>
          <ScaleTransform/>
          <SkewTransform/>
          <RotateTransform x:Name="testLineAngle" Angle="0"/>
          <TranslateTransform/>
        </TransformGroup>
      </Path.RenderTransform>
    </Path>
    <Slider Width="151" Canvas.Top="32" Canvas.Left="638" Maximum="100" x:Name="p1XSlider" SmallChange="1"/>
    <TextBlock Text="Point 1 X:" TextWrapping="Wrap" Canvas.Top="8" Canvas.Left="638" x:Name="msgP1XSlider"/>
    <Slider Width="151" Canvas.Top="92" Canvas.Left="638" Maximum="100" x:Name="p1YSlider" SmallChange="1"/>
    <TextBlock Text="Point 1 Y:" TextWrapping="Wrap" Canvas.Top="68" Canvas.Left="638" x:Name="msgP1YSlider"/>
    <Slider Width="151" Canvas.Top="147" Canvas.Left="638" Maximum="100" x:Name="p2XSlider" SmallChange="1"/>
    <TextBlock Text="Point 2 X:" TextWrapping="Wrap" Canvas.Top="123" Canvas.Left="638" x:Name="msgP2XSlider"/>
    <Slider Width="151" Canvas.Top="200" Canvas.Left="638" Maximum="100" x:Name="p2YSlider" SmallChange="1"/>
    <TextBlock Text="Point 2 Y:" TextWrapping="Wrap" Canvas.Top="176" Canvas.Left="638" x:Name="msgP2YSlider"/>
    <Border Height="600" Width="800" BorderThickness="1,1,1,1" BorderBrush="#FF000000"/>
  </Canvas>
</UserControl>

//File: Page.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {

        Line myLine = new Line();
        Point p1 = new Point();
        Point p2 = new Point();
        Point vectorLength = new Point();
        double angle;

        public MainPage()
        {
            InitializeComponent();

            p1.X = 0;
            p1.Y = 0;
            p2.X = 250;
            p2.Y = 250;

            myLine.X1 = p1.X;
            myLine.Y1 = p1.Y;
            myLine.X2 = p2.X;
            myLine.Y2 = p2.Y;
            myLine.StrokeThickness = 1;
            SolidColorBrush stroke = new SolidColorBrush();
            stroke.Color = Colors.Black;
            myLine.Stroke = stroke;

            doAngle();

            LayoutRoot.Children.Add(myLine);

            p1XSlider.Maximum = p2XSlider.Maximum = LayoutRoot.Width;
            p1YSlider.Maximum = p2YSlider.Maximum = LayoutRoot.Height;
            p1XSlider.Value = myLine.X1;
            p1YSlider.Value = myLine.Y1;
            p2XSlider.Value = myLine.X2;
            p2YSlider.Value = myLine.Y2;
            msgP1XSlider.Text = "Point 1 X: " + (Int16)myLine.X1;
            msgP1YSlider.Text = "Point 1 Y: " + (Int16)myLine.Y1;
            msgP2XSlider.Text = "Point 2 X: " + (Int16)myLine.X2;
            msgP2YSlider.Text = "Point 2 Y: " + (Int16)myLine.Y2;
            p1XSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(p1XSlider_ValueChanged);
            p1YSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(p1YSlider_ValueChanged);
            p2XSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(p2XSlider_ValueChanged);
            p2YSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(p2YSlider_ValueChanged);
        }

        private void p2YSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            myLine.Y2 = p2YSlider.Value;
            msgP2YSlider.Text = "Point 2 Y: " + Convert.ToInt16(p2YSlider.Value);
            doAngle();
        }

        private void p2XSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            myLine.X2 = p2XSlider.Value;
            msgP2XSlider.Text = "Point 2 X: " + Convert.ToInt16(p2XSlider.Value);
            doAngle();
        }

        private void p1YSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            myLine.Y1 = p1YSlider.Value;
            msgP1YSlider.Text = "Point 1 Y: " + Convert.ToInt16(p1YSlider.Value);
            doAngle();
        }

        private void p1XSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            myLine.X1 = p1XSlider.Value;
            msgP1XSlider.Text = "Point 1 X: " + Convert.ToInt16(p1XSlider.Value);
            doAngle();
        }

        private void doAngle()
        {
            vectorLength.X = myLine.X2 - myLine.X1;
            vectorLength.Y = myLine.Y2 - myLine.Y1;
            double radians = Math.Atan2(vectorLength.Y, vectorLength.X);
            angle = Convert.ToInt16(180 / Math.PI * radians);
            testLineAngle.Angle = angle;
            msgBlock.Text = "Vector X,Y: " + Convert.ToInt16(vectorLength.X) + ", " + Convert.ToInt16(vectorLength.Y) + "\nAngle: " + angle;
        }
    }
}

   
    
    
    
  








Related examples in the same category

1.Vector Angle ConversionVector Angle Conversion
2.Vector MathVector Math
3.One Dimensional VectorOne Dimensional Vector