Computer
Graphics Programs for SE Computer Engineering
A) Installing
graphics.h in Linux Ubuntu
STEP- 1
Make sure you have
the basic compilers installed.
You need the
build-essential package. For this, run the command:
> sudo apt-get
install build-essential
> sudo apt-get
install g++
STEP- 2
First we need to
install a hand full of packages. You can simply run the following
command and get it all done.
> sudo apt-get
install libsdl-image1.2
libsdl-image1.2-dev guile-1.8 guile-1.8-dev libart-2.0-dev
libaudiofile-dev libesd0-dev libdirectfb-dev
libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev
libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev
STEP- 3
Now, download
libgraph using following link...
http://www.filewatcher.com/m/libgraph-1.0.2.tar.gz.584238-0.html
http://www.filewatcher.com/m/libgraph-1.0.2.tar.gz.584238-0.html
Copy the file
libgraph-1.0.2.tar.gz to our home folder. Right click on the file and
select Extract here.
Open a terminal
and run the following commands, one by one.
> cd
libgraph-1.0.2
> ./configure
> sudo make
> sudo make
install
> sudo cp
/usr/local/lib/libgraph.* /usr/lib
STEP- 4
Now you’re ready
to compile your C++ program!
$ g++
program-name.cpp -o program-name -lgraph
$ ./program-name
..........................................................................................................................................
..........................................................................................................................................
B) Installing eclipse
in Linux Ubuntu
1. First of all, you
need to check whether you have java installed on your system. You can
check by using the command:
java -version
If java is not
installed, then you should install it by doing
sudo apt-get
install default-jre
2. Afterwards,
download Eclipse from the download section of the official website
(http://www.eclipse.org/downloads/). Remember to choose the correct
package for your architecture (32bit or 64 bit). The package will
have the name:
eclipse-standard-kepler-SR1-linux-gtk-x86_32.tar.gz
or
eclipse-standard-kepler-SR1-linux-gtk-x86_64.tar.gz
3. After you have
downloaded the correct package, extract the eclipse.XX.YY.tar.gz
using
tar -zxvf
eclipse.XX.YY.tar.gz
4. switch to root
user:
sudo -i
5. Copy the extracted
folder to /opt
cp -r
eclipse.XX.YY /opt
6. Create a desktop
file and install it:
7. gedit
eclipse.desktop
Copy the following
to the eclipse.desktop file.
[Desktop Entry]
Name=Eclipse
Type=Application
Exec=eclipse
Terminal=false
Icon=eclipse
Comment=Integrated
Development Environment
NoDisplay=false
Categories=Development;IDE;
Name[en]=Eclipse
8. Run the following
command to automatically install it in the unity:
desktop-file-install
eclipse.desktop
9. Create a symlink
in /usr/local/bin using
cd /usr/local/bin
ln -s
/opt/eclipse/eclipse
10. For an eclipse
icon to be displayed in dash, eclipse icon can be added as
cp
/opt/eclipse/icon.xpm /usr/share/pixmaps/eclipse.xpm
..........................................................................................................................................
C) How to Install
OpenGL/Glut libraries in Ubuntu 12.04
http://xyzmind.blogspot.in/2012/08/setup-eclipse-c-and-opengl-support-on.html
We usually use Glut, FreeGlut and Mesa for developing openGL in Linux system. We need to get update from Ubuntu repository before installing openGL development resource and then second step install the openGL library.
We usually use Glut, FreeGlut and Mesa for developing openGL in Linux system. We need to get update from Ubuntu repository before installing openGL development resource and then second step install the openGL library.
sudo apt-get update
sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev
$ Compile: g++
program_name.cpp -o program_name -lGL -lglut
$ Run:
./program_name
How to install and
use Eclipse CDT for C/C++ programming
Follow the
link....
http://www3.ntu.edu.sg/home/ehchua/programming/howto/EclipseCpp_HowTo.html
OR
Step 1: Install
Eclipse C/C++ Development Tool (CDT)
1. Two ways to
install CDT, depending on whether you have previously installed an
Eclipse:
If you have
already installed "Eclipse for Java Developers" or other
Eclipse packages, you could install the CDT plug-in as follows:
Launch Eclipse ⇒
Help ⇒
Install New Software ⇒
In "Work with" field, pull down the drop-down menu and
select "Kepler - http://download.eclipse.org/releases/kepler"
(or juno for Eclipse 4.2; or helios for Eclipse 3.7).
In "Name"
box, expand "Programming Language" node ⇒
Check "C/C++ Development Tools" ⇒
"Next" ⇒
... ⇒
"Finish".
2. If you have not
install any Eclipse package, you could download "Eclipse IDE for
C/C++ Developers" from http://www.eclipse.org/downloads, and
unzip the downloaded file into a directory of your choice.
Step 2:
Configuration
You do NOT need to
do any configuration, as long as the Cygwin or MinGW binaries are
included in the PATH environment variable. CDT searches the PATH to
discover the C/C++ compilers.
Writing your First
C/C++ Program in Eclipse
Step 0: Launch
Eclipse
Start Eclipse by
running "eclipse.exe" in the Eclipse installed directory.
Choose an
appropriate directory for your workspace (i.e., where you would like
to save your works).
If the "welcome"
screen shows up, close it by clicking the "close" button.
Step 1: Create a
new C++ Project
For each C++
application, you need to create a project to keep all the source
codes, object files, executable files, and relevant resources.
To create a new
C++ project:
Choose "File"
menu ⇒
"New" ⇒
Project... ⇒
C/C++ ⇒
C++ project.
The "C++
Project" dialog pops up.
In "Project
name" field, enter "FirstProject".
In "Project
Types" box, select "Executable" ⇒
"Empty Project".
In "Toolchains"
box, choose your compiler, e.g., "Cygwin GCC" or "MinGW
GCC" ⇒
Next.
The "Select
Configurations" dialog appears. Select both "Debug"
and "Release" ⇒
Finish.
Step 2: Write a
Hello-world C++ Program
In the "Project
Explorer" (leftmost panel) ⇒
Right-click on "FirstProject" (or use the "File"
menu) ⇒
New ⇒
Source File.
The "New
Source File" dialog pops up.
In "Source
file" field, enter "Hello.cpp".
Click "Finish".
The source file
"Hello.cpp" opens on the editor panel (double-click on
"test.cpp" to open if necessary). Enter the following
codes:
#include
<iostream>
using namespace
std;
int main() {
cout <<
"Hello, world!" << endl;
return 0;
}
Step 3:
Compile/Build
Right-click on the
"FirstProject" (or use the "Project" menu) ⇒
choose "Build Project" to compile and link the program.
Step 4: Run
To run the
program, right-click on the "FirstProject" (or anywhere on
the source "test.cpp", or select the "Run" menu)
⇒
Run As ⇒
Local C/C++ Application ⇒
(If ask, choose Cygwin's gdb debugger) ⇒
The output "Hello, world!" appears on the "Console"
panel.
NOTE: You need to
create a new C++ project for EACH of your programming problems. This
is messy for writing toy programs!
..........................................................................................................................................
Group A (Compulsory)
1.Writing a C/C++
Program to emulate CPU Architecture (Central Bus) Develop register,
ALU level GUI to display results.
2.Writing a C++
class for displaying pixel or point on the screen.
3.Write a C++ class
for a Line drawing method using overloading DDA and Bresenham’s
Algorithms, inheriting the pixel or point.
4.Write a C++ class
for a circle drawing inheriting line class.
Group B (At least
Six)
5.Write a program in
C/C++ to draw a circle of desired radius.
6.Write a program
using C/C++ to draw a line with line styles (Thick, Thin, Dotted).
7.Write a program in
Java/ Python to draw a line with line style (Thick, Thin, Dotted).
8.Write a C/C++
program to draw a convex polygons (Square, Rectangle, Triangle).
9.Write a C/C++
program to draw a convex polygons (Square, Rectangle, Triangle) using
programmable edges.
10.Write a program in Python to draw a concave polygon.
10.Write a program in Python to draw a concave polygon.
11.Write a program in
Java to draw a concave polygon.
12.Write a C/C++
program to fill polygon using scan line algorithm.
multiplication.
Group C: Advanced
Technology Programming (At least One)
14.Use OpenGL ES to
draw a line for Android Mobile.
15. Use Direct3D/Maya
or open source equivalent to draw a Bouncing ball animation.
........................................................................................................................................
Assignment no: 01 (On Ubuntu)
//Aim: Writing a
C/C++ Program to emulate CPU Architecture (Central Bus) Develop
register, ALU level GUI to display results.
#include<stdio.h>
#include<graphics.h>
using namespace std;
int main()
{
int x,y;
int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,NULL);
setcolor(LIGHTRED);
rectangle(2,15,610,450);
outtextxy(200,430,"Fig 1.1 : CPU Architecture");
//internal memory
setcolor(RED);
sector(297,35,0,360,100,15);//colour of internal memory
ellipse (297,35,0,360,100,15);
outtextxy(240,30,"Internal Memory");
line(300,50,300,75);
line(295,70,300,75); //internal memory arrow
line(305,70,300,75); //internal memory arrow
//external memory
setcolor(GREEN);
sector(297,385,0,360,100,15);//colour of external memory
ellipse(297,385,0,360,100,15);
outtextxy(240,380,"External Memory");
line(300,370,300,350);
line(300,370,295,365); //external memory arrow
line(300,370,305,365); //external memory arrow
//input devices
setcolor(CYAN);
sector(65,220,0,360,50,50);//colour of input devices
circle(65,220,50);
outtextxy(20,215,"Input Devices");
line(115,225,150,225);
//mouse
setcolor(BLUE);
sector(65,300,0,360,50,15);//colour of mouse
ellipse(65,300,0,360,50,15);
outtextxy(46,295,"Mouse");
line(65,270,65,285);
line(70,277,65,270);//mouse arrow
line(60,277,65,270);//mouse arrow
//keyboard
setcolor(BLUE);
sector(65,142,0,360,50,15);//colour of keyboard
ellipse(65,142,0,360,50,15);
outtextxy(35,138,"Keyboard");
line(65,158,65,170);
line(70,163,65,170);//keyboard arrow
line(60,163,65,170);//keyboard arrow
//output devices
setcolor(CYAN);
sector(540,220,0,360,55,55);//colour of output devices
circle(540,220,55);
outtextxy(490,215,"Output Devices");
line(450,225,484,225);
//display
setcolor(LIGHTBLUE);
sector(540,310,0,360,50,15);//colour of display
ellipse(540,310,0,360,50,15);
outtextxy(515,305,"Display");
line(540,275,540,295);
line(535,280,540,275);//display arrow
line(545,280,540,275);//display arrow
//printer
setcolor(LIGHTBLUE);
sector(540,130,0,360,50,15);//colour of printer
ellipse(540,130,0,360,50,15);
outtextxy(515,125,"Printer");
line(540,145,540,165);
line(535,160,540,165);//printer arrow
line(545,160,540,165);//printer arrow
//arrow
line(145,220,150,225); //input arrow
line(145,230,150,225);
line(475,220,484,225); //output arrow
line(475,230,484,225);
//cpu
rectangle(150,75,450,350);
outtextxy(220,100,"Central Processing Unit");
//cu
rectangle(180,150,280,200);
outtextxy(190,160,"Control Unit");
//alu
rectangle(300,150,430,200);
outtextxy(310,160,"Arithematic & ");
outtextxy(310,175,"Logic Unit ");
//memory
rectangle(180,220,430,300);
outtextxy(275,225,"M e m o r y ");
line(180,250,430,250);
line(300,250,300,300);
outtextxy(210,270,"Registers ");
outtextxy(350,270,"Cache ");
delay(10000);
closegraph();
return 0;
}
......................................................................................................................#include<graphics.h>
using namespace std;
int main()
{
int x,y;
int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,NULL);
setcolor(LIGHTRED);
rectangle(2,15,610,450);
outtextxy(200,430,"Fig 1.1 : CPU Architecture");
//internal memory
setcolor(RED);
sector(297,35,0,360,100,15);//colour of internal memory
ellipse (297,35,0,360,100,15);
outtextxy(240,30,"Internal Memory");
line(300,50,300,75);
line(295,70,300,75); //internal memory arrow
line(305,70,300,75); //internal memory arrow
//external memory
setcolor(GREEN);
sector(297,385,0,360,100,15);//colour of external memory
ellipse(297,385,0,360,100,15);
outtextxy(240,380,"External Memory");
line(300,370,300,350);
line(300,370,295,365); //external memory arrow
line(300,370,305,365); //external memory arrow
//input devices
setcolor(CYAN);
sector(65,220,0,360,50,50);//colour of input devices
circle(65,220,50);
outtextxy(20,215,"Input Devices");
line(115,225,150,225);
//mouse
setcolor(BLUE);
sector(65,300,0,360,50,15);//colour of mouse
ellipse(65,300,0,360,50,15);
outtextxy(46,295,"Mouse");
line(65,270,65,285);
line(70,277,65,270);//mouse arrow
line(60,277,65,270);//mouse arrow
//keyboard
setcolor(BLUE);
sector(65,142,0,360,50,15);//colour of keyboard
ellipse(65,142,0,360,50,15);
outtextxy(35,138,"Keyboard");
line(65,158,65,170);
line(70,163,65,170);//keyboard arrow
line(60,163,65,170);//keyboard arrow
//output devices
setcolor(CYAN);
sector(540,220,0,360,55,55);//colour of output devices
circle(540,220,55);
outtextxy(490,215,"Output Devices");
line(450,225,484,225);
//display
setcolor(LIGHTBLUE);
sector(540,310,0,360,50,15);//colour of display
ellipse(540,310,0,360,50,15);
outtextxy(515,305,"Display");
line(540,275,540,295);
line(535,280,540,275);//display arrow
line(545,280,540,275);//display arrow
//printer
setcolor(LIGHTBLUE);
sector(540,130,0,360,50,15);//colour of printer
ellipse(540,130,0,360,50,15);
outtextxy(515,125,"Printer");
line(540,145,540,165);
line(535,160,540,165);//printer arrow
line(545,160,540,165);//printer arrow
//arrow
line(145,220,150,225); //input arrow
line(145,230,150,225);
line(475,220,484,225); //output arrow
line(475,230,484,225);
//cpu
rectangle(150,75,450,350);
outtextxy(220,100,"Central Processing Unit");
//cu
rectangle(180,150,280,200);
outtextxy(190,160,"Control Unit");
//alu
rectangle(300,150,430,200);
outtextxy(310,160,"Arithematic & ");
outtextxy(310,175,"Logic Unit ");
//memory
rectangle(180,220,430,300);
outtextxy(275,225,"M e m o r y ");
line(180,250,430,250);
line(300,250,300,300);
outtextxy(210,270,"Registers ");
outtextxy(350,270,"Cache ");
delay(10000);
closegraph();
return 0;
}
#include<graphics.h>
#include<iostream>
using namespace std;
int main() {
int gdriver = DETECT, gmode=VGAMAX;
int x1 = 200, y1 = 200;
int x2 = 300, y2 = 300;
initgraph(&gdriver, &gmode,NULL);
rectangle(20,20,600,450);
rectangle(220,30,420,120);//processor
outtextxy(280,40,"PROCESSOR");
rectangle(230,60,410,120);
outtextxy(260,90,"CPU");
rectangle(320,60,410,120);
outtextxy(350,90,"ALU");
rectangle(220,180,420,250);//memory
outtextxy(290,210,"MEMORY");
rectangle(50,180,180,250);//i/p devices
outtextxy(70,210,"I/O DEVICES");
rectangle(65,270,210,320); //keyboard
rectangle(70,275,75,280);
rectangle(80,275,85,280);
rectangle(90,275,95,280);
rectangle(100,275,105,280);
rectangle(110,275,115,280);
rectangle(120,275,125,280);
rectangle(130,275,135,280);
rectangle(140,275,145,280);
rectangle(150,275,155,280);
rectangle(160,275,165,280);
rectangle(170,275,175,280);
rectangle(180,275,185,280);
rectangle(70,285,75,290);
rectangle(80,285,85,290);
rectangle(90,285,95,290);
rectangle(100,285,105,290);
rectangle(110,285,115,290);
rectangle(120,285,125,290);
rectangle(130,285,135,290);
rectangle(140,285,145,290);
rectangle(150,285,155,290);
rectangle(160,285,165,290);
rectangle(170,285,175,290);
rectangle(180,285,185,290);
rectangle(70,295,75,300);
rectangle(80,295,85,300);
rectangle(90,295,95,300);
rectangle(100,295,105,300);
rectangle(110,295,115,300);
rectangle(120,295,125,300);
rectangle(130,295,135,300);
rectangle(140,295,145,300);
rectangle(150,295,155,300);
rectangle(160,295,165,300);
rectangle(170,295,175,300);
rectangle(180,295,185,300);
rectangle(70,305,75,310);
rectangle(80,305,85,310);
rectangle(90,305,95,310);
rectangle(100,305,135,310); //space bar
rectangle(140,305,145,310);
rectangle(150,305,155,310);
rectangle(160,305,165,310);
rectangle(170,305,175,310);
rectangle(180,305,185,310);
ellipse(40,300,0,360,10,20); //mouse
arc(68,280,180,110,33);
arc(177,280,271,360,34);
rectangle(450,180,580,250); //o/p devices
outtextxy(480,210,"O/P DEVICES");
rectangle(430,280,590,370); //monitor
rectangle(440,290,580,360);
outtextxy(495,360,"DELL");
line(470,370,490,380);
line(490,380,470,390);
line(555,370,535,380);
line(535,380,555,390);
line(490,380,535,380);
line(470,390,555,390);
line(510,250,510,280);
rectangle(220,320,420,390); //st devices
outtextxy(260,340,"STORAGE DEVICES");
line(320,120,320,180); //direction lines
line(320,250,320,320);
line(180,210,220,210);
line(420,210,450,210);
line(320,120,310,130);
line(320,120,330,130);
line(320,180,310,170);
line(320,180,330,170);
line(220,210,210,200);
line(220,210,210,220);
line(320,250,310,260);
line(320,250,330,260);
line(320,320,310,310);
line(320,320,330,310);
line(450,210,440,200);
line(450,210,440,220);
line(510,280,500,270);
line(510,280,520,270);
delay(10000);
closegraph();
getch();
return 0;
}
..........................................................................................................................................
Assignment no: 02 (On UBUNTU)
//Aim:Writing a
C++ class for displaying pixel or point on the screen.
#include<iostream>
#include<graphics.h>
#include<cstdlib>
using namespace std;
class pixel
{
private:
int xc,yc,cl;
public:
pixel()
{
xc=yc=0;
cl=15;
}
void setcor(int x,int y)
{
xc=x;
yc=y;
}
void setcolor(int p)
{
cl=p;
}
void draw()
{
putpixel(xc,yc,cl);
}
};
int main()
{
int x1,y1,x2,y2,cl1,ch,ch1,xmax,ymax,xmid,ymid;
char a;
int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,NULL);
xmax=getmaxx(); ymax=getmaxy();
xmid=xmax/2; ymid=ymax/2;
line(0,ymid,xmax,ymid); //X-Axis
line(xmid,0,xmid,ymax); //Y-Axis
pixel p1;
do
{
//cleardevice();
cout<<"\n1.PIXEL BY HARDCOADED VALUE";
cout<<"\n2.ACCEPTING CO-ORDINATES";
cout<<"\n3.Exit";
cout<<"\nEnter your choice:- ";
cin>>ch;
switch(ch)
{
case 1:
p1.setcor(xmid+50,ymid-50);
p1.setcolor(15);
p1.draw();
break;
case 2:
{
cout<<"Enter X And Y coordinate:- ";
cin>>x1>>y1;
cout<<"\nEnter Color For Pixel:- ";
cin>>cl1;
cout<<"\nEnter the quadrant Number(1/2/3/4) to draw the pixel:- ";
cin>>ch1;
if(ch1==1)
{
cout<<"\nFirst Quadrant is Selected !!!\n";
x2=xmid+x1;
y2=ymid-y1;
p1.setcor(x2,y2);
p1.setcolor(cl1);
p1.draw();
}
else
if(ch1==2)
{
cout<<"\nSecond Quadrant is Selected !!!\n";
x2=xmid-x1;
y2=ymid-y1;
p1.setcor(x2,y2);
p1.setcolor(cl1);
p1.draw();
}
else
if(ch1==3)
{
cout<<"\nThird Quadrant is Selected !!!\n";
x2=xmid-x1;
y2=ymid+y1;
p1.setcor(x2,y2);
p1.setcolor(cl1);
p1.draw();
}
else
if(ch1==4)
{
cout<<"\nFourth Quadrant is Selected !!!\n";
x2=xmid+x1;
y2=ymid+y1;
p1.setcor(x2,y2);
p1.setcolor(cl1);
p1.draw();
}
break;
}
case 3:
exit(0);
}
cout<<"\nDO U Want To Continue y OR n:- ";
cin>>a;
}
while(a!='n');
exit(0);
getch();
closegraph();
return 0;
}
#include<graphics.h>
#include<cstdlib>
using namespace std;
class pixel
{
private:
int xc,yc,cl;
public:
pixel()
{
xc=yc=0;
cl=15;
}
void setcor(int x,int y)
{
xc=x;
yc=y;
}
void setcolor(int p)
{
cl=p;
}
void draw()
{
putpixel(xc,yc,cl);
}
};
int main()
{
int x1,y1,x2,y2,cl1,ch,ch1,xmax,ymax,xmid,ymid;
char a;
int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,NULL);
xmax=getmaxx(); ymax=getmaxy();
xmid=xmax/2; ymid=ymax/2;
line(0,ymid,xmax,ymid); //X-Axis
line(xmid,0,xmid,ymax); //Y-Axis
pixel p1;
do
{
//cleardevice();
cout<<"\n1.PIXEL BY HARDCOADED VALUE";
cout<<"\n2.ACCEPTING CO-ORDINATES";
cout<<"\n3.Exit";
cout<<"\nEnter your choice:- ";
cin>>ch;
switch(ch)
{
case 1:
p1.setcor(xmid+50,ymid-50);
p1.setcolor(15);
p1.draw();
break;
case 2:
{
cout<<"Enter X And Y coordinate:- ";
cin>>x1>>y1;
cout<<"\nEnter Color For Pixel:- ";
cin>>cl1;
cout<<"\nEnter the quadrant Number(1/2/3/4) to draw the pixel:- ";
cin>>ch1;
if(ch1==1)
{
cout<<"\nFirst Quadrant is Selected !!!\n";
x2=xmid+x1;
y2=ymid-y1;
p1.setcor(x2,y2);
p1.setcolor(cl1);
p1.draw();
}
else
if(ch1==2)
{
cout<<"\nSecond Quadrant is Selected !!!\n";
x2=xmid-x1;
y2=ymid-y1;
p1.setcor(x2,y2);
p1.setcolor(cl1);
p1.draw();
}
else
if(ch1==3)
{
cout<<"\nThird Quadrant is Selected !!!\n";
x2=xmid-x1;
y2=ymid+y1;
p1.setcor(x2,y2);
p1.setcolor(cl1);
p1.draw();
}
else
if(ch1==4)
{
cout<<"\nFourth Quadrant is Selected !!!\n";
x2=xmid+x1;
y2=ymid+y1;
p1.setcor(x2,y2);
p1.setcolor(cl1);
p1.draw();
}
break;
}
case 3:
exit(0);
}
cout<<"\nDO U Want To Continue y OR n:- ";
cin>>a;
}
while(a!='n');
exit(0);
getch();
closegraph();
return 0;
}
.......................................................................................................................
Assignment no: 03
//Write a C++
class for a Line drawing method using overloading DDA and Bresenham’s
Algorithms, inheriting the pixel or point.
A Simple program
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
int main()
{
int gd=DETECT,gm=VGAMAX;
int x1,y1,x2,y2,dx,dy,m,c;
initgraph(&gd,&gm,NULL);
cout<<"\n Enter Co-ordinates: "; cin >> x1 >> y1 >> x2 >> y2;
dx = x2 - x1;
dy = y2 - y1;
m = dy/dx;
c = y1 - m *x1; // y = m*x + c
while( x1 <= x2 )
{
if( m <= 1)
{
x1 = x1 + 1;
y1 = m *x1 + c;
putpixel(x1,y1,WHITE);
}
else
{
y1 = y1 + 1;
x1 = (y1 - c)/m;
putpixel(x1,y1,WHITE);
}
}
delay(3000);
getch();
closegraph();
return 0;
}
...........................................................................................>>>>>>
#include<graphics.h>
#include<iostream>
using namespace
std;
class pt //base
class
{
protected: int
xco,yco,color;
public:
pt()
{
xco=0;yco=0;color=15;
}
void setco(int
x,int y)
{
xco=x;
yco=y;
}
void
setcolor(int c)
{
color=c;
}
void draw()
{
putpixel(xco,yco,color);
}
};
class dline:
public pt //derived class
{
private: int
x2,y2;
public:
dline():pt()
{
x2=0,y2=0;
}
void
setline(int x, int y, int xx, int yy)
{
pt::setco(x,y);
x2=xx;
y2=yy;
}
void drawl()
//Bresenham's Line
{
float
x,y,dx,dy,e,temp;
int
i,s1,s2,ex;
dx=abs(x2-xco);
dy=abs(y2-yco);
x=xco;
y=yco;
pt::setco(x,y);
pt::draw();
if(x2 >
xco) //sign() function
{
s1=1;
}
if(x2 <
xco)
{
s1=-1;
}
if(x2 == xco)
{
s1=0;
}
if(y2 >
yco)
{
s2=1;
}
if(y2 <
yco)
{
s2=-1;
}
if(y2 == yco)
{
s2=0;
}
if(dy >
dx)
{
temp =
dx;
dx =
dy;
dy =
temp;
ex =
1;
}
else
{
ex=0;
}
e=2*dy-dx;
//decision variable
i=1;
do
{
while(e>=0)
{
if(ex==1)
{
x = x + s1;
}
else
{
y = y + s2;
}
e = e
+ 2*dy - 2*dx;
}
if(ex==1)
{
y = y + s2;
}
else
{
x = x + s1;
}
e = e
+ 2*dy;
pt::setco(x,y);
pt::draw();
i = i
+ 1;
}while(i<=dx);
}
void drawl(int
colour) //DDA Line
{
float
x,y,dx,dy,len;
int i;
pt::setcolor(colour);
dx=abs(x2-xco);
dy=abs(y2-yco);
if(dx >=
dy)
{
len=dx;
}
else
{
len=dy;
}
dx=(x2-xco)/len;
dy=(y2-yco)/len;
x = xco +
0.5;
y = yco +
0.5;
i=1;
while(i<=len)
{
pt::setco(x,y);
pt::draw();
x = x
+ dx;
y = y
+ dy;
i = i
+ 1;
cout<<"\ti"<<i;
cout<<"\tx"<<x;
cout<<"\ty
"<<y<<endl;
}
pt::setco(x,y);
pt::draw();
}
};
int main()
{
int
gd=DETECT,gm=VGAMAX;
int
ch,x1,y1,x2,y2, xmax,ymax,xmid,ymid;
char a;
initgraph(&gd,&gm,NULL);
pt p;
dline dda;
xmax =
getmaxx();
ymax =
getmaxy();
xmid = xmax
/2;
ymid = ymax
/2;
line(xmid,0,xmid,ymax); //Y co-ordinate
line(0,ymid,xmax,ymid); //X co-ordinate
do
{
xmax =
getmaxx();
ymax =
getmaxy();
xmid = xmax
/2;
ymid = ymax
/2;
cout<<"1.DDA
LINE..";
cout<<"\n2.BRESENHAM'S LINE..";
cout<<"\n3.EXIT..";
cout<<"\nEnter
your choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
dda.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
dda.drawl(15);
break;
case 2:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
dda.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
dda.drawl();
break;
case 3:
exit;
break;
}
cout<<"\nDO
U Want To Continue y OR n: ";
cin>>a;
}while(a!='n');
delay(3000);
getch();
closegraph();
return 0;
}
..........................................................................................................................................
Assignment no: 04
//Aim:Write a C++
class for a circle drawing inheriting line class.
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace
std;
class dline
//base class
{
protected: int
x1,y1,x2,y2, colour;
public:
dline()
{
x1=0;
y1=0; x2=0, y2=0;
}
void
setcolor(int color)
{
colour
=color;
}
void
setline(float x1,float y1, float xx,float yy)
{
x1=x1;
y1=y1; x2=xx, y2=yy;
}
void
drawl(float x1,float y1, float xx,float yy) //Simple DDA Line
{
float
x,y,dx,dy,len;
int i;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx >=
dy)
{
len=dx;
}
else
{
len=dy;
}
dx=(x2-x1)/len;
dy=(y2-y1)/len;
x = x1 +
0.5;
y = y1 +
0.5;
i=1;
while(i<=len)
{
putpixel(x,y,colour);
x = x
+ dx;
y = y
+ dy;
i = i
+ 1;
}
}
};
class
mcircle:public dline
{
private: int
x0,y0;
public:
mcircle():dline()
{
x0=0;y0=0;
}
void
drawc(float x1, float y1, int r)
{
int i, x,
y;
float d;
x=0, y=r;
d = 1.25 -
r; //decision variabel
dline::setline(x,y,x,y);
do
{
dline::drawl(x1+x, y1+y,x1+x, y1+y);
dline::drawl(x1+y, y1+x,x1+y, y1+x);
dline::drawl(x1+y, y1-x,x1+y, y1-x);
dline::drawl(x1+x, y1-y,x1+x, y1-y);
dline::drawl(x1-x, y1-y,x1-x, y1-y);
dline::drawl(x1-y, y1-x,x1-y, y1-x);
dline::drawl(x1-y, y1+x,x1-y, y1+x);
dline::drawl(x1-x, y1+y,x1-x, y1+y);
if(d<0)
{
x
= x + 1;
d
= d + (2*x) + 3;
}
else
{
x
= x + 1;
y
= y - 1;
d
= d + (2*x-2*y) + 3;
}
}while(x<=y);
}
};
int main()
{
int
gd=DETECT,gm=VGAMAX;
int i, x, y,
r, xmax,ymax,xmid,ymid;
initgraph(&gd,&gm,NULL);
dline l;
mcircle c;
cout<<"\n
Enter x: "; cin>>x;
cout<<"\n
Enter y: "; cin>>y;
cout<<"\n
Enter radius: "; cin>>r;
l.setcolor(15);
c.drawc(x,y,r);
delay(3000);
getch();
closegraph();
return 0;
}
...............................................................................................................................................................................................
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace
std;
class dline
//base class
{
protected: int
x0,y0,x1,y1,x2,y2, colour;
public:
dline()
{
x1=0;
y1=0; x2=0, y2=0;
}
void
setcolor(int color)
{
colour
=color;
}
void
setoff1(int xx,int yy)
{
x0=xx;
y0=yy;
}
void
setline(float x1,float y1, float xx,float yy)
{
x1=x1+x0;
y1=y0-y1; x2=xx+x0, y2=y0-yy;
}
void
drawl(float x1,float y1, float xx,float yy) //Simple DDA Line
{
float
x,y,dx,dy,len;
int i;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx >=
dy)
{
len=dx;
}
else
{
len=dy;
}
dx=(x2-x1)/len;
dy=(y2-y1)/len;
x = x1 +
0.5;
y = y1 +
0.5;
putpixel(x,y,colour);
x = x
+ dx;
y = y
+ dy;
}
};
class
mcircle:public dline
{
private: int
x0,y0;
public:
mcircle():dline()
{
x0=0;y0=0;
}
void
setoff(int xx,int yy)
{
x0=xx;
y0=yy;
}
void
drawc(float x1, float y1, int r)
{
int i, x,
y;
float d;
x=0, y=r;
d = 1.25 -
r; //decision variabel
dline::setline(x,y,x,y);
do
{
dline::drawl(x1+x0+x, y0-y1+y,x1+x0+x, y0-y1+y);
dline::drawl(x1+x0+y, y0-y1+x,x1+x0+y, y0-y1+x);
dline::drawl(x1+x0+y, y0-y1-x,x1+x0+y, y0-y1-x);
dline::drawl(x1+x0+x, y0-y1-y,x1+x0+x, y0-y1-y);
dline::drawl(x1+x0-x, y0-y1-y,x1+x0-x, y0-y1-y);
dline::drawl(x1+x0-y, y0-y1-x,x1+x0-y, y0-y1-x);
dline::drawl(x1+x0-y, y0-y1+x,x1+x0-y, y0-y1+x);
dline::drawl(x1+x0-x, y0-y1+y,x1+x0-x, y0-y1+y);
if(d<0)
{
x
= x + 1;
d
= d + (2*x) + 3;
}
else
{
x
= x + 1;
y
= y - 1;
d
= d + (2*x-2*y) + 3;
}
}while(x<=y);
}
};
int main()
{
int
gd=DETECT,gm=VGAMAX;
int i, x, y,
r, xmax,ymax,xmid,ymid;
char a;
initgraph(&gd,&gm,NULL);
dline l;
mcircle c;
xmax =
getmaxx();
ymax =
getmaxy();
xmid =
xmax /2;
ymid =
ymax /2;
line(xmid,0,xmid,ymax); //Y co-ordinate
line(0,ymid,xmax,ymid); //X co-ordinate
do
{
cout<<"\n
Enter x: "; cin>>x;
cout<<"\n
Enter y: "; cin>>y;
cout<<"\n
Enter radius: "; cin>>r;
c.setoff(xmid,
ymid);
l.setoff1(xmid, ymid);
l.setcolor(15);
c.drawc(x,y,r);
cout<<"\nDO
U Want To Continue y OR n: ";
cin>>a;
}while(a!='n');
delay(3000);
getch();
closegraph();
return 0;
}
..........................................................................................................................................
Assignment no: 05
//Aim:Write a
program in C/C++ to draw a circle of desired radius.
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace
std;
class dcircle
{
private: int
x0,y0;
public:
dcircle()
{
x0=0;y0=0;
}
void
setoff(int xx,int yy)
{
x0=xx;
y0=yy;
}
void
drawdc(float x, float y, int r) //DDA Circle
{
float
x1,y1,x2,y2,startx,starty,ep;
int i,val;
x1=r*cos(0); //Initialize starting point
y1=r*sin(0);
startx =
x1;
starty =
y1;
i=0;
do
{
val =
pow(2,i);
i++;
}while(val<r);
ep =
1/pow(2,i-1); //calculation of epsilon
do
{
x2 =
x1 + y1*ep;
y2 =
y1 - x2*ep;
putpixel(x0+x+x2, y0-(y+y2),15);
x1 =
x2;
y1 =
y2;
}while((y1
- starty) < ep || (startx - x1) > ep);
}
void
drawbc(int x1, int y1, int r) //Bresenham's Circle
{
int i, x,
y;
float d;
x=0, y=r;
d = 3 -
2*r; //decision variable
do
{
putpixel(x1+x0+x, y0-y1+y,15);
putpixel(x1+x0+y, y0-y1+x,15);
putpixel(x1+x0+y, y0-y1-x,15);
putpixel(x1+x0+x, y0-y1-y,15);
putpixel(x1+x0-x, y0-y1-y,15);
putpixel(x1+x0-y, y0-y1-x,15);
putpixel(x1+x0-y, y0-y1+x,15);
putpixel(x1+x0-x, y0-y1+y,15);
if(d<=0)
{
x
= x + 1;
d
= d + (4*x) + 6;
}
else
{
x
= x + 1;
y
= y - 1;
d
= d + (4*x-4*y) + 10;
}
}while(x<=y);
}
void
drawmc(float x1, float y1, int r) // Mid point Circle
{
int i, x,
y;
float d;
x=0, y=r;
d = 1.25 -
r; //decision variable
do
{
putpixel(x1+x0+x, y0-y1+y,15);
putpixel(x1+x0+y, y0-y1+x,15);
putpixel(x1+x0+y, y0-y1-x,15);
putpixel(x1+x0+x, y0-y1-y,15);
putpixel(x1+x0-x, y0-y1-y,15);
putpixel(x1+x0-y, y0-y1-x,15);
putpixel(x1+x0-y, y0-y1+x,15);
putpixel(x1+x0-x, y0-y1+y,15);
if(d<0)
{
x
= x + 1;
d
= d + (2*x) + 3;
}
else
{
x
= x + 1;
y
= y - 1;
d
= d + (2*x-2*y) + 5;
}
}while(x<=y);
}
};
int main()
{
int
gd=DETECT,gm=VGAMAX;
int i, x, y,
r,ch, xmax,ymax,xmid,ymid;
float a,b;
char ans;
initgraph(&gd,
&gm, NULL);
dcircle c1;
xmax =
getmaxx();
ymax =
getmaxy();
xmid = xmax
/2;
ymid = ymax
/2;
line(xmid,0,xmid,ymax); //Y co-ordinate
line(0,ymid,xmax,ymid); //X co-ordinate
do
{
cout<<"\nEnter Cricle Drwaing algorithm";
cout<<"\n1.DDA..";
cout<<"\n2.BRESENHAM'S..";
cout<<"\n3.MID POINT..";
cout<<"\n4.EXIT..";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\n
Enter x: "; cin>>a;
cout<<"\n
Enter y: "; cin>>b;
cout<<"\n
Enter radius: "; cin>>r;
c1.setoff(xmid, ymid);
setcolor(15);
c1.drawdc(a,b,r);
break;
}
case 2:
{
cout<<"\n
Enter x: "; cin>>x;
cout<<"\n
Enter y: "; cin>>y;
cout<<"\n
Enter radius: "; cin>>r;
c1.setoff(xmid, ymid);
setcolor(15);
c1.drawbc(x,y,r);
break;
}
case 3:
{
cout<<"\n
Enter x: "; cin>>x;
cout<<"\n
Enter y: "; cin>>y;
cout<<"\n
Enter radius: "; cin>>r;
c1.setoff(xmid, ymid);
setcolor(15);
c1.drawmc(x,y,r);
break;
}
case 4:
exit;
break;
}
cout<<"\nDO
U Want To Continue y OR n: ";
cin>>ans;
}while(ans!='n');
delay(3000);
getch();
closegraph();
return 0;
}
/*
gescoe@gescoe-VirtualBox:~/Downloads/cgprogram012$
g++ 5.cpp -o 5 -lgraph
gescoe@gescoe-VirtualBox:~/Downloads/cgprogram012$
./5
Enter Cricle
Drwaing algorithm
1.DDA..
2.BRESENHAM'S..
3.MID POINT..
4.EXIT..
Enter your choice:
[xcb] Unknown sequence number while processing queue
[xcb] Most likely
this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting,
sorry about that.
5:
../../src/xcb_io.c:274: poll_for_event: Assertion
`!xcb_xlib_threads_sequence_lost' failed.
1
Enter x: 100
Enter y: 100
Enter radius: 50
DO U Want To
Continue y OR n: y
Enter Cricle
Drwaing algorithm
1.DDA..
2.BRESENHAM'S..
3.MID POINT..
4.EXIT..
Enter your choice:
2
Enter x: -100
Enter y: -100
Enter radius: 60
DO U Want To
Continue y OR n: y
Enter Cricle
Drwaing algorithm
1.DDA..
2.BRESENHAM'S..
3.MID POINT..
4.EXIT..
Enter your choice:
3
Enter x: -100
Enter y: 100
Enter radius: 80
DO U Want To
Continue y OR n: n */
..........................................................................................................................................
Assignment no: 06
//Aim:Write a
program using C/C++ to draw a line with line styles (Thick, Thin,
Dotted).
//Line styles
using DDA
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace
std;
class pt //base
class
{
protected: int
xco,yco,color;
public:
pt()
{
xco=0;yco=0;color=15;
}
void setco(int
x,int y)
{
xco=x;
yco=y;
}
void
setcolor(int c)
{
color=c;
}
void draw()
{
putpixel(xco,yco,color);
}
};
class dline:
public pt //derived class
{
private: int
x2,y2;
public:
dline():pt()
{
x2=0,y2=0;
}
void
setline(int x, int y, int xx, int yy)
{
pt::setco(x,y);
x2=xx;
y2=yy;
}
void
drawsi(int colour) //Simple DDA Line
{
float
x,y,dx,dy,len;
int i;
pt::setcolor(colour);
dx=abs(x2-xco);
dy=abs(y2-yco);
if(dx >=
dy)
{
len=dx;
}
else
{
len=dy;
}
dx=(x2-xco)/len;
dy=(y2-yco)/len;
x = xco +
0.5;
y = yco +
0.5;
i=1;
while(i<=len)
{
pt::setco(x,y);
pt::draw();
x = x
+ dx;
y = y
+ dy;
i = i
+ 1;
}
pt::setco(x,y);
pt::draw();
}
void drawda(int
colour) //Dash DDA Line
{
float
x,y,dx,dy,len;
int
i,dash_pixel=0, dash_space=0;
pt::setcolor(colour);
dx=abs(x2-xco);
dy=abs(y2-yco);
if(dx >=
dy)
{
len=dx;
}
else
{
len=dy;
}
dx=(x2-xco)/len;
dy=(y2-yco)/len;
x = xco +
0.5;
y = yco +
0.5;
i=1;
while(i<=len)
{
dash_pixel=0;
while(dash_pixel<5)
{
pt::setco(x,y);
pt::draw();
x = x
+ dx;
y = y
+ dy;
i = i
+ 1;
dash_pixel = dash_pixel +1;
}
dash_space=0;
while(dash_space<=2)
{
x = x
+ dx;
y = y
+ dy;
i = i
+ 1;
dash_space = dash_space +1;
}
}
}
void drawdo(int
colour) //Dotted DDA Line
{ float
x,y,dx,dy,len;
int
i,dot_space;
pt::setcolor(colour);
dx=abs(x2-xco);
dy=abs(y2-yco);
if(dx >=
dy)
{
len=dx;
}
else
{
len=dy;
}
dx=(x2-xco)/len;
dy=(y2-yco)/len;
x = xco +
0.5;
y = yco +
0.5;
i=1;
while(i<=len)
{
dot_space=0;
while(dot_space<=1)
{
x = x
+ dx;
y = y
+ dy;
i = i
+ 1;
dot_space = dot_space +1;
}
pt::setco(x,y);
pt::draw();
}
}
void drawth(int
x1,int y1, int x2, int y2,int colour ) //Thick DDA Line
{
float
x,y,dx,dy,len;
int i;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx >=
dy)
{
len=dx;
}
else
{
len=dy;
}
dx=(x2-x1)/len;
dy=(y2-y1)/len;
x = x1 +
0.5;
y = y1 +
0.5;
i=1;
while(i<=len)
{
putpixel(x,y,colour);
x = x
+ dx;
y = y
+ dy;
i = i
+ 1;
}
putpixel(x,y,colour);
}
};
int main()
{
int
gd=DETECT,gm=VGAMAX;
int i,
ch,x1,y1,x2,y2, dx,dy,xmax,ymax,xmid,ymid,wx,wy,th;
char a;
initgraph(&gd,&gm,NULL);
dline ls;
xmax =
getmaxx();
ymax =
getmaxy();
xmid = xmax
/2;
ymid = ymax
/2;
line(xmid,0,xmid,ymax); //Y co-ordinate
line(0,ymid,xmax,ymid); //X co-ordinate
do
{ xmax =
getmaxx();
ymax =
getmaxy();
xmid = xmax
/2;
ymid = ymax
/2;
cout<<"\nEnter
Line Styles";
cout<<"\n1.SIMPLE..";
cout<<"\n2.DASH..";
cout<<"\n3.DOTTED..";
cout<<"\n4.THICK..";
cout<<"\n5.EXIT..";
cout<<"\nEnter
your choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
ls.drawsi(15);
break;
case 2:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
ls.drawda(15);
break;
case 3:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
ls.drawdo(15);
break;
case 4:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
cout<<"Enter
Thickness: ";
cin>>th;
ls.drawth(x1+xmid,ymid-y1,x2+xmid,ymid-y2,15);
if((y2-y1)/(x2-x1) <1)
{
wy=(th-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*abs(x2-x1));
for(i=0;i<wy;i++)
{
ls.drawth(x1+xmid,ymid-y1-i,x2+xmid,ymid-y2-i,15);
ls.drawth(x1+xmid,ymid-y1+i,x2+xmid,ymid-y2+i,15);
}
}
else
{
wx=(th-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*abs(y2-y1));
for(i=0;i<wx;i++)
{
ls.drawth(x1+xmid-i,ymid-y1,x2+xmid-i,ymid-y2,15);
ls.drawth(x1+xmid+i,ymid-y1,x2+xmid+i,ymid-y2,15);
}
}
break;
case 5:
exit;
break;
}
cout<<"\nDO
U Want To Continue y OR n: ";
cin>>a;
}while(a!='n');
delay(3000);
getch();
closegraph();
return 0;
}
/*
gescoe@gescoe-VirtualBox:~/Downloads/cgprogram012$
./a05
Enter Line Styles
1.SIMPLE..
2.DASH..
3.DOTTED..
4.THICK..
5.EXIT..
Enter your
choice:1
Enter x1: 10
Enter y1: 10
Enter x2: 60
Enter y2: 60
DO U Want To
Continue y OR n: y
Enter Line Styles
1.SIMPLE..
2.DASH..
3.DOTTED..
4.THICK..
5.EXIT..
Enter your choice:
2
Enter x1: -10
Enter y1: 10
Enter x2: -80
Enter y2: 80
DO U Want To
Continue y OR n: y
Enter Line Styles
1.SIMPLE..
2.DASH..
3.DOTTED..
4.THICK..
5.EXIT..
Enter your choice:
3
Enter x1: -10
Enter y1: -10
Enter x2: -100
Enter y2: -100
DO U Want To
Continue y OR n: y
Enter Line Styles
1.SIMPLE..
2.DASH..
3.DOTTED..
4.THICK..
5.EXIT..
Enter your choice:
4
Enter x1: 10
Enter y1: -10
Enter x2: 100
Enter y2: -100
Enter Thickness:
10
DO U Want To
Continue y OR n: n */
---------------------------------------------------------------------------------------------------
//line styles
using Bresenham's algo
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace
std;
class pt //base
class
{
protected: int
xco,yco,color;
public:
pt()
{
xco=0;yco=0;color=15;
}
void setco(int
x,int y)
{
xco=x;
yco=y;
}
void
setcolor(int c)
{
color=c;
}
void draw()
{
putpixel(xco,yco,color);
}
};
class dline:
public pt //derived class
{
private: int
x2,y2;
public:
dline():pt()
{
x2=0,y2=0;
}
void
setline(int x, int y, int xx, int yy)
{
pt::setco(x,y);
x2=xx;
y2=yy;
}
void drawsi()
//Bresenham's Line
{
float
x,y,dx,dy,e,temp;
int
i,s1,s2,ex;
dx=abs(x2-xco);
dy=abs(y2-yco);
x=xco;
y=yco;
pt::setco(x,y);
pt::draw();
if(x2 >
xco) //sign() function
{
s1=1;
}
if(x2 <
xco)
{
s1=-1;
}
if(x2 == xco)
{
s1=0;
}
if(y2 >
yco)
{
s2=1;
}
if(y2 <
yco)
{
s2=-1;
}
if(y2 == yco)
{
s2=0;
}
if(dy >
dx)
{
temp =
dx;
dx =
dy;
dy =
temp;
ex =
1;
}
else
{
ex=0;
}
e=2*dy-dx;
//decision variable
i=1;
do
{
while(e>=0)
{
if(ex==1)
{
x = x + s1;
}
else
{
y = y + s2;
}
e = e
+ 2*dy - 2*dx;
}
if(ex==1)
{
y = y + s2;
}
else
{
x = x + s1;
}
e = e
+ 2*dy;
pt::setco(x,y);
pt::draw();
i = i
+ 1;
}while(i<=dx);
}
void drawda()
//Dash Line
{
float
x,y,dx,dy,e,temp;
int
i,s1,s2,ex, dash=0;
dx=abs(x2-xco);
dy=abs(y2-yco);
x=xco;
y=yco;
pt::setco(x,y);
pt::draw();
if(x2 >
xco) //sign() function
{
s1=1;
}
if(x2 <
xco)
{
s1=-1;
}
if(x2 == xco)
{
s1=0;
}
if(y2 >
yco)
{
s2=1;
}
if(y2 <
yco)
{
s2=-1;
}
if(y2 == yco)
{
s2=0;
}
if(dy >
dx)
{
temp =
dx;
dx =
dy;
dy =
temp;
ex =
1;
}
else
{
ex=0;
}
e=2*dy-dx;
//decision variable
i=1;
do
{
dash=0;
if(dash<5)
{
while(e>=0)
{
if(ex==1)
{
x = x + s1;
}
else
{
y = y + s2;
}
e = e
+ 2*dy - 2*dx;
}
if(ex==1)
{
y = y + s2;
}
else
{
x = x + s1;
}
e = e
+ 2*dy;
pt::setco(x,y);
pt::draw();
i = i
+ 1;
dash =
dash +1;
}
else
{
x = x
+ 2*s1;
y = y
+ 2*s2;
}
}while(i<=dx);
}
void drawdo()
//Dotted Line
{
float
x,y,dx,dy,e,temp;
int
i,s1,s2,ex, dot=0;
dx=abs(x2-xco);
dy=abs(y2-yco);
x=xco;
y=yco;
pt::setco(x,y);
pt::draw();
if(x2 >
xco) //sign() function
{
s1=1;
}
if(x2 <
xco)
{
s1=-1;
}
if(x2 == xco)
{
s1=0;
}
if(y2 >
yco)
{
s2=1;
}
if(y2 <
yco)
{
s2=-1;
}
if(y2 == yco)
{
s2=0;
}
if(dy >
dx)
{
temp =
dx;
dx =
dy;
dy =
temp;
ex =
1;
}
else
{
ex=0;
}
e=2*dy-dx;
//decision variable
i=1;
do
{
dot=0;
while(dot<5)
{
while(e>=0)
{
if(ex==1)
{
x = x + s1;
}
else
{
y = y + s2;
}
e = e
+ 2*dy - 2*dx;
}
if(ex==1)
{
y = y + s2;
}
else
{
x = x + s1;
}
e = e
+ 2*dy;
pt::setco(x,y);
pt::draw();
i = i
+ 1;
dot =
dot +1;
}
}while(i<=dx);
}
void drawth(int
x1,int y1, int x2, int y2,int colour ) //Thick Line
{
float
x,y,dx,dy,e,temp;
int
i,s1,s2,ex;
dx=abs(x2-xco);
dy=abs(y2-yco);
x=xco;
y=yco;
pt::setco(x,y);
pt::draw();
if(x2 >
xco) //sign() function
{
s1=1;
}
if(x2 <
xco)
{
s1=-1;
}
if(x2 == xco)
{
s1=0;
}
if(y2 >
yco)
{
s2=1;
}
if(y2 <
yco)
{
s2=-1;
}
if(y2 == yco)
{
s2=0;
}
if(dy >
dx)
{
temp =
dx;
dx =
dy;
dy =
temp;
ex =
1;
}
else
{
ex=0;
}
e=2*dy-dx;
//decision variable
i=1;
do
{
while(e>=0)
{
if(ex==1)
{
x = x + s1;
}
else
{
y = y + s2;
}
e = e
+ 2*dy - 2*dx;
}
if(ex==1)
{
y = y + s2;
}
else
{
x = x + s1;
}
e = e
+ 2*dy;
pt::setco(x,y);
pt::draw();
i = i
+ 1;
}while(i<=dx); }
};
int main()
{
int
gd=DETECT,gm=VGAMAX;
int i,
ch,x1,y1,x2,y2, dx,dy,xmax,ymax,xmid,ymid,wx,wy,th;
char a;
initgraph(&gd,&gm,NULL);
dline ls;
xmax =
getmaxx();
ymax =
getmaxy();
xmid = xmax
/2;
ymid = ymax
/2;
line(xmid,0,xmid,ymax); //Y co-ordinate
line(0,ymid,xmax,ymid); //X co-ordinate
do
{ xmax =
getmaxx();
ymax =
getmaxy();
xmid = xmax
/2;
ymid = ymax
/2;
cout<<"\nEnter
Line Styles";
cout<<"\n1.SIMPLE..";
cout<<"\n2.DASH..";
cout<<"\n3.DOTTED..";
cout<<"\n4.THICK..";
cout<<"\n5.EXIT..";
cout<<"\nEnter
your choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
ls.drawsi();
break;
case 2:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
ls.drawda();
break;
case 3:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
ls.drawdo();
break;
case 4:
cout<<"\n
Enter x1: "; cin>>x1;
cout<<"\n
Enter y1: "; cin>>y1;
cout<<"\n
Enter x2: "; cin>>x2;
cout<<"\n
Enter y2: "; cin>>y2;
cout<<"Enter
Thickness: ";
cin>>th;
ls.drawth(x1+xmid,ymid-y1,x2+xmid,ymid-y2,15);
if((y2-y1)/(x2-x1) <1)
{
wy=(th-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*abs(x2-x1));
for(i=0;i<wy;i++)
{
ls.drawth(x1+xmid,ymid-y1-i,x2+xmid,ymid-y2-i,15);
ls.drawth(x1+xmid,ymid-y1+i,x2+xmid,ymid-y2+i,15);
}
}
else
{
wx=(th-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*abs(y2-y1));
for(i=0;i<wx;i++)
{
ls.drawth(x1+xmid-i,ymid-y1,x2+xmid-i,ymid-y2,15);
ls.drawth(x1+xmid+i,ymid-y1,x2+xmid+i,ymid-y2,15);
}
}
break;
case 5:
exit;
break;
}
cout<<"\nDO
U Want To Continue y OR n: ";
cin>>a;
}while(a!='n');
delay(3000);
getch();
closegraph();
return 0;
}