예제 #1
0
파일: CubicSpline.cs 프로젝트: bertt/tango
		/// <summary>
		/// 求曲线上与给定点最近的点
		/// </summary>
		public float GetClosestLocation(Vector3 point, int segments)
		{
			Vector3 last = _f0;
			Vector3 current;
			float closest01;
			float sqrMagnitude;
			float bestSqrMagnitude = float.MaxValue;
			float bestT = 0f;

			for (int i = 1; i <= segments; i++)
			{
				current = GetPoint((float)i / segments);
				closest01 = Kit.ClosestPoint01(last, current, point);
				sqrMagnitude = (last + (current - last) * closest01 - point).sqrMagnitude;

				if (sqrMagnitude < bestSqrMagnitude)
				{
					bestSqrMagnitude = sqrMagnitude;
					bestT = (i - 1f + closest01) / segments;
				}

				last = current;
			}

			return bestT;
		}