ActionScript 3.0 類結構探討筆記: Rectangle類

來源:互聯網
上載者:User

 package fx.gemo<br />{<br /> //Rectangle 對象是按其位置(由它左上方的點 (x, y) 確定)以及寬度和高度定義的地區。<br />import flash.geom.Point;<br />public class Rectangle2D<br />{<br />public var width:Number;<br />public var height:Number;<br />public var x:Number;<br />public var y:Number;<br />public function Rectangle2D(x:Number=0,y:Number=0,width:Number=0,height:Number=0)<br />{<br />this.x=x;<br />this.y=y;<br />this.width=width;<br />this.height=height;</p><p>}<br />//返回一個新的 Rectangle 對象,其 x、y、width 和 height 屬性的值與原始 Rectangle 對象的對應值相同。<br />public function clone():Rectangle2D<br />{<br />return new Rectangle2D(x,y,width,height);<br />}</p><p>//確定由此 Rectangle 對象定義的矩形地區內是否包含指定的點。<br />public function contains(dx:Number,dy:Number):Boolean<br />{<br /> return ((this.x< dx) &&( dx < this.right) && (this.y< dy) && (dy< this.bottom))?true:false;<br />}<br />//確定由此 Rectangle 對象定義的矩形地區內是否包含指定的點。<br />public function containsPoint(point:Point):Boolean<br />{<br />return contains(point.x,point.y);<br />}<br />//確定此 Rectangle 對象內是否包含由 rect 參數指定的 Rectangle 對象。</p><p>public function containsRect(rect:Rectangle2D):Boolean<br />{<br /> return (contains(rect.x,rect.y)&& contains(rect.x,rect.bottom) && contains(rect.right,rect.y) && contains(rect.right,rect.bottom)) ?true:false;</p><p>}<br />//確定在 toCompare 參數中指定的對象是否等於此 Rectangle 對象。<br />public function equals(toCompare:Rectangle2D):Boolean<br />{<br />return (x == toCompare.x && y == toCompare.y && width == toCompare.width && height == toCompare.width);</p><p>}<br />//按指定量增加 Rectangle 對象的大小(以像素為單位)。<br />public function inflate(dx:Number,dy:Number):void<br />{<br />width+= dx;<br />height+= dy;<br />}<br />//增加 Rectangle 對象的大小。<br />public function inflatePoint(point:Point):void<br />{<br />inflate(point.x,point.y);<br />}</p><p>//如果在 toIntersect 參數中指定的 Rectangle 對象與此 Rectangle 對象相交,<br />//則返回交集地區作為 Rectangle 對象。<br />public function intersection(toIntersect:Rectangle2D):Rectangle2D<br />{<br />var x1:Number;<br />var y1:Number;<br />var width1:Number;<br />var height1:Number;</p><p> if(intersects(toIntersect))<br /> {<br /> if(x>toIntersect.x&& y>toIntersect.y){ x1=x;y1=y;width1=toIntersect.right-x;height1=toIntersect.bottom-y;}<br /> if(x>toIntersect.x&& y<toIntersect.y){ x1=x;y1=toIntersect.y;width1=toIntersect.right-x;height1=this.bottom-toIntersect.y;}<br /> if(x<toIntersect.x&& y>toIntersect.y){ x1=toIntersect.x;y1=y;width1=this.right-toIntersect.x;height1=toIntersect.bottom-y;}<br /> if(x<toIntersect.x&& y<toIntersect.y) {x1=toIntersect.x;y1=toIntersect.y;width1=this.right-toIntersect.x;height1=this.bottom-toIntersect.y;}<br /> }<br /> else<br /> {<br /> x=0;y=0;width=0;height=0;<br /> }<br /> return new Rectangle2D(x1,y1,width1,height1);<br />}<br />//確定在 toIntersect 參數中指定的對象是否與此 Rectangle 對象相交<br />public function intersects(toIntersect:Rectangle2D):Boolean<br />{<br />var bool:Boolean;<br />if(this.contains(toIntersect.x,toIntersect.y) ||this.contains(x,toIntersect.bottom)||this.contains(toIntersect.right,toIntersect.y) || this.contains(toIntersect.right,toIntersect.bottom))<br /> bool=true;<br /> else<br /> bool= false;<br /> return bool;<br />}<br />public function isEmpty():Boolean<br />{<br />return (width == 0 || height == 0) ? true :false ;<br />}<br />//按指定量調整 Rectangle 對象的位置(由其左上方確定)。<br />public function offset(dx:Number,dy:Number):void<br />{<br />x+= dx;<br />y+= dy;<br />}</p><p>//將 Point 對象用作參數來調整 Rectangle 對象的位置<br />public function offsetPoint(point:Point):void<br />{<br />offset(point.x,point.y);<br />}<br />public function setEmpty():void<br />{<br />if (isEmpty())<br />return;<br />x=0;<br />y=0;<br />width=0;<br />height=0;<br />}<br />public function toString():String<br />{<br />return "(x=" + x + ",y=" + y + ",w=" + width + ",h=" + height + ")";<br />}</p><p>//通過填充兩個矩形之間的水平和垂直空間,將這兩個矩形組合在一起以建立一個新的 Rectangle 對象<br />public function union(toUnion:Rectangle2D):Rectangle2D<br />{<br />var rect:Rectangle2D;</p><p>if(x>=toUnion.x && y<=toUnion.y)<br />{<br />rect=new Rectangle2D(toUnion.x,this.y,this.x-toUnion.x+width,toUnion.y-this.y+toUnion.height);<br />}</p><p>if(x>=toUnion.x && y>=toUnion.y)<br />{<br />rect=new Rectangle2D(toUnion.x,toUnion.y,this.x-toUnion.x+width,this.y-toUnion.y+height);<br />}</p><p> if(x<=toUnion.x && y>=toUnion.y)<br />{<br />rect=new Rectangle2D(this.x,this.y,toUnion.x-this.x+toUnion.width,toUnion.y-this.y+toUnion.height);<br />}<br /> if(x<=toUnion.x && y<=toUnion.y)<br />{<br />rect= new Rectangle2D(this.x,this.y,toUnion.x-this.x+toUnion.width,toUnion.bottom-y);<br />}</p><p>return rect;<br />}</p><p>public function get bottom():Number<br />{<br />return y+height;<br />}</p><p>public function set bottom(value:Number):void<br />{<br /> height=value-y;<br />}<br />public function get bottomRight():Point<br />{<br />return new Point(x+width,y+height);<br />}</p><p>public function set bottomRight(value:Point):void<br />{<br />right=value.x;<br />bottom=value.y;<br />}</p><p>//矩形左上方的 x 座標<br />public function get left():Number<br />{<br />return x;<br />}</p><p>public function set left(value:Number):void<br />{<br />width=right-value;<br />x=value;<br />}<br />public function get right():Number<br />{<br />return x+width;<br />}<br />public function set right(value:Number):void<br />{<br />width=value-x;<br />}</p><p>//Rectangle 對象的大小,該對象表示為具有 width 和 height 屬性的值的 Point 對象。<br />public function get size():Point<br />{<br />return new Point(width,height);<br />}<br />public function set size(value:Point):void<br />{<br />width=value.x;<br />height=value.y;<br />}<br />//矩形左上方的 y 座標<br />public function get top():Number<br />{<br />return y;<br />}<br />public function set top(value:Number):void<br />{<br />height=bottom-value;//對高度影響<br />y=value;<br />}</p><p>public function get topLeft():Point<br />{<br />return new Point(x,y);<br />}</p><p>public function set topLeft(value:Point):void<br />{<br />height=bottom-value.y;//對高度影響<br />width=x-value.x+width;</p><p>x=value.x;<br />y=value.y;</p><p>}<br />}<br />}

 

  下面是adobe 官方的協助文檔方法:

 

 

 

 

Rectangle類是flash.gemo.Rectangle類庫裡面一個,參考裡面的方法,大概寫了一下裡面的結構。當是筆記記錄吧

過幾天再把Matrix 內部的結構看看是否能夠寫出來。通過這個類,從中可以看到一些涉及到數學模型的設計的方法,以及一些很常見的設計方法。這也是值得看看。瞭解了內部的過程,日後自己不容易受限制於別人的開發的類庫。

 

 

對比測試:

var rect1:Rectangle=new Rectangle(100,100,50,50);<br />var rect2:Rectangle=new Rectangle(120,120,50,50);<br />trace(rect1);<br />trace(rect1.left);</p><p>trace(rect1);</p><p>trace(rect1.union(rect2))<br />trace(rect1.intersects(rect2));<br />trace(rect1.contains(120,120))<br />trace("交集區"+rect1.intersection(rect2))<br />trace("原先Bottom值"+rect1.bottom);<br />rect1.bottom=30;<br />trace("bottom:"+rect1.bottom)<br />trace(rect1);</p><p>trace("原先left的值"+rect1.left)<br />rect1.left=200;<br />trace("left:"+rect1.left);<br />trace(rect1);<br />rect1.right=20;<br />trace(rect1);</p><p>trace("原先TOP"+rect1.top);<br />rect1.top=20;<br />trace(rect1);</p><p>rect1.topLeft=new Point(50,52);<br />trace("上左"+rect1);</p><p>rect1.bottomRight=new Point(100,52);<br />trace("右下"+rect1);</p><p>trace("****************************************");<br />import fx.gemo.Rectangle2D;<br />var rect:Rectangle2D=new Rectangle2D(100,100,50,50);<br />var rect3:Rectangle2D=new Rectangle2D(120,120,50,50);<br />trace(rect);<br />trace(rect.left);</p><p>trace(rect);<br />trace(rect.union(rect3))<br />trace(rect.intersects(rect3));<br />trace(rect.contains(120,120))<br />trace("交集區"+rect.intersection(rect3))<br />trace("原先Bottom值"+rect.bottom);<br />rect.bottom=30;<br />trace("bottom:"+rect.bottom)<br />trace(rect);</p><p>trace("原先left的值"+rect.left)<br />rect.left=200;<br />trace("left:"+rect.left);<br />trace(rect);<br />rect.right=20;<br />trace(rect);<br />trace("原先TOP"+rect.top);<br />rect.top=20;<br />trace(rect);</p><p>rect.topLeft=new Point(50,52);<br />trace("上左"+rect);</p><p>rect.bottomRight=new Point(100,52);<br />trace("右下"+rect);</p><p>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.