彩音 - Adobe AIR - 研究室:XION -Adobe AIR-laboratory
短形領域の加算 union()

union() は短形領域の加算を行う。短形領域の面積の足し算をするのではなく、互いの領域を完全に含む1つの広い領域を新規に生成する。ここの短形領域が狭くても、対角線上にはなれた位置にあるほど加算した結果は広い領域となる。
以下の例では、rect1 と rect2 を union() を使い加算した短形領域 rect3 と生成している。


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="600" height="256" initialize="init()">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
private var container:UIComponent = new UIComponent();
private function init():void {
addChild(container);

var rect1:Rectangle = new Rectangle(100, 50, 150, 120);
var rect2:Rectangle = new Rectangle(150, 150, 160, 80);
var rect3:Rectangle = rect1.union(rect2);
var lineColor:uint = 0x000000;
var fillColor:uint = 0xBBBBBB;

var box1:Sprite = new Sprite();
box1.graphics.lineStyle(1, lineColor);
box1.graphics.beginFill(fillColor);
box1.graphics.drawRect(rect1.x, rect1.y, rect1.width, rect1.height);
box1.graphics.endFill();
container.addChild(box1);

var box2:Sprite = new Sprite();
box2.graphics.lineStyle(1, lineColor);
box2.graphics.beginFill(fillColor);
box2.graphics.drawRect(rect2.x, rect2.y, rect2.width, rect2.height);
box2.graphics.endFill();
container.addChild(box2);

var box3:Sprite = new Sprite();
box3.graphics.lineStyle(2, lineColor);
box3.graphics.drawRect(rect3.x, rect3.y, rect3.width, rect3.height);
container.addChild(box3);
}
]]>
</mx:Script>
</mx:Application>

索引