Thursday, June 21, 2018

Implementation of curve drawing using Bezier algorithm


  1. #include<dos.h>
  2. #include<stdio.h>
  3. #include<iostream.h>
  4. #include<graphics.h>
  5. #include<conio.h>
  6. int a[150],b[150],m,n=0,j,d; float bx[5000],by[5000],u; union REGS i,o;
  7. initmouse()
  8. {
  9. i.x.ax=0; int86(0x33,&i,&o); return(o.x.ax);
  10. }
  11. void showmouseptr()
  12. {
  13. i.x.ax=1; int86(0x33,&i,&o);
  14. }
  15. void getmousepos(int *button,int *x,int *y)
  16. {
  17. i.x.ax=3; int86(0x33,&i,&o);
  18. *button=o.x.bx;
  19. *x=o.x.cx;
  20. *y=o.x.dx; i.h.ah=0x02; i.h.dh=0;
  21.  


  22. i.h.dl=0;  int86(0x10,&i,&o);  cout<<"X: "<<*x<<" Y: "<<*y; i.h.ah=0x02; int86(0x10,&i,&o);
  23. }
  24. void hidemouseptr()
  25. {
  26. i.x.ax=2; int86(0x33,&i,&o);
  27. }
  28. int compco()
  29. {
  30. int k,coeff=1;
  31. for(k=d+1;k<=n;k++) coeff=coeff*k; for(k=2;k<=n-d;k++) coeff=coeff/k; return coeff;
  32. }
  33. float blenval()
  34. {
  35. int k;
  36. float blend; blend=compco();
  37. for(k=1;k<=d;k++) blend=blend*u; for(k=1;k<=n-d;k++) blend=blend*(1.0-u); return blend;
  38. }
  39. void bezier()
  40. {
  41. float blend1;
  42.  


  43. bx[j]=0;by[j]=0; for(d=0;d<=n;d++)
  44. {
  45. blend1=blenval(); bx[j]+=a[d]*blend1; by[j]+=b[d]*blend1;
  46. }
  47. }
  48. void curve()
  49. {
  50. for(j=0;j<m;j++) putpixel(bx[j],by[j],GREEN);
  51. }
  52. void drawi()
  53. {
  54. for(j=0;j<=m;j++)
  55. {
  56. u=(float)j/m; bezier();
  57. }
  58. curve();
  59. }
  60. void main()
  61. {
  62. int gd=DETECT,gm; int button,tx,ty; clrscr();  m=4990;
  63. flushall(); initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice();
  64.  


  65. showmouseptr(); while(!kbhit())
  66. {
  67. getmousepos(&button,&tx,&ty); if((button & 1) == 1)
  68. {
  69. hidemouseptr(); setcolor(WHITE); a[n]=tx;
  70. b[n]=ty; putpixel(tx,ty,WHITE); circle(tx,ty,2);
  71. n++;
  72. delay(50); showmouseptr();
  73. }
  74. }
  75. n--;
  76. drawi();
  77. getch();
  78. }



Output:

No comments:

Post a Comment