写程序不能忘记数学啊
2011-12-04 18:44
204 查看
不仅仅是写数据结构与算法,写日常小程序也要数学知识呢!这会遇到的数学知识是高中的椭圆。
这是用.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} }
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} }
相关文章推荐
- 讨论:写程序到底需不需要懂数学?(转摘)
- android keyguard锁屏程序不能全屏显示
- WIN7 64位可以上网 其他程序不能访问网络的问题
- C#编译时提示未能解析引用的程序(被引用项目编译成功,但引用项目编译时却不能正常引用)
- 关于出现错误:_tkinter.TclError: couldn't open "E:\Python系列\Python程序\Python安装.gif"tkinter中不能打开GIF格式图片的解决办法
- VC编写的程序不能在其他机器上运行的解决方案
- GetMessage用法错误导致程序不能退出
- 数学建模中的Matlab模板程序
- 第3周项目4-考了语文数学的学生(多文件形式组织程序)
- 自己写的小程序中密码和账号输入都正确,但是不能正确进入下一个界面
- 浅析为什么设置用户ID程序中不能直接调用system函数?
- liteIDE写Golang程序引用外面的package不能自动补全的问题
- 移植的的QT程序不能显示中文问题
- scanf("%c", &c);这句不能正常接收字符的原因。(附strindex的实现程序)
- Microsoft Excel 不能访问文件“ 文件名称或路径不存在。 • 文件正被其他程序使用。 • 您正要保存的工作簿与当前打开的工作簿同名。
- androidStudio出现Executing tasks: [:app:assembleDebug]等错误不能运行程序
- Windows安装程序不能验证update.inf文件的完整性.解决方法
- C++因为应用程序没关闭,不能编译程序
- VS2010最大的硬伤,编译出来的程序不能在XP SP2以下的系统运行
- 在IIS中不能启动网站, 提示: “另一个程序正在使用此文件,进程无法访问”