0% found this document useful (0 votes)
31 views

Chuong Trinh

The document describes algorithms for drawing circles and lines in graphics. It includes: 1) The Midpoint circle algorithm for drawing circles using integer coordinates and increments. 2) Bresenham's line algorithm for drawing lines in a raster display. It presents optimized versions for lines with slopes between 0-1, 1 and above, -1 to 0, and below -1. 3) A circle drawing algorithm attributed to Michener that optimizes the Midpoint circle algorithm.

Uploaded by

One Click Login
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Chuong Trinh

The document describes algorithms for drawing circles and lines in graphics. It includes: 1) The Midpoint circle algorithm for drawing circles using integer coordinates and increments. 2) Bresenham's line algorithm for drawing lines in a raster display. It presents optimized versions for lines with slopes between 0-1, 1 and above, -1 to 0, and below -1. 3) A circle drawing algorithm attributed to Michener that optimizes the Midpoint circle algorithm.

Uploaded by

One Click Login
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

<thuat toan midpoint ve duong tron>

#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<stdio.h>
void Mcircle(int xo,int yo,int R)
{ int x,y,p,c;
setviewport(xo,yo,xo+R,yo+R,0);
p=1-R;
x=0;y=R;c=getcolor();
while(x<=y)
{ putpixel(x,y,c);putpixel(-x,y,c);
putpixel(x,-y,c);putpixel(-x,-y,c);
putpixel(y,x,c);putpixel(y,-x,c);
putpixel(-y,x,c);putpixel(-y,-x,c);
if(p<0) p+=2*x+3;
else {p+=2*(x-y)+5;y--;}
x++;
}
}
void main()
{
int gd=0,gm,R,x,y;
initgraph(&gd,&gm,"F:\\learn\\TC\\BGI");
printf("nhap x=");scanf("%d",&x);
printf("nhap y=");scanf("%d",&y);
printf("nhap R=");scanf("%d",&R);
setcolor(3);
Mcircle(x,y,R);
getch();
closegraph();
}

//ve duong thang thuat toan Bresenham


#include <conio.h>
#include<graphics.h>
#include <stdio.h>
void Bresline(int xa,int ya,int xb,int yb,int c){
int dx,dy,edy,e,esi,x,y;
dx=xb-xa;
dy=yb-ya;
edy=dy+dy;//2*delta(y)
e=edy-dx;//e1
esi=e-dx;//2*delta(y)-2*delta(x)
x=xa;
y=ya;
while(x<=xb){
putpixel(x,y,c);
if(e<0) e+=edy;
else { e+=esi;
y++;
}
x++;
}
}
void main(){
int gd=0,gm=0,xa,ya,xb,yb,c;
printf("Nhap toa do diem A: \n");
printf("Xa= ");scanf("%d",&xa);
printf("\nYa= ");scanf("%d",&ya);
printf("\nNhap toa do diem B: \n");
printf("Xa= ");scanf("%d",&xb);
printf("\nYa= ");scanf("%d",&yb);
printf("\n Nhap mau: ");scanf("%d",&c);
initgraph(&gd,&gm,"F:\\learn\\tc\\bgi");
Bresline(xa,ya,xb,yb,c);
line(xa+5,ya,xb+5,yb);
getch();
closegraph();
}

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void hoanvi(int *,int *,int *,int
void Bresenham1(int,int ,int ,int
void Bresenham2(int,int ,int ,int
void Bresenham3(int,int ,int ,int
void Bresenham4(int,int ,int ,int
void Bresenham5(int,int ,int ,int
void main() {

*);
);
);
);
);
);

int gd=0,gm,xa,ya,xb,yb,xmax,ymax;
float m;
clrscr();
printf("\nnhap toa do A: ");
scanf("%d%d",&xa,&ya);
printf("\nnhap toa do B:");
scanf("%d%d",&xb,&yb);
if((xb-xa)!=0)
m=(yb-ya)/float(xb-xa);
printf("\nm= %f",m);
getch();
initgraph(&gd,&gm,"D:\\TC\\BGI");
setcolor(4);
if(xa>xb) hoanvi(&xa,&ya,&xb,&yb);
if(m>0&&m<1)
Bresenham1(xa,ya,xb,yb);
if(m>1)
Bresenham2(xa,ya,xb,yb);
if(m>-1&&m<0)
Bresenham3(xa,ya,xb,yb);
if(m<-1)
Bresenham4(xa,ya,xb,yb);
if(xa==xb||ya==yb||m==1)
Bresenham5(xa,ya,xb,yb);
getch();
closegraph();
}
void hoanvi(int*xa,int *ya,int *xb,int *yb)
{
int tam;
tam=*xa;
*xa=*xb;
*xb=tam;
tam=*ya;
*ya=*yb;
*yb=tam;
}
// ********* 0<M<1****************
void Bresenham1(int xa,int ya,int xb,int yb)
{
int x,y,dx,dy,e,ekt,et,c;
dx = xb-xa;
dy=yb-ya;
ekt=dy+dy;
e=ekt-dx;
et=e-dx;
c=getcolor();
for(x=xa,y=ya;x<=xb;x++){
putpixel(x,y,c);
if(e<0)
e+=ekt;
else {
y++;
e+=et;
}
}
outtextxy(200,200,"truonghop1");
}

//***********M>1*******************
void Bresenham2(int xa,int ya,int xb,int yb)
{
int x,y,dx,dy,e,ekt,et,c;
dx = xb-xa;
dy=yb-ya;
ekt=dx+dx;
e=ekt-dy;
et=e-dy;
c=getcolor();
for(x=xa,y=ya;x<=xb;y++){
putpixel(x,y,c);
if(e<0)
e+=ekt;
else {
x++;
e+=et;
}
}
}

outtextxy(200,200,"truonghop2");

//************* -1<M<0 ************


void Bresenham3(int xa,int ya,int xb,int yb)
{
int x,y,dx,dy,e,ekt,et,c;
dx = xb-xa;
dy=yb-ya;
ekt=-(dy+dy);
e=ekt-dx;
et=e-dx;
c=getcolor();
for(x=xa,y=ya;x<=xb;x++){
putpixel(x,y,c);
if(e<0)
e+=ekt;
else {
y--;
e+=et;
}
}
outtextxy(200,200,"truonghop3");
}
// *********** M<-1 ******************
void Bresenham4(int xa,int ya,int xb,int yb)
{
int x,y,dx,dy,e,ekt,et,c;
dx = xb-xa;
dy=yb-ya;
ekt=dx+dx;
e=ekt+dy;
et=e+dy;
c=getcolor();
for(x=xa,y=ya;y>=xb;y--){
putpixel(x,y,c);
if(e<0)
e+=ekt;
else {
x++;
e+=et;
}
}
outtextxy(200,200,"truonghop4");
}
// *********** TH khac ****************
void Bresenham5(int xa,int ya,int xb,int yb)
{

int x,y,c;
c=getcolor();
if(xa==xb){
for(y=ya;y<=yb;y++)
putpixel(xa,y,c);
}
if(ya==yb){
for(x=xa;x<=xb;x++)
putpixel(x,ya,c);
}
if((xb-xa)/(yb-ya)==1) {
for(x=xa,y=ya;x<=xb;x++,y++)
putpixel(x,y,c);
}
outtextxy(200,200,"truonghop5");

Thut ton Michener k ng trn :


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void marde(int a,int b,int r);
void main()
{
int r,c,gd=0,gm,a,b;
printf("\nnhap tao do tam : ");
scanf("%d%d",&a,&b);
printf("\nnhap ban kinh r: ");
scanf("%d",&r);
initgraph(&gd,&gm,"D:\\TC\\BGI");
marde(a,b,r);
circle(a,b,r);
getch();
closegraph();
}
void marde(int a,int b,int r)
{
int x,y,d,c;
d=3-2*r;
x=0;y=r;
c=getcolor();
while(x<=y) {
putpixel(x+a,y+b,c);
putpixel(-x+a,y+b,c);
putpixel(x+a,-y+b,c);
putpixel(-x+a,-y+b,c);
putpixel(y+b,x+a,c);
putpixel(y+b,-x+a,c);
putpixel(-y+b,x+a,c);
putpixel(-y+b,-x+a,c);
if(d<0) d+=4*x+6;
else {
d+=4*(x-y)+10;
y--;
}
x++;
}
}

You might also like