From 6f08496a563febc8fac108a274d8ca68cccf9216 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Thu, 30 Nov 2023 00:18:46 +0530 Subject: [PATCH] added new codes, updated file names, restructed the repo --- Assignment A-1 (Scan Fill).cpp | 43 ++++++++ ... Sutherland Line Clipping) (using DDA).cpp | 11 +- ...ne Clipping) (using built-in function).cpp | 11 +- ... using DDA and Bresenham Line Drawing).cpp | 97 ++++++++++++++++++ ...pp => Assignment B-4 (Transformations).cpp | 8 ++ ...ake.cpp => Assignment B-5a (Snowflake).cpp | 7 ++ Assignment B-5b (Hilbert Curve).cpp | 57 ++++++++++ ...ve.cpp => Assignment B-5c (Koch Curve).cpp | 7 ++ DDA-circle.cpp | 7 ++ DDA-line.cpp | 7 ++ DDA-triangle.cpp | 7 ++ HilbertCurve.cpp | 67 ------------ ...df => CGL - Koch and Snowflake Curves.pdf} | Bin 13 files changed, 260 insertions(+), 69 deletions(-) create mode 100644 Assignment A-1 (Scan Fill).cpp rename CohenSutherland(UsingDDA).cpp => Assignment A-2 (Cohen Sutherland Line Clipping) (using DDA).cpp (88%) rename CohenSutherland(Using built-in).cpp => Assignment A-2 (Cohen Sutherland Line Clipping) (using built-in function).cpp (86%) create mode 100644 Assignment A-3 (Pattern using DDA and Bresenham Line Drawing).cpp rename Transformation.cpp => Assignment B-4 (Transformations).cpp (89%) rename Snowflake.cpp => Assignment B-5a (Snowflake).cpp (80%) create mode 100644 Assignment B-5b (Hilbert Curve).cpp rename Koch-Curve.cpp => Assignment B-5c (Koch Curve).cpp (77%) delete mode 100644 HilbertCurve.cpp rename lab-notes/{CGL Koch and Snowflake Curves.pdf => CGL - Koch and Snowflake Curves.pdf} (100%) diff --git a/Assignment A-1 (Scan Fill).cpp b/Assignment A-1 (Scan Fill).cpp new file mode 100644 index 0000000..f241937 --- /dev/null +++ b/Assignment A-1 (Scan Fill).cpp @@ -0,0 +1,43 @@ +/* +Problem Statement: Write C++ program to draw a concave polygon and fill it with desired color using scan +fill algorithm. Apply the concept of inheritance. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE +#include +#include +#include +using namespace std; + +void ffill(int x,int y,int o_col,int n_col) { + int current = getpixel(x,y); + if(current==o_col) { + delay(1); + putpixel(x,y,n_col); + ffill(x+1,y,o_col,n_col); + ffill(x-1,y,o_col,n_col); + ffill(x,y+1,o_col,n_col); + ffill(x,y-1,o_col,n_col); + } +} + +int main() { + int x1,y1,x2,y2,x3,y3,xavg,yavg; + int gdriver = DETECT,gmode; + cout << " \n\t Enter the points of triangle"; + cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; + setcolor(1); + initgraph(&gdriver,&gmode,NULL); + xavg = (int)(x1+x2+x3)/3; + yavg = (int)(y1+y2+y3)/3; + line(x1,y1,x2,y2); + line(x2,y2,x3,y3); + line(x3,y3,x1,y1); + ffill(xavg,yavg,BLACK,RED); + getch(); + delay(50000); + closegraph(); + return 0; +} +// END OF CODE \ No newline at end of file diff --git a/CohenSutherland(UsingDDA).cpp b/Assignment A-2 (Cohen Sutherland Line Clipping) (using DDA).cpp similarity index 88% rename from CohenSutherland(UsingDDA).cpp rename to Assignment A-2 (Cohen Sutherland Line Clipping) (using DDA).cpp index bbb0678..516aa13 100644 --- a/CohenSutherland(UsingDDA).cpp +++ b/Assignment A-2 (Cohen Sutherland Line Clipping) (using DDA).cpp @@ -1,9 +1,16 @@ +/* +Problem Statement: Write C++ program to implement Cohen Sutherland line clipping algorithm. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE #include #include #include #include using namespace std; static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmax,ymax,xmin,ymin; + int getcode(int x,int y) { int code = 0; @@ -13,6 +20,7 @@ int getcode(int x,int y) if(x>xmax) code |= RIGHT; return code; } + void line1(int x1,int y1,int x2,int y2) { float len; @@ -41,12 +49,12 @@ void line1(int x1,int y1,int x2,int y2) int main() { int gd = DETECT,gm; - initgraph(&gd,&gm,NULL); int x1,y1,x2,y2; cout<<"Enter top left and bottom right coordinates: "; cin>>xmin>>ymin>>xmax>>ymax; cout<<"Enter endpoints of line: "; cin>>x1>>y1>>x2>>y2; + initgraph(&gd,&gm,NULL); //outtext("Before clipping"); rectangle(xmin,ymin,xmax,ymax); line1(x1,y1,x2,y2); @@ -131,3 +139,4 @@ int main() closegraph(); return 0; } +// END OF CODE \ No newline at end of file diff --git a/CohenSutherland(Using built-in).cpp b/Assignment A-2 (Cohen Sutherland Line Clipping) (using built-in function).cpp similarity index 86% rename from CohenSutherland(Using built-in).cpp rename to Assignment A-2 (Cohen Sutherland Line Clipping) (using built-in function).cpp index 46b9c6d..e6916aa 100644 --- a/CohenSutherland(Using built-in).cpp +++ b/Assignment A-2 (Cohen Sutherland Line Clipping) (using built-in function).cpp @@ -1,9 +1,17 @@ +/* +Problem Statement: Write C++ program to implement Cohen Sutherland line clipping algorithm. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE #include #include #include #include using namespace std; + static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmax,ymax,xmin,ymin; + int getcode(int x,int y) { int code = 0; @@ -17,12 +25,12 @@ int getcode(int x,int y) int main() { int gd = DETECT,gm; - initgraph(&gd,&gm,NULL); int x1,y1,x2,y2; cout<<"Enter top left and bottom right coordinates: "; cin>>xmin>>ymin>>xmax>>ymax; cout<<"Enter endpoints of line: "; cin>>x1>>y1>>x2>>y2; + initgraph(&gd,&gm,NULL); //outtext("Before clipping"); rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); @@ -107,3 +115,4 @@ int main() closegraph(); return 0; } +// END OF CODE \ No newline at end of file diff --git a/Assignment A-3 (Pattern using DDA and Bresenham Line Drawing).cpp b/Assignment A-3 (Pattern using DDA and Bresenham Line Drawing).cpp new file mode 100644 index 0000000..145eec1 --- /dev/null +++ b/Assignment A-3 (Pattern using DDA and Bresenham Line Drawing).cpp @@ -0,0 +1,97 @@ +/* +Problem Statement:Write C++ program to draw the following pattern. Use DDA line and Bresenham's +circle drawing algorithm. Apply the concept of encapsulation. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE +#include +#include +#include +using namespace std; + +class algo { + public: + void dda_line(float x1, float y1, float x2, float y2); + void bresneham_cir(int r); +}; + +void algo::dda_line(float x1, float y1, float x2, float y2) { + float x,y,dx,dy,step; + int i; + //step 2 + dx=abs(x2-x1); + dy=abs(y2-y1); + cout<<"dy="<=dy) + step=dx; + else + step=dy; + cout<<"\n"<>r; + a1.bresneham_cir((int)r); + ang=3.24/180; + float c=r*cos(30*ang); + float s=r*sin(30*ang); + a1.dda_line(0,r,0-c,0-s); + a1.dda_line(0-c,0-s,0+c,0-s); + a1.dda_line(0+c,0-s,0,r); + r1=s; + a1.bresneham_cir((int)r1); + getch(); + closegraph(); + return 0; +} +// END OF CODE \ No newline at end of file diff --git a/Transformation.cpp b/Assignment B-4 (Transformations).cpp similarity index 89% rename from Transformation.cpp rename to Assignment B-4 (Transformations).cpp index 05c1eb4..692f681 100644 --- a/Transformation.cpp +++ b/Assignment B-4 (Transformations).cpp @@ -1,3 +1,10 @@ +/* +Problem Statement: a) Write C++ program to draw 2-D object and perform following basic transformations: +Scaling, Translation, Rotation. Apply the concept of operator overloading +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE #include #include #include @@ -112,3 +119,4 @@ int main() closegraph(); return 0; } +// END OF CODE \ No newline at end of file diff --git a/Snowflake.cpp b/Assignment B-5a (Snowflake).cpp similarity index 80% rename from Snowflake.cpp rename to Assignment B-5a (Snowflake).cpp index eab94f5..41e950c 100644 --- a/Snowflake.cpp +++ b/Assignment B-5a (Snowflake).cpp @@ -1,3 +1,9 @@ +/* +Problem Statement: a) Write C++ program to generate snowflake using concept of fractals. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE #include #include #include @@ -52,3 +58,4 @@ int main() delay(10000); return 0; } +// END OF CODE \ No newline at end of file diff --git a/Assignment B-5b (Hilbert Curve).cpp b/Assignment B-5b (Hilbert Curve).cpp new file mode 100644 index 0000000..2af17ba --- /dev/null +++ b/Assignment B-5b (Hilbert Curve).cpp @@ -0,0 +1,57 @@ +/* +Problem Statement: b) Write C++ program to generate Hilbert curve using concept of fractals. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE +#include +#include +#include +#include +using namespace std; + +void move(int j, int h, int &x,int &y) { + if(j==1) { + y-=h; + } + else if(j==2) { + x+=h; + } + else if(j==3) { + y+=h; + } + else if(j==4) { + x-=h; + } + lineto(x,y); +} + +void hilbert(int r,int d,int l ,int u,int i,int h,int &x,int &y) { + if(i>0) { + i--; + hilbert(d,r,u,l,i,h,x,y); + move(r,h,x,y); + hilbert(r,d,l,u,i,h,x,y); + move(d,h,x,y); + hilbert(r,d,l,u,i,h,x,y); + move(l,h,x,y); + hilbert(u,l,d,r,i,h,x,y); + } +} + +int main() { + int n,x1,y1; + int x0=50,y0=150,x,y,h=10,r=2,d=3,l=4,u=1; + cout<<"Give the value of n="; + cin>>n; + x=x0; + y=y0; + int driver=DETECT,mode=0; + initgraph(&driver,&mode,NULL); + moveto(x,y); + hilbert(r,d,l,u,n,h,x,y); + delay(10000); + closegraph(); + return 0; +} +// END OF CODE \ No newline at end of file diff --git a/Koch-Curve.cpp b/Assignment B-5c (Koch Curve).cpp similarity index 77% rename from Koch-Curve.cpp rename to Assignment B-5c (Koch Curve).cpp index 212f854..d6b90f4 100644 --- a/Koch-Curve.cpp +++ b/Assignment B-5c (Koch Curve).cpp @@ -1,3 +1,9 @@ +/* +Problem Statement: c) Write C++ program to generate fractal patterns by using Koch curves. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE #include #include #include @@ -47,3 +53,4 @@ int main() delay(10000); return 0; } +// END OF CODE \ No newline at end of file diff --git a/DDA-circle.cpp b/DDA-circle.cpp index 9016521..825c995 100644 --- a/DDA-circle.cpp +++ b/DDA-circle.cpp @@ -1,3 +1,9 @@ +/* +Digital Differential Analyzer (DDA) algorithm for drawing a circle. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE #include #include using namespace std; @@ -48,3 +54,4 @@ int main() { closegraph(); return 0; } +// END OF CODE \ No newline at end of file diff --git a/DDA-line.cpp b/DDA-line.cpp index 60762ba..d7ee338 100644 --- a/DDA-line.cpp +++ b/DDA-line.cpp @@ -1,3 +1,9 @@ +/* +Digital Differential Analyzer (DDA) algorithm for drawing a line. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE #include #include using namespace std; @@ -45,3 +51,4 @@ delay(50000); closegraph(); return (0); } +// END OF CODE \ No newline at end of file diff --git a/DDA-triangle.cpp b/DDA-triangle.cpp index e0a86fa..e7dd0f3 100644 --- a/DDA-triangle.cpp +++ b/DDA-triangle.cpp @@ -1,3 +1,9 @@ +/* +Digital Differential Analyzer (DDA) algorithm for drawing a triangle. +Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG +*/ + +// BEGINNING OF CODE #include #include using namespace std; @@ -66,3 +72,4 @@ int main(){ closegraph(); return 0; } +// END OF CODE \ No newline at end of file diff --git a/HilbertCurve.cpp b/HilbertCurve.cpp deleted file mode 100644 index 9a78a4e..0000000 --- a/HilbertCurve.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include -#include -using namespace std; - -void move(int j, int h, int &x, int &y) -{ - if(j == 1) - { - y = y - h; - } - else if(j == 2) - { - x = x + h; - } - else if(j == 3) - { - y = y + h; - } - else if(j == 4) - { - x = x - h; - } - setcolor(BROWN); - lineto(x,y); -} - -void hilbert(int d, int r, int u, int l, int i, int h, int &x, int &y) -{ - if(i > 0) - { - i--; - hilbert(r,d,l,u,i,h,x,y); - move(d,h,x,y); - hilbert(d,r,u,l,i,h,x,y); - move(r,h,x,y); - hilbert(d,r,u,l,i,h,x,y); - move(u,h,x,y); - hilbert(l,u,r,d,i,h,x,y); - } -} - -int main() -{ - int gd = DETECT,gm; - initgraph(&gd,&gm,NULL); - int x,y,h,n,u = 1,r = 2,d = 3,l = 4; - cout<<"Enter x-co-ordinate of initial point: "; - cin>>x; - - cout<<"Enter y-co-ordinate of initial point: "; - cin>>y; - - cout<<"Enter length of line segment: "; - cin>>h; - - cout<<"Enter order of curve: "; - cin>>n; - - moveto(x,y); - hilbert(d,r,u,l,n,h,x,y); - getch(); - closegraph(); - return 0; -} diff --git a/lab-notes/CGL Koch and Snowflake Curves.pdf b/lab-notes/CGL - Koch and Snowflake Curves.pdf similarity index 100% rename from lab-notes/CGL Koch and Snowflake Curves.pdf rename to lab-notes/CGL - Koch and Snowflake Curves.pdf