现在已经实现的扩展有Math简单数学函数、模拟实现C++的CPoint的JsPoint类、模拟实现C++的CRect的JsRect矩形类、模拟实现C++的CRgn的JsRange类、对于线条的实现JsLine类等,将会陆续贴出。
下面JsRect类是对C++的CRect类的实现,依附于JsPoint类和扩展后的Object对象、Math对象:
/*- ==========================================================
* 开发人员:卢印刚
* 编写时间:2006-9-26
* 函数名称:JsRect
* 参数说明:
* 功能说明:对C++的CRect类的实现
* 使用说明: 1、JsRect( JsPoint, JsPoint ),根据JsPoint构造
* 2、JsRect( Width, height ),根据宽度和高度构造
* 3、JsRect( JsPoint, Width, height ),根据点,宽度和高度构造
* 4、JsRect( left, top, right, bottom ),根据左,上,右,下构造
*
* 5、JsRect.Clone(),复制当前的JsRect对象并返回
* 6、JsRect.Copy( JsRect ),复制JsRect对象的数据作为当前
*
* 7、JsRect.SetRect( JsRect ),将JsRect对象的数据作为当前
* 8、JsRect.SetRect( JsPoint, JsPoint ),将左上角点,右下角点的数据作为当前
* 9、JsRect.SetRect( JsPoint, width, height ),将左上角点,宽度,高度的数据作为当前
* 10、JsRect.SetRect( left, top, right, bottom ),根据左,上,右,下的数据设置为当前
*
* 11、JsRect.Width(),返回JsRect的宽度
* 12、JsRect.Height(),返回JsRect的高度
* 13、JsRect.Empty(),将当前的JsRect清空,各数据为0
* 14、JsRect.IsEmpty(),判断当前JsRect的各数据是否为0
*
* 15、JsRect.PtInRect( JsPoint ),判断某JsPoint是否在当前的JsRect内
* 16、JsRect.PtInRect( x, y ),判断某x,y点是否在当前的JsRect内
*
* 17、JsRect.Equal( JsRect ),判断某JsRect是否与当前数据相同
* 18、JsRect.Intersect( JsRect ),返回某JsRect与当前JsRect相交后得到的JsRect
* 19、JsRect.IsIntersect( JsRect ),判断某JsRect是否与当前JsRect相交
* 20、JsRect.Merge( JsRect ),返回将某JsRect与当前JsRect合并后得到的JsRect
*
* 21、JsRect.Inflate( cx, cy ),将当前的JsRect扩大cx,cy
* 22、JsRect.Deflate( cx, cy ),将当前的JsRect缩小cx,cy
* 23、JsRect.Inflate( left, top, right, bottom ),将当前的JsRect扩大
* 24、JsRect.Deflate( left, top, right, bottom ),将当前的JsRect缩小
-*/
function JsRect()
{
this.left = this.top = this.right = this.bottom = 0;
switch( arguments.length )
{
case 1: this.SetRect( arguments[0] );break;
case 2: this.SetRect( arguments[0], arguments[1] );break;
case 3: this.SetRect( arguments[0], arguments[1], arguments[2] );break;
case 4: this.SetRect( arguments[0], arguments[1], arguments[2], arguments[3] );break;
}
this.version = function(){
alert( "JsRect1.0版\n\n作者:卢印刚\n\n2006.9.26\n\n版权所有");
}
}
JsRect.prototype.Clone = function() {
return ( new this.constructor ( this.left, this.top, this.right, this.bottom ) );
}
JsRect.prototype.Copy = function( JsRt ) {
if(JsRt.IsRect()){
this.left=JsRt.left;
this.top=JsRt.top;
this.right=JsRt.right;
this.bottom=JsRt.bottom;
}
}
JsRect.prototype.Width = function() {
return Math.abs( Math.abs( this.right ) - Math.abs( this.left ) );
}
JsRect.prototype.Height = function() {
return Math.abs( Math.abs( this.bottom ) - Math.abs( this.top ) );
}
JsRect.prototype.Empty = function() {
this.left = this.top = this.right = this.bottom = 0;
}
JsRect.prototype.IsEmpty = function() {
return ( this.left==this.top==this.right==this.bottom==0 ) ? true : false;
}
JsRect.prototype.SetRect = function() {
if ( arguments.length == 1 && arguments[0].IsRect() ){
//根据JsRect构造,JsRect( JsRect );
this.Copy( arguments[0] );
}else if ( arguments.length == 2 ){
//根据JsPoint构造,JsRect( JsPoint, JsPoint );
if ( arguments[0].IsPoint() && arguments[1].IsPoint() ){
this.left = Math.min( arguments[0].x, arguments[1].x ) ;
this.top = Math.min( arguments[0].y, arguments[1].y ) ;
this.right = Math.max( arguments[0].x, arguments[1].x ) ;
this.bottom = Math.max( arguments[0].y, arguments[1].y ) ;
}
}else if ( arguments.length == 3 ){
//根据点,宽度和高度构造,JsRect( JsPoint, Width, height )
if ( arguments[0].IsPoint() && arguments[1].IsNum() && arguments[2].IsNum() )
{
this.left = arguments[0].x ;
this.top = arguments[0].y ;
this.right = Math.abs( arguments[1] ) + this.left ;
this.bottom = Math.abs( arguments[2] ) + this.top ;
}
}else if ( arguments.length == 4 ){
//根据左,上,右,下构造,JsRect( left, top, right, bottom )
if ( arguments[0].IsNum() && arguments[1].IsNum() && arguments[2].IsNum() && arguments[3].IsNum() )
{
this.left = Math.min( arguments[0], arguments[2] ) ;
this.top = Math.min( arguments[1], arguments[3] ) ;
this.right = Math.max( arguments[0], arguments[2] ) ;
this.bottom = Math.max( arguments[1], arguments[3] ) ;
}
}
}
JsRect.prototype.PtInRect = function() {
var x = y =0;
if ( arguments.length == 1 && arguments[0].IsPoint() ){
x = arguments[0].x;
y = arguments[0].y;
}else if ( arguments.length == 2 && arguments[0].IsNum() && arguments[1].IsNum() ){
x = arguments[0];
y = arguments[1];
}
return ( x > this.left && x < this.right && y > this.top && y < this.bottom ) ? true : false;
}
JsRect.prototype.Equal = function( JsRt ) {
return ( JsRt.IsRect() && this.left == JsRt.left && this.top == JsRt.top
&& this.right == JsRt.right && this.bottom == JsRt.bottom ) ? true : false;
}
JsRect.prototype.Intersect = function( JsRt ) {
if ( !JsRt.IsRect() ) return null;
var left = Math.max( this.left, JsRt.left );
var top = Math.max( this.top, JsRt.top );
var right = Math.min( this.right, JsRt.right );
var bottom = Math.min( this.bottom, JsRt.bottom );
return ( new this.constructor ( left, top, right, bottom ) );
}
JsRect.prototype.IsIntersect = function( JsRt ) {
return ( JsRt.IsRect() && this.PtInRect( JsRt.left, JsRt.right )
&& JsRt.PtInRect( this.right, this.bottom ) ) ? true : false;
}
JsRect.prototype.Merge = function () {
var oldleft = oldright = oldtop = oldbottom = 0;
if ( arguments.length == 1 ){
if ( !arguments[0].IsRect() ) return null;
oldleft = arguments[0].left;
oldtop = arguments[0].top;
oldright = arguments[0].right;
oldbottom = arguments[0].bottom;
}else if ( arguments.length == 4 ){
if ( !arguments[0].IsNum() || !arguments[1].IsNum()
|| !arguments[2].IsNum()|| !arguments[3].IsNum() ) return null;
oldleft = Math.min( arguments[0], arguments[2] );
oldtop = Math.min( arguments[1], arguments[3] );
oldright = Math.max( arguments[0], arguments[2] );
oldbottom = Math.max( arguments[1], arguments[3] );
}
oldleft = Math.min( this.left, arguments[0] );
oldtop = Math.min( this.top, arguments[1] );
oldright = Math.max( this.right, arguments[2] );
oldbottom = Math.max( this.bottom, arguments[3] );
return ( new this.constructor ( oldleft, oldtop, oldright, oldbottom ) );
}
JsRect.prototype.Inflate = function () {
if ( arguments.length == 2 ){
if ( arguments[0].IsNum() && arguments[1].IsNum() )
{
this.left -= Math.abs( arguments[0] ) ;
this.right += Math.abs( arguments[0] ) ;
this.top -= Math.abs( arguments[1] ) ;
this.bottom += Math.abs( arguments[1] ) ;
}
}else if ( arguments.length == 4 ){
if ( arguments[0].IsNum() && arguments[1].IsNum()
&& arguments[2].IsNum() && arguments[3].IsNum() ){
this.left -= Math.abs( arguments[0] ) ;
this.right += Math.abs( arguments[2] ) ;
this.top -= Math.abs( arguments[1] ) ;
this.bottom += Math.abs( arguments[3] ) ;
}
}
}
JsRect.prototype.Deflate = function () {
if ( arguments.length == 2 ){
if ( arguments[0].IsNum() && arguments[1].IsNum() )
{
this.left += Math.abs( arguments[0] ) ;
this.right -= Math.abs( arguments[0] ) ;
this.top += Math.abs( arguments[1] ) ;
this.bottom -= Math.abs( arguments[1] ) ;
}
}else if ( arguments.length == 4 ){
if ( arguments[0].IsNum() && arguments[1].IsNum()
&& arguments[2].IsNum() && arguments[3].IsNum() ){
this.left += Math.abs( arguments[0] ) ;
this.right -= Math.abs( arguments[2] ) ;
this.top += Math.abs( arguments[1] ) ;
this.bottom -= Math.abs( arguments[3] ) ;
}
}
}
* 开发人员:卢印刚
* 编写时间:2006-9-26
* 函数名称:JsRect
* 参数说明:
* 功能说明:对C++的CRect类的实现
* 使用说明: 1、JsRect( JsPoint, JsPoint ),根据JsPoint构造
* 2、JsRect( Width, height ),根据宽度和高度构造
* 3、JsRect( JsPoint, Width, height ),根据点,宽度和高度构造
* 4、JsRect( left, top, right, bottom ),根据左,上,右,下构造
*
* 5、JsRect.Clone(),复制当前的JsRect对象并返回
* 6、JsRect.Copy( JsRect ),复制JsRect对象的数据作为当前
*
* 7、JsRect.SetRect( JsRect ),将JsRect对象的数据作为当前
* 8、JsRect.SetRect( JsPoint, JsPoint ),将左上角点,右下角点的数据作为当前
* 9、JsRect.SetRect( JsPoint, width, height ),将左上角点,宽度,高度的数据作为当前
* 10、JsRect.SetRect( left, top, right, bottom ),根据左,上,右,下的数据设置为当前
*
* 11、JsRect.Width(),返回JsRect的宽度
* 12、JsRect.Height(),返回JsRect的高度
* 13、JsRect.Empty(),将当前的JsRect清空,各数据为0
* 14、JsRect.IsEmpty(),判断当前JsRect的各数据是否为0
*
* 15、JsRect.PtInRect( JsPoint ),判断某JsPoint是否在当前的JsRect内
* 16、JsRect.PtInRect( x, y ),判断某x,y点是否在当前的JsRect内
*
* 17、JsRect.Equal( JsRect ),判断某JsRect是否与当前数据相同
* 18、JsRect.Intersect( JsRect ),返回某JsRect与当前JsRect相交后得到的JsRect
* 19、JsRect.IsIntersect( JsRect ),判断某JsRect是否与当前JsRect相交
* 20、JsRect.Merge( JsRect ),返回将某JsRect与当前JsRect合并后得到的JsRect
*
* 21、JsRect.Inflate( cx, cy ),将当前的JsRect扩大cx,cy
* 22、JsRect.Deflate( cx, cy ),将当前的JsRect缩小cx,cy
* 23、JsRect.Inflate( left, top, right, bottom ),将当前的JsRect扩大
* 24、JsRect.Deflate( left, top, right, bottom ),将当前的JsRect缩小
-*/
function JsRect()
{
this.left = this.top = this.right = this.bottom = 0;
switch( arguments.length )
{
case 1: this.SetRect( arguments[0] );break;
case 2: this.SetRect( arguments[0], arguments[1] );break;
case 3: this.SetRect( arguments[0], arguments[1], arguments[2] );break;
case 4: this.SetRect( arguments[0], arguments[1], arguments[2], arguments[3] );break;
}
this.version = function(){
alert( "JsRect1.0版\n\n作者:卢印刚\n\n2006.9.26\n\n版权所有");
}
}
JsRect.prototype.Clone = function() {
return ( new this.constructor ( this.left, this.top, this.right, this.bottom ) );
}
JsRect.prototype.Copy = function( JsRt ) {
if(JsRt.IsRect()){
this.left=JsRt.left;
this.top=JsRt.top;
this.right=JsRt.right;
this.bottom=JsRt.bottom;
}
}
JsRect.prototype.Width = function() {
return Math.abs( Math.abs( this.right ) - Math.abs( this.left ) );
}
JsRect.prototype.Height = function() {
return Math.abs( Math.abs( this.bottom ) - Math.abs( this.top ) );
}
JsRect.prototype.Empty = function() {
this.left = this.top = this.right = this.bottom = 0;
}
JsRect.prototype.IsEmpty = function() {
return ( this.left==this.top==this.right==this.bottom==0 ) ? true : false;
}
JsRect.prototype.SetRect = function() {
if ( arguments.length == 1 && arguments[0].IsRect() ){
//根据JsRect构造,JsRect( JsRect );
this.Copy( arguments[0] );
}else if ( arguments.length == 2 ){
//根据JsPoint构造,JsRect( JsPoint, JsPoint );
if ( arguments[0].IsPoint() && arguments[1].IsPoint() ){
this.left = Math.min( arguments[0].x, arguments[1].x ) ;
this.top = Math.min( arguments[0].y, arguments[1].y ) ;
this.right = Math.max( arguments[0].x, arguments[1].x ) ;
this.bottom = Math.max( arguments[0].y, arguments[1].y ) ;
}
}else if ( arguments.length == 3 ){
//根据点,宽度和高度构造,JsRect( JsPoint, Width, height )
if ( arguments[0].IsPoint() && arguments[1].IsNum() && arguments[2].IsNum() )
{
this.left = arguments[0].x ;
this.top = arguments[0].y ;
this.right = Math.abs( arguments[1] ) + this.left ;
this.bottom = Math.abs( arguments[2] ) + this.top ;
}
}else if ( arguments.length == 4 ){
//根据左,上,右,下构造,JsRect( left, top, right, bottom )
if ( arguments[0].IsNum() && arguments[1].IsNum() && arguments[2].IsNum() && arguments[3].IsNum() )
{
this.left = Math.min( arguments[0], arguments[2] ) ;
this.top = Math.min( arguments[1], arguments[3] ) ;
this.right = Math.max( arguments[0], arguments[2] ) ;
this.bottom = Math.max( arguments[1], arguments[3] ) ;
}
}
}
JsRect.prototype.PtInRect = function() {
var x = y =0;
if ( arguments.length == 1 && arguments[0].IsPoint() ){
x = arguments[0].x;
y = arguments[0].y;
}else if ( arguments.length == 2 && arguments[0].IsNum() && arguments[1].IsNum() ){
x = arguments[0];
y = arguments[1];
}
return ( x > this.left && x < this.right && y > this.top && y < this.bottom ) ? true : false;
}
JsRect.prototype.Equal = function( JsRt ) {
return ( JsRt.IsRect() && this.left == JsRt.left && this.top == JsRt.top
&& this.right == JsRt.right && this.bottom == JsRt.bottom ) ? true : false;
}
JsRect.prototype.Intersect = function( JsRt ) {
if ( !JsRt.IsRect() ) return null;
var left = Math.max( this.left, JsRt.left );
var top = Math.max( this.top, JsRt.top );
var right = Math.min( this.right, JsRt.right );
var bottom = Math.min( this.bottom, JsRt.bottom );
return ( new this.constructor ( left, top, right, bottom ) );
}
JsRect.prototype.IsIntersect = function( JsRt ) {
return ( JsRt.IsRect() && this.PtInRect( JsRt.left, JsRt.right )
&& JsRt.PtInRect( this.right, this.bottom ) ) ? true : false;
}
JsRect.prototype.Merge = function () {
var oldleft = oldright = oldtop = oldbottom = 0;
if ( arguments.length == 1 ){
if ( !arguments[0].IsRect() ) return null;
oldleft = arguments[0].left;
oldtop = arguments[0].top;
oldright = arguments[0].right;
oldbottom = arguments[0].bottom;
}else if ( arguments.length == 4 ){
if ( !arguments[0].IsNum() || !arguments[1].IsNum()
|| !arguments[2].IsNum()|| !arguments[3].IsNum() ) return null;
oldleft = Math.min( arguments[0], arguments[2] );
oldtop = Math.min( arguments[1], arguments[3] );
oldright = Math.max( arguments[0], arguments[2] );
oldbottom = Math.max( arguments[1], arguments[3] );
}
oldleft = Math.min( this.left, arguments[0] );
oldtop = Math.min( this.top, arguments[1] );
oldright = Math.max( this.right, arguments[2] );
oldbottom = Math.max( this.bottom, arguments[3] );
return ( new this.constructor ( oldleft, oldtop, oldright, oldbottom ) );
}
JsRect.prototype.Inflate = function () {
if ( arguments.length == 2 ){
if ( arguments[0].IsNum() && arguments[1].IsNum() )
{
this.left -= Math.abs( arguments[0] ) ;
this.right += Math.abs( arguments[0] ) ;
this.top -= Math.abs( arguments[1] ) ;
this.bottom += Math.abs( arguments[1] ) ;
}
}else if ( arguments.length == 4 ){
if ( arguments[0].IsNum() && arguments[1].IsNum()
&& arguments[2].IsNum() && arguments[3].IsNum() ){
this.left -= Math.abs( arguments[0] ) ;
this.right += Math.abs( arguments[2] ) ;
this.top -= Math.abs( arguments[1] ) ;
this.bottom += Math.abs( arguments[3] ) ;
}
}
}
JsRect.prototype.Deflate = function () {
if ( arguments.length == 2 ){
if ( arguments[0].IsNum() && arguments[1].IsNum() )
{
this.left += Math.abs( arguments[0] ) ;
this.right -= Math.abs( arguments[0] ) ;
this.top += Math.abs( arguments[1] ) ;
this.bottom -= Math.abs( arguments[1] ) ;
}
}else if ( arguments.length == 4 ){
if ( arguments[0].IsNum() && arguments[1].IsNum()
&& arguments[2].IsNum() && arguments[3].IsNum() ){
this.left += Math.abs( arguments[0] ) ;
this.right -= Math.abs( arguments[2] ) ;
this.top += Math.abs( arguments[1] ) ;
this.bottom -= Math.abs( arguments[3] ) ;
}
}
}