Задача: Сглаживание кривой В-сплайном
Исходник: Сглаживание кривой В-сплайном, язык: C++ [code #133, hits: 9265]
автор: - [добавлен: 17.05.2006]
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. // this function initializes graphics mode
  7. // it will work only if you're using Borland C++ compiler & BGI drivers
  8. // if you're using another compiler you should overwrite body of this function
  9. void init_gr(void)
  10. {
  11. /* request autodetection */
  12. int gdriver = DETECT, gmode, errorcode;
  13. /* initialize graphics mode */
  14. initgraph(&gdriver, &gmode, "");
  15. /* read result of initialization */
  16. errorcode = graphresult();
  17. if (errorcode != grOk) /* an error occurred */
  18. {
  19. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  20. printf("Press any key to halt:");
  21. getch();
  22. exit(1); /* return with error code */
  23. }
  24. }
  25. // this function shuts graphics mode down
  26. // it will work only if you're using Borland C++ compiler & BGI drivers
  27. // if you're using another compiler you should overwrite body of this function
  28. void end_gr(void)
  29. {
  30. closegraph();
  31. }
  32. // this function moves CP to (x,y) position
  33. // it will work only if you're using Borland C++ compiler & BGI drivers
  34. // if you're using another compiler you should overwrite body of this function
  35. void MoveTo(int x, int y)
  36. {
  37. moveto(x,y);
  38. }
  39. // this function draws a line to (x,y) position
  40. // it will work only if you're using Borland C++ compiler & BGI drivers
  41. // if you're using another compiler you should overwrite body of this function
  42. void LineTo(int x, int y)
  43. {
  44. lineto(x,y);
  45. }
  46. // this function draws a line from (x1,y1) to (x2,y2)
  47. // it will work only if you're using Borland C++ compiler & BGI drivers
  48. // if you're using another compiler you should overwrite body of this function
  49. void Line(int x1, int y1, int x2, int y2)
  50. {
  51. line(x1,y1,x2,y2);
  52. }
  53. const N=21; // number of points
  54. const M=300.0; // number of steps between two points
  55. // coordinates of all given points
  56. int x[N]={50,25,50,125,200,225,275,475,590,615,615,615,600,475,275,225,200,125,50,25,50};
  57. int y[N]={140,100,60,40,70,90,85,75,80,85,100,115,120,125,115,110,130,160,140,100,60};
  58. // coefficients
  59. float t,xA,xB,xC,xD,yA,yB,yC,yD,a0,a1,a2,a3,b0,b1,b2,b3;
  60. int n,i,j,first;
  61. int main(void)
  62. {
  63. // initializing graphics mode
  64. init_gr();
  65. /* mark the given points */
  66. for (i=0;i<N;i++)
  67. {
  68. Line(x[i]-4,y[i]-4,x[i]+4,y[i]+4);
  69. Line(x[i]+4,y[i]-4,x[i]-4,y[i]+4);
  70. }
  71. /* main loop */
  72. first=1;
  73. for(i=1;i<N-2;i++)
  74. {
  75. // calculating coefficients
  76. xA=x[i-1]; xB=x[i]; xC=x[i+1]; xD=x[i+2];
  77. yA=y[i-1]; yB=y[i]; yC=y[i+1]; yD=y[i+2];
  78. a3=(-xA+3*(xB-xC)+xD)/6.0; b3=(-yA+3*(yB-yC)+yD)/6.0;
  79. a2=(xA-2*xB+xC)/2.0; b2=(yA-2*yB+yC)/2.0;
  80. a1=(xC-xA)/2.0; b1=(yC-yA)/2.0;
  81. a0=(xA+4*xB+xC)/6.0; b0=(yA+4*yB+yC)/6.0;
  82. for (j=0;j<M;j++) // drawing curve between two given points
  83. {
  84. t=(float)j/(float)M;
  85. if (first)
  86. {
  87. first=0;
  88. MoveTo(((a3*t+a2)*t+a1)*t+a0,((b3*t+b2)*t+b1)*t+b0);
  89. } else LineTo(((a3*t+a2)*t+a1)*t+a0,((b3*t+b2)*t+b1)*t+b0);
  90. }
  91. }
  92. /* clean up */
  93. getch();
  94. end_gr();
  95.  
  96. return 0;
  97. }
//////////////////////////////////////////////////////////////////////////////
//
// Curve fitting using B-splines
// (c) Johna Smith, 1996
//
// Method description:
// We are drawing lines between points using the following formulas:
// x(t)=((a3*t+a2)*t+a1)*t+a0
// y(t)=((b3*t+b2)*t+b1)*t+b0
// t=0..1
// Look program for formulas for coefficients ai,bi
// These coefficients depends on coordinates of current point,
// previous one, next and next-next ones.
//
//////////////////////////////////////////////////////////////////////////////

+добавить реализацию