c# - 平滑连接圆心

标签 c# math geometry

我正在尝试沿着中心绘制一条穿过 pipe 内部的路径。我必须处理的数据是在管道中每一圈的开始和结束处描述管道的圆的中心点。

绘制通过管道直线段的路径很简单,但我不确定如何接近弯道。两个圆之间的任何转弯都应具有恒定半径。所以我可以访问这个圆上的两个点,以及那个点的圆线的方向。

有谁知道我将如何计算出圆的其余部分?

编辑:

附上逼真的管道草图。

It's like I'm in the pipe!

所以假装这条曲线并不摇摆不定,蓝线表示圆圈,红色表示中心点,绿色表示通过中心的路径。

最佳答案

  1. 澄清

    pipe 各处都有相同的圆直径,因此不会因弯曲而变形 !!!输入是 2 个端点(管中心)P0,P1 和 2 个向量(法线/管方向)N0,N1

    pipe bend

  2. 解决方案

    使用Interpolation cubic作为例子

    p(t)=a0+a1*t+a2*t*t+a3*t*t*t
    t=<0,1.0>
    

    因此,为已知数据编写方程式,为您需要的每个轴求解a0,a1,a2,a3 系数(2D:x,y),然后您就可以获取中心点及其在沿弯曲侧任意点的法线,这是您需要的。

    现在是一些通用方程:

    p(t)=a0+a1*t+     a2*t*t+     a3*t*t*t // circle center position
    n(t)=   a1   +2.0*a2*t   +3.0*a3*t*t   // circle orientation
    
    • p,n,a0,a1,a2,a3 是向量!!!
    • t 是标量

    现在添加已知数据

    I. t=0 -> p(0)=P0
    P0=a0
    a0=P0
    
    II. t=0 -> n(0)=N0
    N0=a1
    a1=N0
    
    III. t=1 -> p(1)=P1
    P1=a0+a1+a2+a3
    P1=P0+N0+a2+a3
    a2=P1-P0-N0-a3
    
    IV. t=1 -> n(1)=N1
    N1=a1+2.0*a2+3.0*a3
    N1=N0+2.0*(P1-P0-N0-a3)+3.0*a3
    a3=N1+N0-2.0*(P1-P0)
    
    III.
    a2=P1-P0-N0-(N1+N0-2.0*(P1-P0))
    a2=P1-P0-N0-N1-N0+2.0*(P1-P0)
    a2=P1-P0-N1+2.0*(P1-P0-N0)
    a2=3.0*(P1-P0)-N1-2.0*N0
    

    所以如果我没有犯任何愚蠢的错误,那么系数是:

    a0=P0
    a1=N0
    a2=3.0*(P1-P0)-N1-2.0*N0
    a3=N1+N0-2.0*(P1-P0)
    

    现在只需将泛型方程编码为输入参数为 t 的函数,并输出 p(t)n(t) 和/或渲染圆或管段并在 for 循环中调用它,例如:

    for (t=0.0;t<=1.0;t+=0.1) f(t);
    

[edit1] C++ 实现

//---------------------------------------------------------------------------
void glCircle3D(double *pos,double *nor,double r,bool _fill)
    {
    int i,n=36;
    double a,da=divide(pi2,n),p[3],dp[3],x[3],y[3];
         if (fabs(nor[0]-nor[1])>1e-6) vector_ld(x,nor[1],nor[0],nor[2]);
    else if (fabs(nor[0]-nor[2])>1e-6) vector_ld(x,nor[2],nor[1],nor[0]);
    else if (fabs(nor[1]-nor[2])>1e-6) vector_ld(x,nor[0],nor[2],nor[1]);
    else                       vector_ld(x,1.0,0.0,0.0);
    vector_mul(x,x,nor);
    vector_mul(y,x,nor);
    vector_len(x,x,r);
    vector_len(y,y,r);
    if (_fill)
        {
        glBegin(GL_TRIANGLE_FAN);
        glVertex3dv(pos);
        }
    else glBegin(GL_LINE_STRIP);
    for (a=0.0,i=0;i<=n;i++,a+=da)
        {
        vector_mul(dp,x,cos(a)); vector_add(p,pos,dp);
        vector_mul(dp,y,sin(a)); vector_add(p,p  ,dp);
        glVertex3dv(p);
        }
    glEnd();
    }
//---------------------------------------------------------------------------
void tube(double *P0,double *N0,double *P1,double *N1,double R)
    {
    int i;
    double a0[3],a1[3],a2[3],a3[3],p[3],n[3],t,tt,ttt;
    // compute coefficients
    for (i=0;i<3;i++)
        {
        a0[i]=P0[i];
        a1[i]=N0[i];
        a2[i]=(3.0*(P1[i]-P0[i]))-N1[i]-(2.0*N0[i]);
        a3[i]=N1[i]+N0[i]-2.0*(P1[i]-P0[i]);
        }
    // step through curve from t=0 to t=1
    for (t=0.0;t<=1.0;t+=0.02)
        {
        tt=t*t;
        ttt=tt*t;
        // compute circle position and orientation
        for (i=0;i<3;i++)
            {
            p[i]=a0[i]+(a1[i]*t)+(a2[i]*tt)+(a3[i]*ttt);
            n[i]=a1[i]+(2.0*a2[i]*t)+(3.0*a3[i]*tt);
            }
        // render it
        glCircle3D(p,n,R,false);
        }
    }
//---------------------------------------------------------------------------
void test()
    {
    // tube parameters
    double P0[3]={-1.0, 0.0, 0.0},N0[3]={+1.0,-1.0, 0.0},p[3];
    double P1[3]={+1.0,+1.0, 0.0},N1[3]={ 0.0,+1.0, 0.0};
    // just normalize normals to size 3.1415...
    vector_len(N0,N0,M_PI);
    vector_len(N1,N1,M_PI);
    // draw normals to visula confirmation of tube direction
    glBegin(GL_LINES);
    glColor3f(0.0,0.0,1.0); vector_add(p,P0,N0); glVertex3dv(P0); glVertex3dv(p);
    glColor3f(0.0,0.0,1.0); vector_add(p,P1,N1); glVertex3dv(P1); glVertex3dv(p);
    glEnd();
    // render tube
    glColor3f(1.0,1.0,1.0); tube(P0,N0,P1,N1,0.2);
    }
//---------------------------------------------------------------------------

从视觉上看,当法线的大小为 M_PI (3.1415...) 时,上面的代码看起来是这样的:

pipe C++

我的代码使用我的向量库,所以你只需要编写如下函数:

vector_ld(a,x,y,z); //a[]={ x,y,z }
vector_mul(a,b,c);  //a[]=b[] x c[]
vector_mul(a,b,c);  //a[]=b[] * c
vector_add(a,b,c);  //a[]=b[] + c[]
vector_sub(a,b,c);  //a[]=b[] - c[]
vector_len(a,b,c);  //a[]=b[]*  c / |b[]|

这很容易(希望我没有忘记复制一些东西...)...

关于c# - 平滑连接圆心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25178181/

相关文章:

python - 如何测试Python中一条线是否与任意数量的其他未知线相交?

c# - 以 "TryParse"的方式反序列化 json

c# - 如果不为空,检查查询字符串参数值的最优雅方法?

c# - 如何解决另一个进程正在使用的文件?

math - glm::normalize 有什么作用?

javascript - 在 Google Maps API v3 中使用单独的删除按钮绘制圆形/多边形

java - 查找 : '' The specified procedure could not be found 时出错

python - 将 float 量化为任意数量的容器的一般方法?

当在循环内使用计数器时 C++ 程序崩溃

math - 在 switch case 语句中检测 2 个变量值的 4 个排列