双圆弧插值算法(三,代码实现)
交互式演示 这是一个用HTML5编写的交互式演示。要移动控制点,请单击并拖动它们。若要移动切线,请单击并拖动控制点外的区域。默认情况下,曲线保持d1和d2相等,但也可以在下面指定自定义d1值。
代码
到目前为止,我们只讨论了二维情况。让我们编写一些C++代码来解决三维的情况。它非常相似,除了每个弧可以在不同的平面上对齐。这将在查找旋转方向和平面法线时创建一些调整。经过几次交叉积后,一切都成功了。 这些代码示例是在以下许可下发布的。
/******************************************************************************
Copyright © 2014 Ryan Juckett
http://www.ryanjuckett.com/
This software is provided ‘as-is’, without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
-
The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required. -
Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software. -
This notice may not be removed or altered from any source
distribution.
/ Here’s our vector type. It’s about as basic as vector types come.//
//******************************************************************************
struct tVec3
{
float m_x;
float m_y;
float m_z;
}; Now, let’s define some math functions to help with common operations (mostly linear algebra).//******************************************************************************
// Compute the dot product of two vectors.
//******************************************************************************
float Vec_DotProduct(const tVec3 & lhs, const tVec3 & rhs)
{
return lhs.m_xrhs.m_x + lhs.m_yrhs.m_y + lhs.m_z*rhs.m_z;
}
//******************************************************************************
// Compute the cross product of two vectors.
//******************************************************************************
void Vec_CrossProduct(tVec3 * pResult, const tVec3 & lhs, const tVec3 & rhs)
{
float x = lhs.m_yrhs.m_z - lhs.m_zrhs.m_y;
float y = lhs.m_zrhs.m_x - lhs.m_xrhs.m_z;
float z = lhs.m_xrhs.m_y - lhs.m_yrhs.m_x;
pResult->m_x = x;
pResult->m_y = y;
pResult->m_z = z;
}
//******************************************************************************
// Compute the sum of two vectors.
//******************************************************************************
void Vec_Add(tVec3 * pResult, const tVec3 & lhs, const tVec3 & rhs)
{
pResult->m_x = lhs.m_x + rhs.m_x;
pResult->m_y = lhs.m_y