how can i change the b-spline curves from 4 point to 6?

917 views Asked by At

I have a code on C++ it's b-spline curve that has 4 points if I want to change it to 6 point what shall I change in the code?

You can check the code:

#include "graphics.h"
#include <math.h>

int main(void) {
  int gd, gm, page = 0;
  gd = VGA;
  gm = VGAMED;
  initgraph(&gd, &gm, "");
  point2d pontok[4] = { 100, 100, 150, 200, 170, 130, 240, 270 }; //pontok means points

  int ap;
  for (;;) {
    setactivepage(page);
    cleardevice();
    for (int i = 0; i < 4; i++)
      circle(integer(pontok[i].x), integer(pontok[i].y), 3);

    double t = 0;

    moveto((1.0 / 6) * (pontok[0].x * pow(1 - t, 3) +
                        pontok[1].x * (3 * t * t * t - 6 * t * t + 4) +
                        pontok[2].x * (-3 * t * t * t + 3 * t * t + 3 * t + 1) +
                        pontok[3].x * t * t * t),
           (1.0 / 6) * (pontok[0].y * pow(1 - t, 3) +
                        pontok[1].y * (3 * t * t * t - 6 * t * t + 4) +
                        pontok[2].y * (-3 * t * t * t + 3 * t * t + 3 * t + 1) +
                        pontok[3].y * t * t * t));

    for (t = 0; t <= 1; t += 0.01)
      lineto(
          (1.0 / 6) * (pontok[0].x * pow(1 - t, 3) +
                       pontok[1].x * (3 * t * t * t - 6 * t * t + 4) +
                       pontok[2].x * (-3 * t * t * t + 3 * t * t + 3 * t + 1) +
                       pontok[3].x * t * t * t),
          (1.0 / 6) * (pontok[0].y * pow(1 - t, 3) +
                       pontok[1].y * (3 * t * t * t - 6 * t * t + 4) +
                       pontok[2].y * (-3 * t * t * t + 3 * t * t + 3 * t + 1) +
                       pontok[3].y * t * t * t));

    /* Egerkezeles */ //Egerkezeles means mouse event handling
    if (!balgomb)
      ap = getactivepoint((point2d *)pontok, 4, 5);
    if (ap >= 0 && balgomb) { //balgomb means left mouse button
      pontok[ap].x = egerx;   //eger means mouse
      pontok[ap].y = egery;
    }
    /* Egerkezeles vege */

    setvisualpage(page);
    page = 1 - page;
    if (kbhit())
      break;
  }
  getch();
  closegraph();
  return 0;
}
1

There are 1 answers

2
fang On

From your formula, it looks like you are trying to draw a cubic Bezier curve. But the formula does not seem entirely correct. You can google "cubic Bezier curve" to find the correct formula. The Wikipedia page contains the formula for any degree of Bezier curve. You can find the "6-points" formula from there by using degree = 5.