sin cos tan
package {
import flash.filters.GlowFilter;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.events.MouseEvent;
import flash.display.Shape;
import flash.display.Graphics;
import flash.display.Sprite;
public class Main extends Sprite {
public function Main() {
// write as3 code here..
var unitCircle:Sprite = new Sprite();
var unitCircleGraph:Graphics = unitCircle.graphics;
unitCircleGraph.lineStyle(0);
unitCircleGraph.drawCircle(stage.stageWidth/2,stage.stageHeight/2,stage.stageWidth / 2 - 50);
addChild(unitCircle);
var axis:Shape = new Shape();
var axisGraph:Graphics = axis.graphics;
axisGraph.lineStyle(0);
axisGraph.moveTo(0,stage.stageHeight/2);
axisGraph.lineTo(stage.stageWidth,stage.stageHeight/2);
axisGraph.moveTo(stage.stageWidth/2,0);
axisGraph.lineTo(stage.stageWidth/2,stage.stageHeight);
addChild(axis);
var refPoint:Sprite = new Sprite();
var refPointGraph:Graphics = refPoint.graphics;
refPointGraph.beginFill(0x0);
refPointGraph.drawCircle(0,0,7);
refPoint.x = stage.stageWidth/2 + Math.cos(-1) * unitCircle.width/2;
refPoint.y = stage.stageHeight/2 + Math.sin(-1) * unitCircle.height/2;
refPointGraph.endFill();
addChild(refPoint);
var redTrans:ColorTransform = new ColorTransform(0,0,0,1,225,0,0,0);
var blackTrans:ColorTransform = new ColorTransform(0,0,0,1,0,0,0,0);
refPoint.buttonMode = true;
var onMouseOver:Boolean = false;
refPoint.addEventListener(MouseEvent.MOUSE_OVER,function ():void{
onMouseOver = true;})
refPoint.addEventListener(MouseEvent.MOUSE_OUT,function ():void{
onMouseOver = false;});
var dragging:Boolean = false;
refPoint.addEventListener(MouseEvent.MOUSE_DOWN,function():void{
dragging = true;})
stage.addEventListener(MouseEvent.MOUSE_UP,function():void{
dragging = false;})
addEventListener(Event.ENTER_FRAME,enterFrame);
function enterFrame(e:Event):void{
if(dragging || onMouseOver) {refPoint.transform.colorTransform = redTrans; refPoint.filters =
[new GlowFilter()]}
else {refPoint.transform.colorTransform = blackTrans; refPoint.filters = []}
moveRefPoint();
adjustLines();
setText();
}
var toMouseRad:Number = 0;
var hitRange:int = 50;
var refCoordinate:Point = new Point(refPoint.x + 7, refPoint.y + 7);
function moveRefPoint():void{
if(dragging && mouseX > 0 && mouseX < stage.stageWidth && mouseY > 0 && mouseY < stage.stageWidth){
refCoordinate = new Point(refPoint.x + 7, refPoint.y + 7);
toMouseRad = Math.atan2(mouseY - stage.stageHeight/2,mouseX - stage.stageWidth/2);
refPoint.x = Math.cos(toMouseRad) * (stage.stageWidth / 2 - 50) + stage.stageWidth/2;
refPoint.y = Math.sin(toMouseRad) * (stage.stageWidth / 2 - 50) + stage.stageHeight/2;
}
else dragging = false;
}
var sinLine:Sprite = new Sprite();
var cosLine:Sprite = new Sprite();
var radiusLine:Sprite = new Sprite();
addChildAt(radiusLine,1)
addChildAt(sinLine,2);
addChild(cosLine);
function drawLine(sp:Sprite,x1:int,y1:int,x2:int,y2:int,color:uint):void{
sp.graphics.clear();
sp.graphics.lineStyle(3,color);
sp.graphics.moveTo(x1,y1);
sp.graphics.lineTo(x2,y2);
}
function adjustLines():void{
drawLine(sinLine,refPoint.x,stage.stageHeight/2,refPoint.x,refPoint.y,0x00DD00);
drawLine(cosLine,stage.stageWidth/2,stage.stageHeight/2,refPoint.x,stage.stageHeight/2,0xDD0000);
drawLine(radiusLine,stage.stageWidth/2,stage.stageHeight/2,refPoint.x,refPoint.y,0xFFAA00);
}
var sintext:TextField = new TextField();
var costext:TextField = new TextField();
var tantext:TextField = new TextField();
var theta:TextField = new TextField();
sintext.selectable = costext.selectable = tantext.selectable = theta.selectable = false;
costext.y = 15;
tantext.y = 30;
theta.y = 45;
addChild(sintext); addChild(costext); addChild(tantext); addChild(theta);
var autotext:String = TextFieldAutoSize.LEFT
function setText():void{
tantext.text = "tanθ = " +
String(Math.floor((stage.stageHeight/2 - refPoint.y) / (refPoint.x - stage.stageWidth/2) * 10000) / 10000);
tantext.autoSize = autotext;
sintext.text = "sinθ = " +
- Math.floor((refPoint.y - stage.stageHeight/2) / Math.sqrt((refPoint.x - stage.stageWidth/2) * (refPoint.x - stage.stageWidth/2) + (refPoint.y - stage.stageHeight/2) * (refPoint.y - stage.stageHeight/2)) * 10000 ) / 10000;
sintext.autoSize = autotext;
costext.text = "cosθ = " +
Math.floor((refPoint.x - stage.stageWidth/2) / Math.sqrt((refPoint.x - stage.stageWidth/2) * (refPoint.x - stage.stageWidth/2) + (refPoint.y - stage.stageHeight/2) * (refPoint.y - stage.stageHeight/2)) * 10000 )/ 10000;
costext.autoSize = autotext;
theta.text = "θ = " + Math.floor ( - Math.atan2(refPoint.y - stage.stageHeight/2,refPoint.x - stage.stageWidth/2) * 180 / Math.PI * 10) / 10 + "°";
}
}
}
}