Sie sind auf Seite 1von 6

Graphics primitives

In graphics, primitives are basic elements, such as lines, triangles, curves, and polygons, which you
can combine to create more complex graphical images. A primitive is a graphics object that is
essential for the creation or construction of complex images.
Pixel
 A pixel is a point of light.
 It is just one tiny dot on the raster displays.
 Though it has no structure, it is definitely a building block and hence it can be considered as the
graphics primitive.
 The resolution of CRT is related to the dot size, the diameter of a single dot.
 A resolution of 100 dots lines/inch implies a dot size of 0.01 inch.
 However, in reality, pixels are more elliptic than circle.
 The shape of a pixel purely depends upon the characteristics of the visual display unit.

Points
 Points are uniquely defined by their x- and y-coordinates
 Points are usually not drawn themselves
 Their main function is the description of other objects like lines that can be defined by their two
endpoints

Lines, Polylines or Curves


 These can be defined by two or more points
 For lines two points are needed
 Curves need two points and additional control points
 Polylines are connected sequences of lines

Areas
 Areas are bounded by closed polylines or polygons
 Areas can be filled with colour or a texture

Bresenham’s line code: DDA code:


#include<iostream.h> #include <graphics.h>
#include<graphics.h> #include <iostream.h>
void drawline(int x0, int y0, int x1, int y1) #include <math.h>
{ #include <dos.h>
int dx, dy, p, x, y; void main( )
dx=x1-x0; {
dy=y1-y0; float x,y,x1,y1,x2,y2,dx,dy,step;
x=x0; int i,gd=DETECT,gm;
y=y0; initgraph(&gd,&gm,"..\\bgi");
p=2*dy-dx; cout<<"Enter the value of x1 and y1 : ";
while(x<x1) cin>>x1>>y1;
{ cout<<"Enter the value of x2 and y2: ";
if(p>=0) cin>>x2>>y2;
{ dx=abs(x2-x1);
putpixel(x,y,7); dy=abs(y2-y1);

Page 1 of 6
y=y+1; if(dx>=dy)
p=p+2*dy-2*dx; step=dx;
} else
else step=dy;
{ dx=dx/step;
putpixel(x,y,7); dy=dy/step;
p=p+2*dy; x=x1;
} y=y1;
x=x+1; i=1;
} while(i<=step)
} {
int main() putpixel(x,y,5);
{ x=x+dx;
int gdriver=DETECT, gmode, error, x0, y0, x1, y1; y=y+dy;
initgraph(&gdriver, &gmode, "..\\bgi"); i=i+1;
cout<<"Enter co-ordinates of first point: "; delay(100);
cin>>x0>>y0; }
cout<<"Enter co-ordinates of second point: "; closegraph();
cin>>x1>>y1; }
drawline(x0, y0, x1, y1);
return 0;
}
Comparison
1) Arithmetic - DDA algorithm uses floating points i.e. Real Arithmetic. Bresenhams algorithm uses
fixed points i.e. Integer Arithmetic.
2) Operations - DDA algorithm uses multiplication and division in its operations. Bresenhams
algorithm uses only subtraction and addition in its operations.
3) Speed - DDA algorithm is rather slowly than Bresenhams algorithm in line drawing because it uses
real arithmetic (floating-point operations). Bresenhams algorithm is faster than DDA algorithm in line
drawing because it performs only addition and subtraction in its calculation and uses only integer arithmetic
so it runs significantly faster.
4) Accuracy Efficiency - DDA algorithm is not as accurate and efficient as Bresenham algorithm.
Bresenhams algorithm is more efficient and much accurate than DDA algorithm.
5) Drawing - DDA algorithm can draw circles and curves but that are not as accurate as Bresenhams
algorithm. Bresenhams algorithm can draw circles and curves with much more accuracy than DDA
algorithm.
6) Round Off - DDA algorithm round off the coordinates to integer that is nearest to the line.
Bresenhams algorithm does not round off but takes the incremental value in its operation.
7) Expensive - DDA algorithm uses an enormous number of floating-point multiplications so it is
expensive. Bresenhams algorithm is less expensive than DDA algorithm as it uses only addition and
subtraction.

Program to implement Circle Drawing Algorithm

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>

Page 2 of 6
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class myCircle
{
private:
int x,y,r,d,x1,y1;
public:
myCircle(); //Constructor
void showCircle();
};
myCircle::myCircle()
{
x=0;y=0;
cout<<"
"Enter The Co-Ordinates Of The Circle ":=";
cin>>x>>y;
cout<<"
"Enter The Radius Of The Circle ":=";
cin>>r;
}
void myCircle::showCircle()
{
char *s;
x1=0;y1=r;
d=3-2*r;
while(x1<=y1)
{
putpixel((x+x1+320),(y+y1+240),5);
putpixel((x-x1+320),(y+y1+240),5);
putpixel((x+x1+320),(y-y1+240),5);
putpixel((x-x1+320),(y-y1+240),5);
putpixel((x+y1+320),(y+x1+240),5);
putpixel((x-y1+320),(y+x1+240),5);
putpixel((x+y1+320),(y-x1+240),5);
putpixel((x-y1+320),(y-x1+240),5);
if(d<0)
d+=4*x1+6;
else
{
d+=4*(x1-y1)+10;
y1--;
}
x1++;
}
setcolor(5);
outtextxy(318+x,235+y,".");
setcolor(15);
sprintf(s,"Center(%d,%d)",x,y);
outtextxy(20,10,"The Center Is At");
outtextxy(20,20,s);
sprintf(s,"Radius=%d",r);

Page 3 of 6
outtextxy(20,30,s);
getch();
}
void main()
{
int gd=DETECT,gm,i,j,xx=190,xxx=480;
clrscr();
myCircle a;
char *mess[]={"B","R","E","S","E","N","H","A","M","'","S"," ","C","I","R","C","L","E"," ","A","L","G","O","R","I","T","H","M"};
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=27;i<16,j>=14;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showCircle();
// closegraph();
}

Program to implement DDA Circle Drawing Algorithm

#include<iostream.h>
#include<graphics.h>
#include<conio.h>

Page 4 of 6
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class myCircle
{
private:
float x,y,r,d,x1,y1;
public:
myCircle(); //Constructor
void showCircle();
int sign(int);
};

int myCircle::sign(int xx)


{
if(xx<0)
return -1;
if(xx==0)
return 0;
if(xx>0)
return 1;
return 0;
}
myCircle::myCircle()
{
x=0;y=0;
cout<<"
"Enter The Co-Ordinates Of The Circle ":=";
cin>>x1>>y1;
cout<<"
"Enter The Radius Of The Circle ":=";
cin>>r;
}

void myCircle::showCircle()
{
char *s;
int s1,s2,ic;
x=x1;y=y1;
float i=0;
while(i<=360)
{
x=x1+r*cos(i);
y=y1+r*sin(i);
putpixel((x+320),480-(y+240),6);
i+=0.5;
}
setcolor(15);
sprintf(s,"Center(%d,%d)",x1,y1);
outtextxy(40,10,"The Center Is At :=");
outtextxy(40,20,s);

Page 5 of 6
sprintf(s,"Radius=%g",r);
outtextxy(40,30,s);
getch();
}
void main()
{
int gd=DETECT,gm,i,j,xx=220,xxx=430;
clrscr();
myCircle a;
char *mess[]={"D","D","A"," ","C","I","R","C","L","E"," ","A","L","G","O","R","I","T","H","M"};
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=19;i<12,j>=10;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showCircle();
closegraph();
}

Page 6 of 6

Das könnte Ihnen auch gefallen