added new codes, updated file names, restructed the repo
This commit is contained in:
parent
c8574f03fa
commit
6f08496a56
43
Assignment A-1 (Scan Fill).cpp
Normal file
43
Assignment A-1 (Scan Fill).cpp
Normal file
@ -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<graphics.h>
|
||||||
|
#include<iostream>
|
||||||
|
#include<stdlib.h>
|
||||||
|
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
|
@ -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<iostream>
|
#include<iostream>
|
||||||
#include<graphics.h>
|
#include<graphics.h>
|
||||||
#include<math.h>
|
#include<math.h>
|
||||||
#include<cstdlib>
|
#include<cstdlib>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmax,ymax,xmin,ymin;
|
static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmax,ymax,xmin,ymin;
|
||||||
|
|
||||||
int getcode(int x,int y)
|
int getcode(int x,int y)
|
||||||
{
|
{
|
||||||
int code = 0;
|
int code = 0;
|
||||||
@ -13,6 +20,7 @@ int getcode(int x,int y)
|
|||||||
if(x>xmax) code |= RIGHT;
|
if(x>xmax) code |= RIGHT;
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
void line1(int x1,int y1,int x2,int y2)
|
void line1(int x1,int y1,int x2,int y2)
|
||||||
{
|
{
|
||||||
float len;
|
float len;
|
||||||
@ -41,12 +49,12 @@ void line1(int x1,int y1,int x2,int y2)
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int gd = DETECT,gm;
|
int gd = DETECT,gm;
|
||||||
initgraph(&gd,&gm,NULL);
|
|
||||||
int x1,y1,x2,y2;
|
int x1,y1,x2,y2;
|
||||||
cout<<"Enter top left and bottom right coordinates: ";
|
cout<<"Enter top left and bottom right coordinates: ";
|
||||||
cin>>xmin>>ymin>>xmax>>ymax;
|
cin>>xmin>>ymin>>xmax>>ymax;
|
||||||
cout<<"Enter endpoints of line: ";
|
cout<<"Enter endpoints of line: ";
|
||||||
cin>>x1>>y1>>x2>>y2;
|
cin>>x1>>y1>>x2>>y2;
|
||||||
|
initgraph(&gd,&gm,NULL);
|
||||||
//outtext("Before clipping");
|
//outtext("Before clipping");
|
||||||
rectangle(xmin,ymin,xmax,ymax);
|
rectangle(xmin,ymin,xmax,ymax);
|
||||||
line1(x1,y1,x2,y2);
|
line1(x1,y1,x2,y2);
|
||||||
@ -131,3 +139,4 @@ int main()
|
|||||||
closegraph();
|
closegraph();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// END OF CODE
|
@ -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<iostream>
|
#include<iostream>
|
||||||
#include<graphics.h>
|
#include<graphics.h>
|
||||||
#include<math.h>
|
#include<math.h>
|
||||||
#include<cstdlib>
|
#include<cstdlib>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmax,ymax,xmin,ymin;
|
static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmax,ymax,xmin,ymin;
|
||||||
|
|
||||||
int getcode(int x,int y)
|
int getcode(int x,int y)
|
||||||
{
|
{
|
||||||
int code = 0;
|
int code = 0;
|
||||||
@ -17,12 +25,12 @@ int getcode(int x,int y)
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int gd = DETECT,gm;
|
int gd = DETECT,gm;
|
||||||
initgraph(&gd,&gm,NULL);
|
|
||||||
int x1,y1,x2,y2;
|
int x1,y1,x2,y2;
|
||||||
cout<<"Enter top left and bottom right coordinates: ";
|
cout<<"Enter top left and bottom right coordinates: ";
|
||||||
cin>>xmin>>ymin>>xmax>>ymax;
|
cin>>xmin>>ymin>>xmax>>ymax;
|
||||||
cout<<"Enter endpoints of line: ";
|
cout<<"Enter endpoints of line: ";
|
||||||
cin>>x1>>y1>>x2>>y2;
|
cin>>x1>>y1>>x2>>y2;
|
||||||
|
initgraph(&gd,&gm,NULL);
|
||||||
//outtext("Before clipping");
|
//outtext("Before clipping");
|
||||||
rectangle(xmin,ymin,xmax,ymax);
|
rectangle(xmin,ymin,xmax,ymax);
|
||||||
line(x1,y1,x2,y2);
|
line(x1,y1,x2,y2);
|
||||||
@ -107,3 +115,4 @@ int main()
|
|||||||
closegraph();
|
closegraph();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// END OF CODE
|
@ -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<iostream>
|
||||||
|
#include<graphics.h>
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
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<<"\tdx="<<dx;
|
||||||
|
//step 3
|
||||||
|
if(dx>=dy)
|
||||||
|
step=dx;
|
||||||
|
else
|
||||||
|
step=dy;
|
||||||
|
cout<<"\n"<<step<<endl;
|
||||||
|
//step 4
|
||||||
|
float xinc=float((x2-x1)/step);
|
||||||
|
float yinc=float((y2-y1)/step);
|
||||||
|
//step 5
|
||||||
|
x=x1;
|
||||||
|
y=y1;
|
||||||
|
// outtextxy(0,0,"(0,0)");
|
||||||
|
//step 6
|
||||||
|
i=1;
|
||||||
|
while(i<=step) {
|
||||||
|
// cout<<endl<<"\t"<<i<<"\t(x,y)=("<<x<<","<<y<<")";
|
||||||
|
putpixel(320+x,240-y,4);
|
||||||
|
x=x+xinc;
|
||||||
|
y=y+yinc;
|
||||||
|
i=i+1;
|
||||||
|
// delay(10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void algo::bresneham_cir(int r) {
|
||||||
|
float x,y,p;
|
||||||
|
x=0;
|
||||||
|
y=r;
|
||||||
|
p=3-(2*r);
|
||||||
|
while(x<=y) {
|
||||||
|
putpixel(320+x,240+y,1);
|
||||||
|
putpixel(320-x,240+y,2);
|
||||||
|
putpixel(320+x,240-y,3);
|
||||||
|
putpixel(320-x,240-y,5);
|
||||||
|
putpixel(320+y,240+x,6);
|
||||||
|
putpixel(320+y,240-x,7);
|
||||||
|
putpixel(320-y,240+x,8);
|
||||||
|
putpixel(320-y,240-x,9);
|
||||||
|
x=x+1;
|
||||||
|
if(p<0) {
|
||||||
|
p=p+4*(x)+6;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
p=p+4*(x-y)+10;
|
||||||
|
y=y-1;
|
||||||
|
}
|
||||||
|
// delay(20);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
algo a1;
|
||||||
|
int i;
|
||||||
|
float r,ang,r1;
|
||||||
|
initwindow(630,480);
|
||||||
|
cout<<"Enter radius of circle";
|
||||||
|
cin>>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
|
@ -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<iostream>
|
#include<iostream>
|
||||||
#include<graphics.h>
|
#include<graphics.h>
|
||||||
#include<cmath>
|
#include<cmath>
|
||||||
@ -112,3 +119,4 @@ int main()
|
|||||||
closegraph();
|
closegraph();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// END OF CODE
|
@ -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<iostream>
|
#include<iostream>
|
||||||
#include<graphics.h>
|
#include<graphics.h>
|
||||||
#include<cmath>
|
#include<cmath>
|
||||||
@ -52,3 +58,4 @@ int main()
|
|||||||
delay(10000);
|
delay(10000);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// END OF CODE
|
57
Assignment B-5b (Hilbert Curve).cpp
Normal file
57
Assignment B-5b (Hilbert Curve).cpp
Normal file
@ -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<iostream>
|
||||||
|
#include<graphics.h>
|
||||||
|
#include<math.h>
|
||||||
|
#include<cstdlib>
|
||||||
|
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
|
@ -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<iostream>
|
#include<iostream>
|
||||||
#include<graphics.h>
|
#include<graphics.h>
|
||||||
#include<cmath>
|
#include<cmath>
|
||||||
@ -47,3 +53,4 @@ int main()
|
|||||||
delay(10000);
|
delay(10000);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// END OF CODE
|
@ -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 <iostream>
|
#include <iostream>
|
||||||
#include <graphics.h>
|
#include <graphics.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -48,3 +54,4 @@ int main() {
|
|||||||
closegraph();
|
closegraph();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// END OF CODE
|
@ -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<iostream>
|
#include<iostream>
|
||||||
#include<graphics.h>
|
#include<graphics.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -45,3 +51,4 @@ delay(50000);
|
|||||||
closegraph();
|
closegraph();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
// END OF CODE
|
@ -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 <iostream>
|
#include <iostream>
|
||||||
#include <graphics.h>
|
#include <graphics.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -66,3 +72,4 @@ int main(){
|
|||||||
closegraph();
|
closegraph();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// END OF CODE
|
@ -1,67 +0,0 @@
|
|||||||
#include<iostream>
|
|
||||||
#include<graphics.h>
|
|
||||||
#include<math.h>
|
|
||||||
#include<cstdlib>
|
|
||||||
#include<stdlib.h>
|
|
||||||
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;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user