您的位置:首页 > Web前端 > JavaScript

Javascript对象扩展 - Math

2006-09-27 13:23 246 查看
在Javascript程序设计中,时常会用到或新增自己编写的各类函数,随着此类小函数的数量的增加,管理上会比较繁重,如果把基本功能分类封装到各种既有对象或自定义类中去,如Object,Math,String,Number等等,使用的时候会非常的方便,并且更容易理解和管理。

现在已经实现的扩展有Math简单数学函数、模拟实现C++的CPoint的JsPoint类、模拟实现C++的CRect的JsRect矩形类、模拟实现C++的CRgn的JsRange类、对于线条的实现JsLine类等,将会陆续贴出。

下面是对Javascript的Math对象简单功能的扩展,直接输入角度值进行计算,扩展了弧度与角度的转换和坐标转换:

/*- ==========================================================
* 开发人员:卢印刚
* 编写时间:2006-9-26
* 函数名称:
* 参数说明:
* 功能说明:对Math对象的扩展
* 使用说明: 1、重写后的数学三角函数的参数不再是弧度,而是角度
* 2、Math.Radian(),将角度转换为弧度
* 3、Math.Angel(),将弧度转换为角度
* 4、Math.Sin(),计算角度的正弦
* 5、Math.Cin(),计算角度的余弦
* 6、Math.Tan(),计算角度的正切
* 7、Math.Pol(),通过x,y坐标计算距离和角度
* 7、Math.Rec(),通过距离和角度计算坐标
-*/
Math.Radian = function( angel ) { return angel*this.PI/180; }
Math.Angel = function( radian ) { return radian*180/this.PI; }
Math.Sin = function( angel ) { return this.sin( this.Radian( angel ) ); }
Math.Asin = function( nums ) { return this.Angel( this.asin( nums ) ); }
Math.Cos = function( angel ) { return this.cos( this.Radian( angel ) ); }
Math.Acos = function( nums ) { return this.Angel( this.acos( nums ) ); }
Math.Tan = function( angel ) { return this.tan( this.Radian( angel ) ); }
Math.Atan = function( nums ) { return this.Angel( this.atan( nums ) ); }
Math.Atan2 = function( x, y ) { return this.Angel( this.atan2( y, x ) ); }
Math.Pol = function( x, y ) { return [ this.sqrt( x*x + y*y ), this.Atan2( x, y ) ]; }
Math.Rec = function( dist, angel ) { return [ dist*this.Cos( angel ), dist*this.Sin( angel ) ]; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: