Android中使用Bezier曲线

  1. import android.graphics.Point;  
  2. /** 
  3.  *  
  4.  * @author http://blog.csdn.net/arui319 
  5.  * 
  6.  */  
  7. public class Bezier {  
  8.     private static final float AP = 0.5f;  
  9.     private Point[] bPoints;  
  10.     /** 
  11.      * Creates a new Bezier curve. 
  12.      *  
  13.      * @param points 
  14.      */  
  15.     public Bezier(Point[] points) {  
  16.         int n = points.length;  
  17.         if (n < 3) {  
  18.             // Cannot create bezier with less than 3 points  
  19.             return;  
  20.         }  
  21.         bPoints = new Point[2 * (n - 2)];  
  22.         double paX, paY;  
  23.         double pbX = points[0].x;  
  24.         double pbY = points[0].y;  
  25.         double pcX = points[1].x;  
  26.         double pcY = points[1].y;  
  27.         for (int i = 0; i < n - 2; i++) {  
  28.             paX = pbX;  
  29.             paY = pbY;  
  30.             pbX = pcX;  
  31.             pbY = pcY;  
  32.             pcX = points[i + 2].x;  
  33.             pcY = points[i + 2].y;  
  34.             double abX = pbX - paX;  
  35.             double abY = pbY - paY;  
  36.             double acX = pcX - paX;  
  37.             double acY = pcY - paY;  
  38.             double lac = Math.sqrt(acX * acX + acY * acY);  
  39.             acX = acX / lac;  
  40.             acY = acY / lac;  
  41.             double proj = abX * acX + abY * acY;  
  42.             proj = proj < 0 ? -proj : proj;  
  43.             double apX = proj * acX;  
  44.             double apY = proj * acY;  
  45.             double p1X = pbX - AP * apX;  
  46.             double p1Y = pbY - AP * apY;  
  47.             bPoints[2 * i] = new Point((int) p1X, (int) p1Y);  
  48.             acX = -acX;  
  49.             acY = -acY;  
  50.             double cbX = pbX - pcX;  
  51.             double cbY = pbY - pcY;  
  52.             proj = cbX * acX + cbY * acY;  
  53.             proj = proj < 0 ? -proj : proj;  
  54.             apX = proj * acX;  
  55.             apY = proj * acY;  
  56.             double p2X = pbX - AP * apX;  
  57.             double p2Y = pbY - AP * apY;  
  58.             bPoints[2 * i + 1] = new Point((int) p2X, (int) p2Y);  
  59.         }  
  60.     }  
  61.     /** 
  62.      * Returns the calculated bezier points. 
  63.      *  
  64.      * @return the calculated bezier points 
  65.      */  
  66.     public Point[] getPoints() {  
  67.         return bPoints;  
  68.     }  
  69.     /** 
  70.      * Returns the number of bezier points. 
  71.      *  
  72.      * @return number of bezier points 
  73.      */  
  74.     public int getPointCount() {  
  75.         return bPoints.length;  
  76.     }  
  77.     /** 
  78.      * Returns the bezier points at position i. 
  79.      *  
  80.      * @param i 
  81.      * @return the bezier point at position i 
  82.      */  
  83.     public Point getPoint(int i) {  
  84.         return bPoints[i];  
  85.     }  
  86. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值