In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

# ２つの円を円弧で繋ぐ

```２つの円を円弧で繋ぐ
* リアルな水滴の表現に向けて目玉焼きみたいな
* 例外処理は未実装```
by Kay 02 Mar 2009
```/*
* ２つの円を円弧で繋ぐ
* リアルな水滴の表現に向けて目玉焼きみたいな
* 例外処理は未実装
*/
package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;
[SWF(width=400, height=400, backgroundColor=0x333366)]
public class Take05 extends Sprite {
private const SW:Number = stage.stageWidth;
private const SH:Number = stage.stageHeight;
private var circleA:Circle;
private var circleB:Circle;
private var nA:Number = 0;
private var nB:Number = 0;
public function Take05():void {
circleA = new Circle();
circleA.x = SW/2-55;
circleA.y = SH/2+55;
circleB = new Circle();
circleB.x = SW/2+90;
circleB.y = SH/2-90;
// circleA,Bの位置を常時移動する
}
private function moveCircleB(eventObject:Event):void {
nA+=0.07;
circleA.y = SH/2 + 60*Math.sin(nA);
nB+=0.15;
circleB.y = SH/2 + 100*Math.sin(nB);
graphics.clear();
// ２つの円を曲線で繋ぐ
// オマケ
graphics.beginFill(0xffcc00);
graphics.endFill();
graphics.beginFill(0xffcc00);
graphics.endFill();
graphics.lineStyle(16, 0xffffff);
var pA:Point = new Point(circleA.x, circleA.y);
var pB:Point = new Point(circleB.x, circleB.y);
}
// ２つの円を繋ぐ接続円を描く
private function getConectCenter(circleA:Circle, circleB:Circle, radiusC:Number):void {
// ３辺の長さを求める
var pointA:Point = new Point(circleA.x,circleA.y);
var pointB:Point = new Point(circleB.x,circleB.y);
var distAB:Number = Point.distance(pointA,pointB);
if (distAB < distBC+distAC) {
// ヘロンの公式で三角形の高さを求める
var nABC:Number = (distAB+distAC+distBC)/2;
var area:Number = Math.sqrt(nABC*(nABC-distAB)*(nABC-distAC)*(nABC-distBC));
var nH:Number = area/distAB*2;
// ∠CABを求める
var radianCAB:Number = Math.asin(nH / distAC);
// pointAとpointBの角度を求める
// 交点C座標を求める
pointC.offset(pointA.x, pointA.y);
// もう１つの交点D座標を求める
pointD.offset(pointA.x, pointA.y);
// ２つの円を円弧で繋いだ線を描画
graphics.beginFill(0xffffff);
graphics.lineStyle(16,0xffff99);
var vAC:Number = Math.atan2((pointC.y-pointA.y),(pointC.x-pointA.x));
var vBC:Number = Math.atan2((pointC.y-pointB.y),(pointC.x-pointB.x));
var vBD:Number = Math.atan2((pointD.y-pointB.y),(pointD.x-pointB.x));
pointBegin.offset(pointA.x, pointA.y);
graphics.moveTo(pointBegin.x, pointBegin.y);
graphics.endFill();
// オマケ
graphics.beginFill(0xff6666);
graphics.endFill();
graphics.beginFill(0xff6666);
graphics.endFill();
graphics.lineStyle(16,0xffffff);
}
}
}
private function drawArc(center:Point, radius:Number, begin:Number, end:Number):void {
// 描画範囲を取得
// 描画開始点を取得
pBegin.offset(center.x, center.y);
pEnd.offset(center.x, center.y);
// 中間点の数を求める
// 分割する角度を求める
// 開始点を求める
for (var i:uint = 0; i < nStep; i++) {
// 到達点
pTarget.offset(center.x, center.y);
// コントロールポイント（pControl）を求める
pControl.offset(center.x, center.y);
graphics.curveTo(pControl.x, pControl.y, pTarget.x, pTarget.y);
// 開始点を更新
}
// 最終到達点までを描く
pControl.offset(center.x, center.y);
graphics.curveTo(pControl.x, pControl.y, pEnd.x, pEnd.y);
}
}
}
import flash.display.Sprite;
class Circle extends Sprite {
public var color:uint = 0;
public var nX:Number = 0;
public var nY:Number = 0;
public function Circle() {
}
}```