flash on 2010-10-27
♥0 |
Line 124 |
Modified 2010-10-27 15:13:26 |
MIT License
archived:2017-03-20 20:22:14
ActionScript3 source code
/**
* Copyright hacker_y48qdmdh ( http://wonderfl.net/user/hacker_y48qdmdh )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zGPh
*/
package {
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Graphics;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
stage.addEventListener( Event.ENTER_FRAME, this.enterframe );
stage.addEventListener( KeyboardEvent.KEY_DOWN, this.key_down_event );
}
private var line_length:Number = 100;
private var line_p0:Vector2 = new Vector2();
private var line_p1:Vector2 = new Vector2();
private var circle_center:Vector2 = new Vector2(200, 200);
private var circle_radius:Number = 100;
private function ctrl():void {
this.line_p0.x = mouseX;
this.line_p0.y = mouseY;
this.line_p1.x = this.line_p0.x - this.line_length;
this.line_p1.y = this.line_p0.y;
// 計算
var p0:Vector2 = this.line_p0;
var p1:Vector2 = this.line_p1;
var p:Vector2 = this.circle_center;
var v:Vector2 = p1.sub( p0 );
var a:Vector2 = p.sub( p0 );
var inner:Number = v.dot( a );
var v2:Number = v.lengthSquared();
var a2:Number = a.lengthSquared();
//var t:Number = inner / l2;
}
private fucntion eject_c( p:Vector2, center:Vector2, r:Number ):Vector2 {
var ret:Vector2 = null;
var v:Vector2 = p.sub( center );
var l2 = v.lengthSquared();
if( l2 > r*r ){
var l:Number = Math.sqrt( l2 );
if( l > 0 ){
ret = v.multi( (r-l)/l );
}
}
return ret;
}
private function key_down_event( event:KeyboardEvent ):void {
switch( event.keyCode ) {
case Keyboard.UP:
this.line_length += 5;
break;
case Keyboard.DOWN:
this.line_length -= 5;
break;
}
}
private function disp():void{
var g :Graphics = graphics;
g.clear();
// circle
g.lineStyle( 0x0, 0x0 );
g.drawCircle( this.circle_center.x, this.circle_center.y, this.circle_radius );
// center
g.moveTo( this.circle_center.x - 5, this.circle_center.y );
g.lineTo( this.circle_center.x + 5, this.circle_center.y );
g.moveTo( this.circle_center.x, this.circle_center.y - 5 );
g.lineTo( this.circle_center.x, this.circle_center.y + 5 );
// line
g.moveTo( this.line_p0.x, this.line_p0.y );
g.lineTo( this.line_p1.x, this.line_p1.y );
}
private function enterframe( event:Event ):void {
this.ctrl();
this.disp();
}
}
}
class Vector2 {
public var x:Number = 0;
public var y:Number = 0;
// コンストラクタ
public function Vector2( in_x:Number =0, in_y:Number =0 ) {
this.x = in_x;
this.y = in_y;
}
public function set( in_x:Number =0, in_y:Number =0 ):void {
this.x = in_x;
this.y = in_y;
}
// 和算
//public function add( that:Vector2 ):Vector2 {
//var ret:Vector2 = new Vector2( this.x + that.x, this.y + that.y );
//
//return ret;
//}
public function add_equal( that:Vector2 ):void {
this.x += that.x;
this.y += that.y;
}
// 減算
public function sub( that:Vector2 ):Vector2 {
var ret:Vector2 = new Vector2( this.x - that.x, this.y - that.y );
return ret;
}
public function sub_equal( that:Vector2 ):void {
this.x -= that.x;
this.y -= that.y;
}
// 乗算
public function multi( num:Number ):Vector2 {
var ret:Vector2 = new Vector2( this.x * num, this.x * num );
return ret;
}
public function multi_equal( num:Number ):void {
this.x *= num;
this.y *= num;
}
// 除算
public function div( num:Number ):Vector2 {
var ret:Vector2 = new Vector2( this.x / num, this.x / num );
return ret;
}
public function div_equal( num:Number ):void {
this.x /= num;
this.y /= num;
}
// クローン作成
public function copy():Vector2 {
var ret:Vector2 = new Vector2( this.x, this.y );
return ret;
}
// 値コピー
public function equal( src:Vector2 ):void {
this.x = src.x;
this.y = src.y;
}
public function dot( that:Vector2 ):Number{
return this.x*that.y + this.y*that.x;
}
public function lengthSquared( ):Number{
return this._x*this.x + this.y*this.y;
}
}