求解释一段代码,与数学椭圆相关
- C# code
protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e){ base.OnMouseMove(e); double width = ActualWidth - 2 * SystemParameters.ResizeFrameVerticalBorderWidth; double height = ActualHeight - 2 * SystemParameters.ResizeFrameHorizontalBorderHeight - SystemParameters.CaptionHeight; Point ptMouse = e.GetPosition(this);//获取鼠标在窗口中的位置 Point ptCenter = new Point(width / 2, height / 2); Vector vectMouse = ptMouse - ptCenter; double angle = Math.Atan2(vectMouse.X, vectMouse.Y); Vector vectEllipse = new Vector(width / 2 * Math.Cos(angle), height / 2 * Math.Sin(angle));//不懂!!! byte level = (byte)(255 * (1 - Math.Min(1, vectMouse.Length / vectEllipse.Length))); SolidColorBrush brush = ((SolidColorBrush)Background); brush.Color = Color.FromRgb(level, level, level); Console.WriteLine(level);}这是用.net WPF写的一段代码,让窗口的灰度随鼠标到窗口中心的距离而变化。鼠标在窗口中心,背景色是白色;鼠标在窗口边缘,背景色是黑色。
我看到标注的地方就不懂了,求解答。。。貌似跟椭圆的数学知识有关,求详解!十分感谢。
[解决办法]
angle是窗体内切椭圆的离心角。
利用椭圆的参数方程
x=a*cos(angle)
y=b*sin(angle)
所得到的(x,y)不是鼠标位置到圆心的连线,在椭圆上的交点;而是鼠标位置向X轴做垂线,垂线与椭圆的交点。
当然可以求得鼠标位置到圆心的连线,在椭圆上的交点,但计算复杂。
x= { ab } over { sqrt {b^2 +a ^2 tan %theta } }
y= { ab tan %theta } over { sqrt {b ^2 +a ^2 tan %theta} }