00001 #region --- License --- 00002 /* Licensed under the MIT/X11 license. 00003 * Copyright (c) 2006-2008 the OpenTK Team. 00004 * This notice may not be removed from any source distribution. 00005 * See license.txt for licensing detailed licensing details. 00006 * 00007 * Contributions by Georg W�chter. 00008 */ 00009 #endregion 00010 00011 using System; 00012 using System.Collections.Generic; 00013 using System.Text; 00014 00015 namespace OpenTK 00016 { 00020 [Serializable] 00021 public struct BezierCurveCubic 00022 { 00023 #region Fields 00024 00028 public Vector2 StartAnchor; 00029 00033 public Vector2 EndAnchor; 00034 00038 public Vector2 FirstControlPoint; 00039 00043 public Vector2 SecondControlPoint; 00044 00052 public float Parallel; 00053 00054 #endregion 00055 00056 #region Constructors 00057 00065 public BezierCurveCubic(Vector2 startAnchor, Vector2 endAnchor, Vector2 firstControlPoint, Vector2 secondControlPoint) 00066 { 00067 this.StartAnchor = startAnchor; 00068 this.EndAnchor = endAnchor; 00069 this.FirstControlPoint = firstControlPoint; 00070 this.SecondControlPoint = secondControlPoint; 00071 this.Parallel = 0.0f; 00072 } 00073 00082 public BezierCurveCubic(float parallel, Vector2 startAnchor, Vector2 endAnchor, Vector2 firstControlPoint, Vector2 secondControlPoint) 00083 { 00084 this.Parallel = parallel; 00085 this.StartAnchor = startAnchor; 00086 this.EndAnchor = endAnchor; 00087 this.FirstControlPoint = firstControlPoint; 00088 this.SecondControlPoint = secondControlPoint; 00089 } 00090 00091 #endregion 00092 00093 #region Functions 00094 00100 public Vector2 CalculatePoint(float t) 00101 { 00102 Vector2 r = new Vector2(); 00103 float c = 1.0f - t; 00104 00105 r.X = (StartAnchor.X * c * c * c) + (FirstControlPoint.X * 3 * t * c * c) + (SecondControlPoint.X * 3 * t * t * c) 00106 + EndAnchor.X * t * t * t; 00107 r.Y = (StartAnchor.Y * c * c * c) + (FirstControlPoint.Y * 3 * t * c * c) + (SecondControlPoint.Y * 3 * t * t * c) 00108 + EndAnchor.Y * t * t * t; 00109 00110 if (Parallel == 0.0f) 00111 return r; 00112 00113 Vector2 perpendicular = new Vector2(); 00114 00115 if (t == 0.0f) 00116 perpendicular = FirstControlPoint - StartAnchor; 00117 else 00118 perpendicular = r - CalculatePointOfDerivative(t); 00119 00120 return r + Vector2.Normalize(perpendicular).PerpendicularRight * Parallel; 00121 } 00122 00128 private Vector2 CalculatePointOfDerivative(float t) 00129 { 00130 Vector2 r = new Vector2(); 00131 float c = 1.0f - t; 00132 00133 r.X = (c * c * StartAnchor.X) + (2 * t * c * FirstControlPoint.X) + (t * t * SecondControlPoint.X); 00134 r.Y = (c * c * StartAnchor.Y) + (2 * t * c * FirstControlPoint.Y) + (t * t * SecondControlPoint.Y); 00135 00136 return r; 00137 } 00138 00146 public float CalculateLength(float precision) 00147 { 00148 float length = 0.0f; 00149 Vector2 old = CalculatePoint(0.0f); 00150 00151 for (float i = precision; i < (1.0f + precision); i += precision) 00152 { 00153 Vector2 n = CalculatePoint(i); 00154 length += (n - old).Length; 00155 old = n; 00156 } 00157 00158 return length; 00159 } 00160 00161 #endregion 00162 } 00163 }
1.6.1