code đồ họa vẽ đường tròn
vẽ đường tròn bằng các thuật toán làm tròn số,bresenham ,midpoint :#include<conio.h>
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<graphics.h>
using namespace std;
void ktdh()
{
int gd=0,gm;
initgraph(&gd,&gm,"");
}
void nhap(int &x,int &y,int &R)
{
cout<<"Nhap vao toa do tam O\n";
cout<<"nhap x = ";
cin>>x;
cout<<"Nhap y = ";
cin>>y;
cout<<"Nhap vao ban kinh duong tron : ";
cin>>R;
}
void put8pixel(int x0,int y0,int x,int y)
{
putpixel(x+x0,y+y0,5);
putpixel(y+x0,x+y0,5);
putpixel(-x+x0,-y+y0,5);
putpixel(-y+x0,-x+y0,5);
putpixel(-x+x0,y+y0,5);
putpixel(-y+x0,x+y0,5);
putpixel(x+x0,-y+y0,5);
putpixel(y+x0,-x+y0,5);;
}
void lamtronso(int x0,int y0,int R)
{
int x,y;
x=0;y=R;
while(x<=y)
{
y=round(sqrt(R*R-x*x));
put8pixel(x0,y0,x,y);
x++;
}
}
void bresenham(int x0,int y0,int R)
{
int x,y;
x=0;y=R;
int p=3-2*R;
while(x<=y)
{
if(p<0) p=p+4*x-6;
else{
y--;
p=p+4*(x-y+10);
}
put8pixel(x0,y0,x,y);
x++;
}
}
void midpoint(int x0,int y0,int R)
{
int x,y;
x=0;y=R;
float P=5/4-R;
cout<<P;
while(x<=y)
{
if(P<0) P=P+2*x+3;
else{
y--;
P=P+2*(x-y)+5;
}
put8pixel(x0,y0,x,y);
x++;
}
}
int main()
{
ktdh();
lamtronso(200,200,50);
bresenham(200,200,100);
midpoint(200,200,150);
getch();
closegraph();
}
hi
Trả lờiXóa