分享

spline样条线算法 | 学步园

 牛人的尾巴 2016-06-23

spline样条线算法

2014年09月05日 ? 综合 ? 共 2636字 ? 字号 ? 评论关闭

在雨松blog上看到一个bezier的class,转过来发现其实不是什么特殊算法,是bezier曲线

using UnityEngine;
[System.Serializable]

public class Bezier : System.Object

{

    public Vector3 p0;

    public Vector3 p1;

    public Vector3 p2;

    public Vector3 p3;

    public float ti = 0f;

    private Vector3 b0 = Vector3.zero;

    private Vector3 b1 = Vector3.zero;

    private Vector3 b2 = Vector3.zero;

    private Vector3 b3 = Vector3.zero;

    private float Ax;

    private float Ay;

    private float Az;

    private float Bx;

    private float By;

    private float Bz;

    private float Cx;

    private float Cy;

    private float Cz;

    // Init function v0 = 1st point, v1 = handle of the 1st point , v2 = handle of the 2nd point, v3 = 2nd point

    // handle1 = v0 + v1

    // handle2 = v3 + v2

    public Bezier( Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3 )

    {

        this.p0 = v0;

        this.p1 = v1;

        this.p2 = v2;

        this.p3 = v3;

    }

    // 0.0 >= t <= 1.0

    public Vector3 GetPointAtTime( float t )

    {

        this.CheckConstant();

        float t2 = t * t;

        float t3 = t * t * t;

        float x = this.Ax * t3 + this.Bx * t2 + this.Cx * t + p0.x;

        float y = this.Ay * t3 + this.By * t2 + this.Cy * t + p0.y;

        float z = this.Az * t3 + this.Bz * t2 + this.Cz * t + p0.z;

        return new Vector3( x, y, z );

    }

    private void SetConstant()

    {

        this.Cx = 3f * ( ( this.p0.x + this.p1.x ) - this.p0.x );

        this.Bx = 3f * ( ( this.p3.x + this.p2.x ) - ( this.p0.x + this.p1.x ) ) - this.Cx;

        this.Ax = this.p3.x - this.p0.x - this.Cx - this.Bx;

        this.Cy = 3f * ( ( this.p0.y + this.p1.y ) - this.p0.y );

        this.By = 3f * ( ( this.p3.y + this.p2.y ) - ( this.p0.y + this.p1.y ) ) - this.Cy;

        this.Ay = this.p3.y - this.p0.y - this.Cy - this.By;

        this.Cz = 3f * ( ( this.p0.z + this.p1.z ) - this.p0.z );

        this.Bz = 3f * ( ( this.p3.z + this.p2.z ) - ( this.p0.z + this.p1.z ) ) - this.Cz;

        this.Az = this.p3.z - this.p0.z - this.Cz - this.Bz;

    }

    // Check if p0, p1, p2 or p3 have changed

    private void CheckConstant()

    {

        if( this.p0 != this.b0 || this.p1 != this.b1 || this.p2 != this.b2 || this.p3 != this.b3 )

        {

            this.SetConstant();

            this.b0 = this.p0;

            this.b1 = this.p1;

            this.b2 = this.p2;

            this.b3 = this.p3;

        }

    }

}

    private Vector3 GetPoint(float t)
    {
        int numSections = getPoints.Length - 3;
        int tSec = (int)Math.Floor(t * numSections);
        int currPt = numSections - 1;
        if (currPt > tSec)
        {
            currPt = tSec;
        }
        float u = t * numSections - currPt;

        Vector3 a = getPoints[currPt];
        Vector3 b = getPoints[currPt + 1];
        Vector3 c = getPoints[currPt + 2];
        Vector3 d = getPoints[currPt + 3];

        return .5f * (
                       (-a + 3f * b - 3f * c + d) * (u * u * u)
                       + (2f * a - 5f * b + 4f * c - d) * (u * u)
                       + (-a + c) * u
                       + 2f * b
                   );

    }


以上的代码可以在0到1之间,沿路径平均细分出曲线

偶然看到的,然后google了一下

发现源头是unity的官方开发人写出来的

然后突然发现itween,hotween,一些赛车塔防的路径都是用这个算法的



当然,我还是没太理解这个算法



补充:

设 已知 的四点为 (x0,y0),(x1,y1),(x2,y2),(x3,y3),其中 0 和 3 是端点,
1,2是控制点,则 该曲线上的所有点表示为:

x = (1-t)^3 *x0 + 3*t*(1-t)^2 *x1 + 3*t^2*(1-t) *x2 + t^3 *x3
y = (1-t)^3 *y0 + 3*t*(1-t)^2 *y1 + 3*t^2*(1-t) *y2 + t^3 *y3
其中 0 <= t <= 1.


应该是beizier曲线的算法



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多