Thursday, January 5, 2012

Mouse and Arc-Tangent.


Add an Ellipse with center at (142,142) and 3 TextBoxes.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightMouse
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void textBox1_MouseMove(object sender, MouseEventArgs e)
        {
            textBox1.Text = "x:y = " + e.GetPosition(null).ToString();
        }

        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            textBox2.Text = "x:y = " + e.GetPosition(null).ToString();
            double x = e.GetPosition(null).X - 142.0;
            double y = e.GetPosition(null).Y - 142.0;
            double angle = Math.Atan2(y, x) * (180 / Math.PI);
            int degrees = (int)angle;
            textBox3.Text = degrees.ToString();
        }
    }
}

The angle from the center of object to orbiting second object is shown on following table. Notice that the opposite pole of 0 degrees is 180 degrees not -180 degrees.


No comments:

Post a Comment