傾いた矩形の内側か外側かの判定
/**
* Copyright shin-go ( http://wonderfl.net/user/shin-go )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uS0C
*/
package {
import flash.events.MouseEvent;
import flash.display.ActionScriptVersion;
import flash.geom.Point;
import flash.text.TextField;
import flash.display.Sprite;
public class FlashTest extends Sprite{
protected var _x:Number=160;
protected var _y:Number=160;
protected var _w:Number=200;
protected var _h:Number=100;
protected var _rotation:Number=30;
protected var p0:Point;
protected var p1:Point;
protected var p2:Point;
protected var p3:Point;
protected var txt:TextField;
public function FlashTest() {
var rad:Number=_rotation*Math.PI/180;
var cos:Number=Math.cos(rad);
var sin:Number=Math.sin(rad);
p0=new Point(_x, _y);
p1=new Point(_x+cos*_w, _y+sin*_w);
p2=new Point(_x+cos*_w-sin*_h, _y+sin*_w+cos*_h);
p3=new Point(_x-sin*_h, _y+cos*_h);
txt=addChild(new TextField()) as TextField;
txt.text="";
this.graphics.lineStyle(0,0x999999,1);
this.graphics.moveTo(p0.x,p0.y);
this.graphics.lineTo(p1.x,p1.y);
this.graphics.lineTo(p2.x,p2.y);
this.graphics.lineTo(p3.x,p3.y);
this.graphics.lineTo(p0.x,p0.y);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
}
protected function onMouseMove(e:MouseEvent):void{
//trace("hoge");
var mouseP:Point=new Point(e.localX,e.localY);
if(isLower(p0,p1,mouseP)&&
isLower(p1,p2,mouseP)&&
isLower(p2,p3,mouseP)&&
isLower(p3,p0,mouseP)
){
trace("内側");
}else{
trace("外側");
}
e.updateAfterEvent();
}
protected function isLower(c:Point,p1:Point,p2:Point):Boolean{
//trace([Math.round(Math.atan2(p1.y-c.y,p1.x-c.x)*100)*0.01,Math.round(Math.atan2(p2.y-c.y,p2.x-c.x)*100)*0.01]);
return (Math.atan2(p2.y-c.y,p2.x-c.x)-Math.atan2(p1.y-c.y,p1.x-c.x)+Math.PI*2)%(Math.PI*2)<Math.PI;
}
public function trace(_str:*):void{
txt.text=(_str.toString());
}
}
}