写程序不能忘记数学啊

本文介绍了一段使用.NET WPF编写的代码,该代码使窗口背景颜色的灰度值随鼠标距离窗口中心的距离变化。文章详细解释了如何通过椭圆参数方程计算鼠标位置到椭圆上对应点的距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不仅仅是写数据结构与算法,写日常小程序也要数学知识呢!这会遇到的数学知识是高中的椭圆。

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} }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值