您的位置:首页 > 其它

写程序不能忘记数学啊

2011-12-04 18:44 204 查看
不仅仅是写数据结构与算法,写日常小程序也要数学知识呢!这会遇到的数学知识是高中的椭圆。

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} }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐