//created by xiangming. http://www.flashxm.com
class Axis extends MovieClip {
private var axis_mc:MovieClip;
private var func_mc:MovieClip;
private var arrx:Arrow;
private var arry:Arrow;
public var _step:Number;
public var _x:Number;
public var _y:Number;
public var _left:Number;
public var _right:Number;
public var _top:Number;
public var _bottom:Number;
private var intervalID:Number;
public function Axis(target:MovieClip, ox:Number, oy:Number, step:Number, gridLength:Number, xLeft:Number, xRight:Number, yUp:Number, yDown:Number, color:Number, depth:Number) {
this._step = step;
this._x = ox;
this._y = oy;
this._left = xLeft;
this._right = xRight;
this._top = yDown;
this._bottom = yUp;
axis_mc = target.createEmptyMovieClip("axis_mc" + depth, depth);
with (axis_mc) {
_x = ox;
_y = oy;
//画坐标:
lineStyle(0, color, 100);
moveTo(xLeft, 0);
lineTo(xRight, 0);
moveTo(0, -yDown);
lineTo(0, -yUp);
//标出原点,x轴,y轴:
createTextField("zero_txt", 100, -10, -1, 10, 10);
zero_txt.textColor = color;
zero_txt.selectable = false;
zero_txt.autoSize = "center";
zero_txt.text = "0";
createTextField("x_txt", 200, xRight, -1, 10, 10);
x_txt.textColor = color;
x_txt.selectable = false;
x_txt.autoSize = "center";
x_txt.html = true;
x_txt.htmlText = "x";
createTextField("y_txt", 300, 4, -yDown - 12, 10, 10);
y_txt.textColor = color;
y_txt.selectable = false;
y_txt.autoSize = "center";
y_txt.html = true;
y_txt.htmlText = "y";
//以下4个for循环分别画出-x轴,+x轴,-y轴,+y轴的刻度:
for (var i = -step; i > xLeft; i -= step) {
moveTo(i, 0);
lineTo(i, -gridLength);
createTextField("xGrid" + 1000 + i, 1000 + i, i - 4, 0, 10, 10);
var _txt = eval("xGrid" + 1000 + i);
_txt.textColor = color;
_txt.selectable = false;
_txt.autoSize = "center";
_txt.text = i / step;
}
for (var i = step; i < xRight; i += step) {
moveTo(i, 0);
lineTo(i, -gridLength);
createTextField("xGrid" + 2000 + i, 2000 + i, i - 4, 0, 10, 10);
var _txt = eval("xGrid" + 2000 + i);
_txt.textColor = color;
_txt.selectable = false;
_txt.autoSize = "center";
_txt.text = i / step;
}
for (var i = step; i < -yUp; i += step) {
moveTo(0, i);
lineTo(gridLength, i);
createTextField("yGrid" + 3000 + i, 3000 + i, -12, i - 9, 10, 10);
var _txt = eval("yGrid" + 3000 + i);
_txt.textColor = color;
_txt.selectable = false;
_txt.autoSize = "right";
_txt.text = -i / step;
}
for (var i = -step; i > -yDown; i -= step) {
moveTo(0, i);
lineTo(gridLength, i);
createTextField("yGrid" + 4000 + i, 4000 + i, -12, i - 9, 10, 10);
var _txt = eval("yGrid" + 4000 + i);
_txt.textColor = color;
_txt.selectable = false;
_txt.autoSize = "right";
_txt.text = -i / step;
}
}
//加上箭头:
arrx = new Arrow(axis_mc, xRight, 0, 14, 8, color, 1);
arry = new Arrow(axis_mc, 0, -yDown, 14, 8, color, 2);
arrx.setDirection("right");
arry.setDirection("up");
}
public function drawFunction(func:Function, thickness:Number, color:Number) {
var depth = axis_mc.getNextHighestDepth();
func_mc = axis_mc.createEmptyMovieClip("func_mc" + depth, depth);
func_mc.lineStyle(thickness, color, 100);
func_mc.moveTo(_left, -func(_left / _step) * _step);
for (var i = _left; i <= _right; i++) {
var fi = -func(i / _step) * _step;
if (fi > _top || fi < _bottom || isNaN(fi)) {
func_mc.moveTo(i, fi);
} else {
func_mc.lineTo(i, fi);
}
}
}
public function drawFunctionSlowly(func:Function, thickness:Number, color:Number, val:Number) {
clearInterval(intervalID);
var _self = this
var depth = axis_mc.getNextHighestDepth();
var func_mc = axis_mc.createEmptyMovieClip("func_mc"+depth, depth);
var i = _left;
var fi = -func(i/_step)*_step;
func_mc.lineStyle(thickness, color, 100);
func_mc.moveTo(i, fi);
intervalID = setInterval(function () {
i++;
fi = -func(i/_self._step)*_self._step;
if (fi>_self._top || fi<_self._bottom || isNaN(fi)) {
func_mc.moveTo(i, fi);
} else {
func_mc.lineTo(i, fi);
}
if (i>=_self._right) {
clearInterval(_self.intervalID);
}
}, val);
}
}