Android中使用Bezier曲線

來源:互聯網
上載者:User

import android.graphics.Point;<br />/**<br /> *<br /> * @author http://blog.csdn.net/arui319<br /> *<br /> */<br />public class Bezier {<br />private static final float AP = 0.5f;<br />private Point[] bPoints;<br />/**<br /> * Creates a new Bezier curve.<br /> *<br /> * @param points<br /> */<br />public Bezier(Point[] points) {<br />int n = points.length;<br />if (n < 3) {<br />// Cannot create bezier with less than 3 points<br />return;<br />}<br />bPoints = new Point[2 * (n - 2)];<br />double paX, paY;<br />double pbX = points[0].x;<br />double pbY = points[0].y;<br />double pcX = points[1].x;<br />double pcY = points[1].y;<br />for (int i = 0; i < n - 2; i++) {<br />paX = pbX;<br />paY = pbY;<br />pbX = pcX;<br />pbY = pcY;<br />pcX = points[i + 2].x;<br />pcY = points[i + 2].y;<br />double abX = pbX - paX;<br />double abY = pbY - paY;<br />double acX = pcX - paX;<br />double acY = pcY - paY;<br />double lac = Math.sqrt(acX * acX + acY * acY);<br />acX = acX / lac;<br />acY = acY / lac;<br />double proj = abX * acX + abY * acY;<br />proj = proj < 0 ? -proj : proj;<br />double apX = proj * acX;<br />double apY = proj * acY;<br />double p1X = pbX - AP * apX;<br />double p1Y = pbY - AP * apY;<br />bPoints[2 * i] = new Point((int) p1X, (int) p1Y);<br />acX = -acX;<br />acY = -acY;<br />double cbX = pbX - pcX;<br />double cbY = pbY - pcY;<br />proj = cbX * acX + cbY * acY;<br />proj = proj < 0 ? -proj : proj;<br />apX = proj * acX;<br />apY = proj * acY;<br />double p2X = pbX - AP * apX;<br />double p2Y = pbY - AP * apY;<br />bPoints[2 * i + 1] = new Point((int) p2X, (int) p2Y);<br />}<br />}<br />/**<br /> * Returns the calculated bezier points.<br /> *<br /> * @return the calculated bezier points<br /> */<br />public Point[] getPoints() {<br />return bPoints;<br />}<br />/**<br /> * Returns the number of bezier points.<br /> *<br /> * @return number of bezier points<br /> */<br />public int getPointCount() {<br />return bPoints.length;<br />}<br />/**<br /> * Returns the bezier points at position i.<br /> *<br /> * @param i<br /> * @return the bezier point at position i<br /> */<br />public Point getPoint(int i) {<br />return bPoints[i];<br />}<br />}<br /> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.