Area of a Triangle(三角形の面積)
3点の座標から三角形の面積を求めます。
find the area of a triangle from the coordinates of three points.
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hBWV
*/
package {
import flash.filters.GlowFilter;
import flash.geom.Rectangle;
import com.bit101.components.*;
import flash.events.Event;
import flash.display.*;
public class FlashTest extends Sprite {
public var text:InputText;
public var dots:Array = [];
public var r:Rectangle = new Rectangle( 10, 10, 445, 420 );
public var canvas:Sprite = new Sprite();
public var target:Object;
public function FlashTest() {
addChild( new Bitmap( new BitmapData(465,465,false,0x111111) ) )
addChild( canvas );
text = new InputText( this, 5, 440 );
text.setSize( 455, 20 );
for( var i:int = 0; i<3; i++ ){
var dot:Dot = new Dot();
dot.move( int( r.x + r.width* Math.random() ), int( r.y + r.height*Math.random() ) );
dot.addEventListener( "mouseDown", start );
dot.addEventListener( "mouseUp", stop );
stage.addEventListener( "mouseMove", move );
dot.buttonMode = true;
canvas.addChild( dot );
dots.push( dot );
}
update();
}
private function update():void{
var g:Graphics = canvas.graphics;
g.clear();
g.lineStyle( 3, 0x66CC66, 1 );
g.beginFill( 0xFFFFFF, 0.2 );
var d:Dot = dots[2];
g.moveTo( d.x, d.y );
for( var i:int = 0; i < 3; i++ ){
d = dots[i];
g.lineTo( d.x, d.y );
}
text.text = "S = " + GeomUtil.triangleSum( dots[0].x, dots[0].y, dots[1].x, dots[1].y, dots[2].x, dots[2].y ).toFixed(1);
}
private function start(e:Event):void{
target = e.target;
}
private function move(e:Event):void{
if( target ){
target.move( mouseX, mouseY );
update();
}
}
private function stop(e:Event):void{
target = null;
}
}
}
import flash.events.Event;
import flash.geom.Point;
import flash.display.Graphics;
import flash.display.Sprite;
import com.bit101.components.*;
class Dot extends Sprite{
private var lbl:Label;
function Dot(){
var g:Graphics = graphics;
g.beginFill( 0x00FF00, 1 );
g.drawRect( -5, -5, 10, 10 );
Style.LABEL_TEXT = 0xBB6666;
lbl = new Label( this, 0, 0, "" )
move(x,y);
}
public function move(x:Number,y:Number):void{
this.x = x; this.y = y; lbl.text = "(" + x + "," + y + ")";
}
}
class GeomUtil{
static public function triangleSum( ax:Number, ay:Number, bx:Number, by:Number, cx:Number, cy:Number ):Number{
var s:Number = 0.5 * ((cy-ay)*(bx-ax)-(cx-ax)*(by-ay))
return s>0?s:-s;
}
}