sandipan

C code to use Be'zier curves to draw Bengali Scripts

Sep 6th, 2018
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. /*
  2.  C code to display Bengali Script on the DOS prompt
  3.  Sandipan Dey
  4.  BCSE, JU, Kolkata
  5.  2002
  6. */
  7.  
  8. #include <math.h>
  9. #include <graphics.h>
  10. #include <stdio.h>
  11. #include <conio.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #define MAX 10
  15.  
  16. struct Point{
  17.     double x,y;
  18. };
  19.  
  20. double pow(double x,int i) {
  21.     if (i==0) return 1;                 //0**0=1,for Jn,0(0)=1,i=0
  22.     else if(x<=0 || i<0)return 0;
  23.     else {
  24.         double p=1;
  25.         for(int j=0;j<i;++j)p*=x;
  26.         return p;
  27.     }
  28. }
  29.  
  30. double fact(int n) {
  31.     double f=1;
  32.     for(int i=1;i<=n;++i)
  33.     f=f*i;
  34.     return f;
  35. }
  36.  
  37. double C(int n,int i) {
  38.     if(i<0 || n<0 || i>n)return 0;
  39.     else if(i==0 || i==n)return 1;
  40.     else return fact(n)/(fact(i)*fact(n-i));
  41. }
  42.  
  43. double J(int n,int i,double t) {   //Bernstein Basis Function
  44.     return C(n,i)*pow(t,i)*pow(1-t,n-i);
  45. }
  46.  
  47. Point P(Point* B,int n,double t) {
  48.     Point p={0,0};
  49.     for(int i=0;i<=n;++i) {
  50.         p.x+=B[i].x*J(n,i,t);
  51.         p.y+=B[i].y*J(n,i,t);
  52.     }
  53.     return p;
  54. }
  55.  
  56. void PatternP(int lt=100,int tp=100,int color=15) {
  57.     int n=4;
  58.     double t;
  59.     Point B[MAX]={{lt,tp+30},{lt+50,tp+45},{lt+50,tp+80},{lt,tp+100}},p;
  60.     for(t=0;t<=1;t+=0.005) {
  61.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  62.     } //Draw Be'zier Curve of Degree n-1
  63.     B[0].x=lt;B[0].y=tp+30;
  64.     B[1].x=lt+70;B[1].y=tp+10;
  65.     B[2].x=lt+50;B[2].y=tp+50;
  66.     B[3].x=lt+100;B[3].y=tp+100;
  67.     for(t=0;t<=1;t+=0.005){
  68.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  69.     } //Draw Be'zier Curve of Degree n-1
  70.     B[0].x=lt;B[0].y=tp+100;
  71.     B[1].x=lt+70;B[1].y=tp+100;
  72.     B[2].x=lt+50;B[2].y=tp+50;
  73.     B[3].x=lt+100;B[3].y=tp+100;
  74.     for(t=0;t<=1;t+=0.005){
  75.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  76.     } //Draw Be'zier Curve of Degree n-1
  77.     line(lt+100,tp+10,lt+100,tp+180);
  78.     line(lt+95,tp+20,lt+130,tp+20);
  79. }
  80.  
  81. void PatternB(int lt=100,int tp=100,int color=15) {
  82.     int n=3;
  83.     double t;
  84.     Point B[MAX]={{lt,tp+100},{lt+50,tp+70},{lt+100,tp+180}},p;
  85.     for(t=0;t<=1;t+=0.005) {
  86.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  87.     } //Draw Be'zier Curve of Degree n-1
  88.     line(lt+100,tp+20,lt+100,tp+180);
  89.     line(lt+100,tp+20,lt,tp+100);
  90.     line(lt,tp+20,lt+120,tp+20);
  91. }
  92.  
  93. void PatternJ(int lt=100,int tp=100,int color=15) {
  94.     int n=3;
  95.     double t;
  96.     Point B[MAX]={{lt,tp+120},{lt+50,tp+90},{lt+100,tp+180}},p;
  97.     for(t=0;t<=1;t+=0.005) {
  98.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  99.     } //Draw Be'zier Curve of Degree n-1
  100.     line(lt+100,tp+20,lt+100,tp+180);
  101.     line(lt+5,tp+20,lt+60,tp+75);
  102.     line(lt+60,tp+75,lt,tp+120);
  103.     line(lt,tp+20,lt+110,tp+20);
  104. }
  105.  
  106. void PatternR(int lt=100,int tp=100,int color=15) {
  107.     PatternB(lt,tp,color);
  108.     circle(lt+50,tp+140,15);
  109. }
  110.  
  111. void PatternD(int lt=100,int tp=100,int color=15) {
  112.     int n=4;
  113.     double t;
  114.     Point B[MAX]={{lt+20,tp+100},{lt+60,tp+60},{lt+80,tp+60},{lt+100,tp+100}},p;
  115.     for(t=0;t<=1;t+=0.005) {
  116.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  117.     } //Draw Be'zier Curve of Degree n-1
  118.     B[0].x=lt+100;B[0].y=tp+100;
  119.     B[1].x=lt+50;B[1].y=tp+110;
  120.     B[2].x=lt+50;B[2].y=tp+140;
  121.     B[3].x=lt+90;B[3].y=tp+170;
  122.     for(t=0;t<=1;t+=0.005) {
  123.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  124.     } //Draw Be'zier Curve of Degree n-1
  125.     line(lt,tp+20,lt+110,tp+20);
  126.     line(lt+20,tp+20,lt+20,tp+100);
  127. }
  128.  
  129. void PatternU(int lt=100,int tp=100,int color=15) {
  130.     int n=4;
  131.     double t;
  132.     Point B[MAX]={{lt+10,tp},{lt,tp+50},{lt+40,tp+50},{lt+70,tp}},p;
  133.     for(t=0;t<=1;t+=0.005) {
  134.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  135.     } //Draw Be'zier Curve of Degree n-1
  136.     B[0].x=lt+10;B[0].y=tp;
  137.     B[1].x=lt+70;B[1].y=tp-50;
  138.     B[2].x=lt+70;B[2].y=tp+70;
  139.     B[3].x=lt+200;B[3].y=tp+50;
  140.     for(t=0;t<=1;t+=0.005) {
  141.         p=P(B,n-1,t);putpixel(p.x,p.y,color);
  142.     } //Draw Be'zier Curve of Degree n-1
  143. }
  144.  
  145. void Line(int lt=100,int tp=100,int color=15) {
  146.     setcolor(color);
  147.     line(lt,tp+10,lt,tp+180);
  148.     line(lt-20,tp+20,lt+20,tp+20);
  149. }
  150.  
  151. void Name() {
  152.     rectangle(515,375,630,415);
  153.     outtextxy(520,380,"SANDIPAN DEY");
  154.     outtextxy(520,390,"BCSE-IV");
  155.     outtextxy(520,400,"Roll-99710");
  156. }
  157.  
  158. void main() {
  159.     int xm,ym;
  160.     clrscr();
  161.     int gd=DETECT,gm;
  162.     initgraph(&gd,&gm,"");
  163.     xm=getmaxx();ym=getmaxy();
  164.     int i=1;
  165.     Name();
  166.     while(!kbhit()) {
  167.         //setcolor(i);
  168.         PatternJ(0,100,i);
  169.         Line(125);
  170.         //setcolor(i);
  171.         PatternD(140,100,i);
  172.         PatternB(250,100,i);
  173.         PatternP(375,100,i);
  174.         PatternU(410,280,i);
  175.         PatternR(500,100,i);
  176.         i=(i+1)%15+1;
  177.         delay(1000);
  178.     }
  179.     getch();
  180.     closegraph();
  181. }
Add Comment
Please, Sign In to add comment