Flash/Flex学习笔记(20):贝塞尔曲线
贝塞尔曲线的身影几乎在所有绘图软件中都有出现,下面的代码演示了如何用AS3.0画一段简单的贝塞尔曲线(没有使用Document文档类,想测试的朋友,直接把下面的代码复制贴到第一帧即可)
01
import
fl.controls.Label;
02
03
var
x1:
uint
=
80
;
04
var
y1:
uint
=
200
;
05
06
var
x2:
uint
=
450
;
07
var
y2:
uint
=
200
;
08
09
var
lbl1:Label =
new
Label();
10
var
lbl2:Label =
new
Label();
11
var
lbl3:Label =
new
Label();
12
13
lbl1.text=
"x1,y1"
;
14
lbl2.text=
"x2,y2"
;
15
lbl3.text=
"x3,y3"
;
16
addChild(lbl1);
17
addChild(lbl2);
18
addChild(lbl3);
19
20
lbl1.move(x1-
30
,y1+
5
);
21
lbl2.move(x2,y2+
5
);
22
23
stage.addEventListener(MouseEvent.MOUSE_MOVE,MouseMoveHandler);
24
25
function
MouseMoveHandler(e:MouseEvent):
void
{
26
drawCurve(mouseX,mouseY);
27
}
28
29
function
drawCurve(uX:
uint
,uY:
uint
):
void
{
30
graphics.clear();
//清除上次画的内容
31
32
graphics.lineStyle(
1
,
0xff0000
,
1
);
//设置线条为红色
33
34
graphics.drawCircle(x1,y1,
5
);
//在x1,y1(左端点)处画一个圈做标记
35
graphics.drawCircle(x2,y2,
5
);
//在x2,y2(右端点)处理一个圈做标记
36
37
var
x3:
uint
=uX;
38
var
y3:
uint
=uY;
39
lbl3.move(x3+
15
,y3);
40
41
graphics.drawCircle(x3,y3,
5
);
//在目标点,画一个圈
42
43
graphics.lineStyle(
1
,
0
,
0.1
);
//设置线条为黑色,且透明度为0.1
44
45
graphics.moveTo(x1,y1);
46
graphics.lineTo(x3,y3);
//画一根从左端点到目标点的线
47
48
graphics.moveTo(x2,y2);
49
graphics.lineTo(x3,y3);
//画一根从右端点到目标点的线
50
51
graphics.moveTo(x1,y1);
52
graphics.lineTo(x2,y2);
//画一根从左端点到右端点的线
53
54
graphics.lineStyle(
1
,
0xff0000
,
1
);
//设置线条为红色
55
graphics.moveTo(x1,y1);
56
graphics.curveTo(x3,y3,x2,y2);
//画一根从左端点出发,经过目标点,终点为右端点的贝赛尔曲线
57
}
58
59
drawCurve(
260
,
50
);
//刚显示时,先用该初始位置画线
一段曲线通常包含三个点:起点(x1,y1),控制点(x3,y3),终点(x2,y2);也许大家也看出来了:该曲线最终并不经过鼠标所在的点(x3,y3),在y轴方向上,曲线最大高度只有鼠标相对高度的一半,如果想真正的经过鼠标点,还要做一下修正(即要把控制点人为抬高或降低一些):
修正公式为:新坐标 = 目标点坐标 * 2 - (起点坐标+终点坐标)/2
即把刚才代码的第56行:
1
graphics.curveTo(x3,y3,x2,y2);
改为:
1
graphics.curveTo(
2
*x3-(x1+x2)/
2
,
2
*y3-(y1+y2)/
2
,x2,y2);
下面再来看下更多点的情况,假如随便给定一些点,要求根据这些点,画一段“平滑”的曲线,最容易想到的思路就是:先从第1个点,画到第3点(第2点为控制点),画出第一段,然后再以第3个点为开始,画到第5点(第4点为控制点)...类推直到全部画完
01
var
numPoints:
uint
=
5
;
02
03
const
X0 =
50
;
04
const
Y0 =
50
;
05
const
X_STEP =
70
;
06
const
Y_STEP =
30
;
07
08
//先对点初始化
09
var
points:
Array
=
new
Array
();
10
for
(
var
i:
int
=
0
; i < numPoints; i++) {
11
points[i] =
new
Object
();
12
points[i].x = X0 + i * X_STEP;
13
var
tY:
int
= (i%
2
)==
0
? Y_STEP : (-
1
*Y_STEP);
14
//trace(tY);
15
points[i].y = Y0 + tY;
16
graphics.lineStyle(
1
,
0xff0000
,
1
);
17
graphics.drawCircle(points[i].x,points[i].y,(i+
2
)*
2
);
//为了更直观,把这几个点都圈标出来
18
}
19
20
graphics.lineStyle(
1
);
21
22
//先将画笔移到第一个点
23
graphics.moveTo(points[
0
].x, points[
0
].y);
24
25
//由于划一条曲线起码要有三个点(起点,终点,控制点),所以循环变量每次+2
26
for
(i =
1
; i < numPoints; i +=
2
) {
27
graphics.curveTo(points[i].x, points[i].y, points[i +
1
].x, points[i +
1
].y);
28
}
但是这样有一个问题,各段曲线之间的连接并不平滑,因为这完全只是把各段曲线简单的强型拼在一起而已,为了实现平滑,我们还得动点脑筋
01
var
numPoints:
uint
=
7
;
02
03
const
X0 =
50
;
04
const
Y0 =
100
;
05
const
X_STEP =
70
;
06
const
Y_STEP =
90
;
07
08
//先对点初始化
09
var
points:
Array
=
new
Array
();
10
for
(
var
i:
int
=
0
; i < numPoints; i++) {
11
points[i] =
new
Object
();
12
points[i].x = X0 + i * X_STEP;
13
var
tY:
int
= (i%
2
)==
0
? Y_STEP : (-
1
*Y_STEP);
14
//trace(tY);
15
points[i].y = Y0 + tY;
16
graphics.lineStyle(
2
,
0xff0000
,
1
);
17
graphics.drawCircle(points[i].x,points[i].y,
3
);
//为了更直观,把这几个点都圈标出来
18
}
19
20
//为了看得更清楚,把新加的点,用蓝色标出来
21
for
(i =
1
; i < numPoints -
2
; i ++) {
22
var
_xc:
Number
= (points[i].x + points[i +
1
].x) /
2
;
23
var
_yc:
Number
= (points[i].y + points[i +
1
].y) /
2
;
24
graphics.lineStyle(
3
,
0x0000ff
,
1
);
25
graphics.drawCircle(_xc,_yc,
3
);
26
}
27
28
graphics.lineStyle(
1
);
29
30
//先把画笔移到第一点
31
graphics.moveTo(points[
0
].x, points[
0
].y);
32
33
//去掉首尾二点后,根据剩下的点和新加的点画曲线
34
for
(i =
1
; i < numPoints -
2
; i ++) {
35
var
xc:
Number
= (points[i].x + points[i +
1
].x) /
2
;
36
var
yc:
Number
= (points[i].y + points[i +
1
].y) /
2
;
37
graphics.curveTo(points[i].x, points[i].y, xc, yc);
38
39
}
40
41
//处理最后一点
42
graphics.curveTo(points[i].x, points[i].y, points[i+
1
].x, points[i+
1
].y);
ok,这样就平滑了,来看下原理:
先把第一点跟最后一点忽略掉(why? 因为刚才的问题仅出在各段曲线的连接点上,而第一段的开头与最后一段曲线的结尾本身没有不平滑的问题,所以我们修正时,不需要管二端),然后在各点之间插入一个新的辅助点(即上图中蓝色的点),点的位置其实可以随便定义,本例中正好取了中间位置,然后把这些新加的蓝色点以原来的头尾二点整体看作起始点与结束点,其它的点看做控制点(即去掉头尾后的红点),这样就行了 :)
类似的,我们还可以再增加三个辅助点,以达到闭合封闭曲线的效果:
01
var
numPoints:
uint
=
7
;
02
03
const
X0 =
50
;
04
const
Y0 =
100
;
05
const
X_STEP =
70
;
06
const
Y_STEP =
90
;
07
08
//先对点初始化
09
var
points:
Array
=
new
Array
();
10
for
(
var
i:
int
=
0
; i < numPoints; i++) {
11
points[i] =
new
Object
();
12
points[i].x = X0 + i * X_STEP;
13
var
tY:
int
= (i%
2
)==
0
? Y_STEP : (-
1
*Y_STEP);
14
//trace(tY);
15
points[i].y = Y0 + tY;
16
graphics.lineStyle(
2
,
0xff0000
,
1
);
17
graphics.drawCircle(points[i].x,points[i].y,
3
);
//为了更直观,把这几个点都圈标出来
18
}
19
20
var
_X_BEGIN = (points[
0
].x + points[
1
].x) /
2
;
21
var
_Y_BEGIN = (points[
0
].y + points[
1
].y) /
2
;
22
graphics.lineStyle(
3
,
0x00ff00
,
1
);
23
graphics.drawCircle(_X_BEGIN,_Y_BEGIN,
3
);
24
25
26
//为了看得更清楚,把新加的点,用蓝色标出来
27
for
(i =
1
; i < numPoints -
2
; i ++) {
28
var
_xc:
Number
= (points[i].x + points[i +
1
].x) /
2
;
29
var
_yc:
Number
= (points[i].y + points[i +
1
].y) /
2
;
30
graphics.lineStyle(
3
,
0x0000ff
,
1
);
31
graphics.drawCircle(_xc,_yc,
3
);
32
}
33
34
graphics.lineStyle(
1
);
35
36
//先把画笔移到第一个辅助点
37
graphics.moveTo(_X_BEGIN, _Y_BEGIN);
38
39
//去掉首尾二点后,根据剩下的点和新加的点画曲线
40
for
(i =
1
; i < numPoints -
2
; i ++) {
41
var
xc:
Number
= (points[i].x + points[i +
1
].x) /
2
;
42
var
yc:
Number
= (points[i].y + points[i +
1
].y) /
2
;
43
graphics.curveTo(points[i].x, points[i].y, xc, yc);
44
}
45
46
47
48
var
_len:
uint
= points.length;
49
50
//倒数第二个绿点
51
var
_X_END_1 = (points[_len-
2
].x + points[_len-
1
].x)/
2
;
52
var
_Y_END_1 = (points[_len-
2
].y + points[_len-
1
].y)/
2
;
53
54
//最后一个绿点
55
var
_X_END_2 = (points[_len-
1
].x + points[
0
].x)/
2
;
56
var
_Y_END_2 = (points[_len-
1
].y + points[
0
].y)/
2
;
57
58
//最后一个蓝点为起点,到_X_END_1,_Y_END_1,倒数第二个红点为控制点
59
graphics.curveTo(points[i].x, points[i].y, _X_END_1,_Y_END_1);
60
61
graphics.curveTo(points[_len-
1
].x, points[_len-
1
].y, _X_END_2,_Y_END_2);
62
63
graphics.curveTo(points[
0
].x, points[
0
].y, _X_BEGIN,_Y_BEGIN);
64
graphics.lineStyle(
3
,
0x00ff00
,
1
);
65
graphics.drawCircle(_X_END_1,_Y_END_1,
3
);
66
graphics.drawCircle(_X_END_2,_Y_END_2,
3
);
最后把上面这段代码抽象封装一下:
01
package
{
02
import
flash.display.Sprite;
03
import
flash.events.MouseEvent;
04
import
flash.events.Event;
05
import
flash.display.Graphics;
06
import
flash.ui.MouseCursor;
07
import
flash.ui.Mouse;
08
09
public
class
ShowCurve
extends
Sprite {
10
11
private
var
isStop:
Boolean
;
12
13
public
function
ShowCurve():
void
{
14
init();
15
}
16
17
private
function
init() {
18
stage.addEventListener(MouseEvent.MOUSE_DOWN,MouseDownHandler);
19
addEventListener(Event.ENTER_FRAME,EnterFrameHandler);
20
isStop =
false
;
21
Mouse.cursor = MouseCursor.BUTTON;
22
}
23
24
private
function
MouseDownHandler(e:MouseEvent) {
25
if
(!isStop){
26
removeEventListener(Event.ENTER_FRAME,EnterFrameHandler);
27
isStop =
true
;
28
}
29
else
{
30
addEventListener(Event.ENTER_FRAME,EnterFrameHandler);
31
isStop =
false
;
32
}
33
}
34
35
private
function
EnterFrameHandler(e:Event):
void
{
36
graphics.clear();
37
var
numPoints:
uint
=
9
;
38
//先对点初始化
39
var
points:
Array
=
new
Array
();
40
for
(
var
i:
int
=
0
; i < numPoints; i++) {
41
points[i] =
new
Object
();
42
points[i].x=stage.stageWidth*Math.random();
43
points[i].y=stage.stageHeight*Math.random();
44
graphics.lineStyle(
2
,
0xff0000
,
1
);
45
graphics.drawCircle(points[i].x,points[i].y,
1
);
//为了更直观,把这几个点都圈标出来
46
}
47
var
_X_BEGIN = (points[
0
].x + points[
1
].x) /
2
;
48
var
_Y_BEGIN = (points[
0
].y + points[
1
].y) /
2
;
49
graphics.lineStyle(
1
,
0x00ff00
,
1
);
50
graphics.drawCircle(_X_BEGIN,_Y_BEGIN,
1
);
51
//为了看得更清楚,把新加的点,用蓝色标出来
52
for
(i =
1
; i < numPoints -
2
; i ++) {
53
var
_xc:
Number
= (points[i].x + points[i +
1
].x) /
2
;
54
var
_yc:
Number
= (points[i].y + points[i +
1
].y) /
2
;
55
graphics.lineStyle(
3
,
0x0000ff
,
1
);
56
graphics.drawCircle(_xc,_yc,
1
);
57
}
58
graphics.lineStyle(
1
);
59
//先把画笔移到第一个辅助点
60
graphics.moveTo(_X_BEGIN, _Y_BEGIN);
61
//去掉首尾二点后,根据剩下的点和新加的点画曲线
62
for
(i =
1
; i < numPoints -
2
; i ++) {
63
var
xc:
Number
= (points[i].x + points[i +
1
].x) /
2
;
64
var
yc:
Number
= (points[i].y + points[i +
1
].y) /
2
;
65
graphics.curveTo(points[i].x, points[i].y, xc, yc);
66
}
67
var
_len:
uint
=points.length;
68
//倒数第二个绿点
69
var
_X_END_1 = (points[_len-
2
].x + points[_len-
1
].x)/
2
;
70
var
_Y_END_1 = (points[_len-
2
].y + points[_len-
1
].y)/
2
;
71
//最后一个绿点
72
var
_X_END_2 = (points[_len-
1
].x + points[
0
].x)/
2
;
73
var
_Y_END_2 = (points[_len-
1
].y + points[
0
].y)/
2
;
74
//最后一个蓝点为起点,到_X_END_1,_Y_END_1,倒数第二个红点为控制点
75
graphics.curveTo(points[i].x, points[i].y, _X_END_1,_Y_END_1);
76
graphics.curveTo(points[_len-
1
].x, points[_len-
1
].y, _X_END_2,_Y_END_2);
77
graphics.curveTo(points[
0
].x, points[
0
].y, _X_BEGIN,_Y_BEGIN);
78
graphics.lineStyle(
1
,
0x00ff00
,
1
);
79
graphics.drawCircle(_X_END_1,_Y_END_1,
1
);
80
graphics.drawCircle(_X_END_2,_Y_END_2,
1
);
81
}
82
}
83
}
注:本文中所演示的只是二次曲线,对于三次曲线或高阶曲线,就更复杂了。详情见维基百科: http://zh.wikipedia.org/wiki/%E8%B2%9D%E8%8C%B2%E6%9B%B2%E7%B7%9A