孤独的猫

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

processing--路径动画

Processing提供了bezierPoint()函数,它可以在贝塞尔曲线上进行插值运算,求出曲线0~1之间t时刻点的坐标值。

 1 float t;
 2 void setup() {
 3   size(900, 600);     
 4   background(200);
 5   rectMode(CENTER);
 6 }
 7 void draw() {
 8   noFill();
 9   bezier(100, 200, 150, 400, 450, 400, 500, 200);         //设置曲线参数
10   float x = bezierPoint(100, 150, 450, 500, t);            //曲线上点的x坐标
11   float y = bezierPoint(200, 400, 400, 200, t);            //曲线上点的y坐标
12   float tx = bezierTangent(100, 150, 450, 500, t);         //求出切线方向x分量
13   float ty = bezierTangent(200, 400, 400, 200, t);        //求出切线方向y分量
14   float radian = atan2(ty, tx);                        //根据切线方向分量求出角度
15   translate(x, y);                                  //坐标移到曲线上点的位置
16   rotate(radian);                                 //坐标系旋转
17   fill(255, 0, 0);
18   rect(0, 0, 50, 30);
19   if (t<=1) {
20     t +=0.01;
21   }
22 }

效果图:

 

posted on 2024-06-16 11:32  孤独的猫  阅读(90)  评论(0)    收藏  举报