c ++中的B样条曲线

时间:2017-01-02 08:02:37

标签: c++ c++11 visual-c++

有人可以帮我解决B样条曲线错误吗?

我想在c ++中绘制B样条曲线,但即使所有坐标都是正数,段的坐标也是负的。

这是B样条曲线代码。

double t = 3.f;
do{

    if ((3 < t) && (t <= 4)) {
    BSplineCurve(ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, DrawCurve, t);
    Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
    }
    else if ((4 < t) && (t <= 5)) {
    BSplineCurve(ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, DrawCurve, t);
    Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
    }
    else if ((5 < t) && (t <= 6)) {
    BSplineCurve(ControlPoint3, ControlPoint4, ControlPoint5, ControlPoint6, DrawCurve, t);
    Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
    }
    t += 0.001;
} while(t < 6.001);

这是绘图代码。

.sk-folding-cube {
  margin: 20px auto;
  width: 40px;
  height: 40px;
  position: relative;
  -webkit-transform: rotateZ(45deg);
  transform: rotateZ(45deg);
}
.sk-folding-cube .sk-cube {
  float: left;
  width: 50%;
  height: 50%;
  position: relative;
  -webkit-transform: scale(1.1);
  -ms-transform: scale(1.1);
  transform: scale(1.1);
}
.sk-folding-cube .sk-cube:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #333;
  -webkit-animation: sk-foldCubeAngle 2.4s 1 linear both;
  animation: sk-foldCubeAngle 2.4s 1 linear both;
  -webkit-transform-origin: 100% 100%;
  -ms-transform-origin: 100% 100%;
  transform-origin: 100% 100%;
}
.sk-folding-cube .sk-cube2 {
  -webkit-transform: scale(1.1) rotateZ(90deg);
  transform: scale(1.1) rotateZ(90deg);
}
.sk-folding-cube .sk-cube3 {
  -webkit-transform: scale(1.1) rotateZ(180deg);
  transform: scale(1.1) rotateZ(180deg);
}
.sk-folding-cube .sk-cube4 {
  -webkit-transform: scale(1.1) rotateZ(270deg);
  transform: scale(1.1) rotateZ(270deg);
}
.sk-folding-cube .sk-cube5 {
  -webkit-transform: scale(1.1) rotateZ(360deg);
  transform: scale(1.1) rotateZ(360deg);
}
.sk-folding-cube .sk-cube5 {
  -webkit-transform: scale(1.1) rotateZ(360deg);
  transform: scale(1.1) rotateZ(360deg);
}
.sk-folding-cube .sk-cube2:before {
  -webkit-animation-delay: 0.3s;
  animation-delay: 0.3s;
}
.sk-folding-cube .sk-cube3:before {
  -webkit-animation-delay: 0.6s;
  animation-delay: 0.6s;
}
.sk-folding-cube .sk-cube4:before {
  -webkit-animation-delay: 0.9s;
  animation-delay: 0.9s;
}
.sk-folding-cube .sk-cube5:before {
  -webkit-animation-delay: 1.2s;
  animation-delay: 1.2s;
}
.sk-folding-cube .sk-cube6:before {
  -webkit-animation-delay: 1.5s;
  animation-delay: 1.5s;
}
@-webkit-keyframes sk-foldCubeAngle {
  0%, 10% {
    -webkit-transform: perspective(140px) rotateX(-180deg);
    transform: perspective(140px) rotateX(-180deg);
    opacity: 0;
  }
  25%,
  75% {
    -webkit-transform: perspective(140px) rotateX(0deg);
    transform: perspective(140px) rotateX(0deg);
    opacity: 1;
  }
  90%,
  100% {
    -webkit-transform: perspective(140px) rotateY(180deg);
    transform: perspective(140px) rotateY(180deg);
    opacity: 0;
  }
}
@keyframes sk-foldCubeAngle {
  0%, 10% {
    -webkit-transform: perspective(140px) rotateX(-180deg);
    transform: perspective(140px) rotateX(-180deg);
    opacity: 0;
  }
  25%,
  75% {
    -webkit-transform: perspective(140px) rotateX(0deg);
    transform: perspective(140px) rotateX(0deg);
    opacity: 1;
  }
  90%,
  100% {
    -webkit-transform: perspective(140px) rotateY(180deg);
    transform: perspective(140px) rotateY(180deg);
    opacity: 0;
  }
}

这是控制点的坐标。

Poiont1:50,50

Poiont2:50,100

Poiont3:200,100

Poiont4:200,50

Poiont5:350,50

Poiont6:350,100

但这是第1段的坐标。

Q3:-1543,-349

2 个答案:

答案 0 :(得分:1)

您的绘图代码看起来不对。

在函数BSplineCurve中,t参数应取[0,1]范围内的值。通过将t从0更改为1,将在点ControlPoint2ControlPoint3之间构建一个三次B样条曲线。

您可以尝试以下方式:

Dot points[6] = {ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, ControlPoint6};
for(double t = 3.0; t < 6.0; t += 0.001)
{
    const int start = static_cast<int>(t);
    BSplineCurve(points[start - 3], 
                 points[start - 2], 
                 points[start - 1], 
                 points[start], 
                 DrawCurve, 
                 t - start);
    Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}

更新

您的B样条计算代码也看起来错误: - )

bi应该是t3/6.0而不是mt3/6.0。见here(幻灯片25)。

固定功能看起来像这样(我没有测试过):

void BSplineCurve(const Dot &point1, 
                  const Dot &point2, 
                  const Dot &point3,
                  const Dot &point4, 
                  const double t,
                  Dot &result)
{
    const double t2 = t * t;
    const double t3 = t2 * t;
    const double mt = 1.0 - t;
    const double mt3 = mt * mt * mt;

    const double bi3 = mt3;
    const double bi2 = 3 * t3 - 6 * t2 + 4;
    const double bi1 =-3 * t3 + 3 * t2 + 3 * t + 1;
    const double bi  = t3;

    result.x = point1.x * bi3 + 
               point2.x * bi2 +
               point3.x * bi1 +
               point4.x * bi;
    result.x /= 6.0;

    result.y = point1.y * bi3 + 
               point2.y * bi2 +
               point3.y * bi1 +
               point4.y * bi;
    result.y /= 6.0;
}

答案 1 :(得分:0)

也许你使用的点太近了。在样条曲线中,使用非常接近的点并不是很好的iidea。因此,我们已经非常疾驰了#34;曲线。像这样:

enter image description here

红色是原创