MultiRectanglex,y,n,R; getch; // Dong do hoa closegraph; } 3.vẽ đường tròn midpoint... Đây là code bài vẽ đường tròn bằng thuật toán MICHENER ,cho phép nhập toạ độ tâm và bán kính,sao đó
Trang 11.hình vuông!
#include <stdio.h>
#include <iostream>
#include <graphics.h>
using namespace std;
void MPCircle(int xc,int yc, int R){
int x,y,c,p;
c=getcolor();
p=1-R;
x=0;
putpixel(xc,yc,8);
for(y=R;x<=y;x++){
putpixel(x+xc,y+yc,c);putpixel(-x+xc,y+yc,c);
putpixel(x+xc,-y+yc,c);putpixel(-x+xc,-y+yc,c); putpixel(y+xc,x+yc,c);putpixel(-y+xc,x+yc,c);
putpixel(y+xc,-x+yc,c);putpixel(-y+xc,-x+yc,c); if(p<0) p+=2*x+3;
else{
p+=2*(x-y)+5;
// y ; bỏ đi
}
}
}
int main( )
{
initwindow( 640 , 480 , "WinBGIm" );
MPCircle(200,200,100);
cin.get();
closegraph( );
return( 0 );
}
Câu 2 : Viết chương trình nhập n số hình tròn hoặc hình vuông từ bàn phím Hãy vẽ đường tròn hoặc hình vuông tại giữa màn hình bằng giải thuật đệ quy
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
void Nhap(int &n)
{
printf("n=");
scanf("%d",&n);
}
void Init()
{
Trang 2int mh=DETECT,mode=0;
initgraph(&mh,&mode,"D:\\TC\\BGI");
if(graphresult()!=0)
exit(1);
}
// Ve nhieu Hinh vuong dong tam(x,y) va Canh=2R void MultiRectangle(int x,int y,int n,int R)
{
if(n==0) return;
else
{
setcolor(random(14)+1);
rectangle(x-n*R,y-n*R,x+n*R,y+n*R);
delay(100);
MultiRectangle(x,y,n-1,R);
}
}
// Ve nhieu hinh tron dong tam(x,y) va Ban kinh 2R void MultiCircle(int x,int y,int n,int R)
{
if(n==0) return;
else
{
setcolor(random(14)+1);
circle(x,y,R*n);
delay(100);
MultiCircle(x,y,n-1,R);
}
}
void main()
{
int n;
int R=20;
// Nhap
Nhap(n);
// Khoi tao do hoa
Init();
int x,y;
// Lay toa do tam man hinh
x=getmaxx()/2;
y=getmaxy()/2;
// Khoi tao random
randomize();
// Ve
Trang 3MultiRectangle(x,y,n,R);
getch();
// Dong do hoa
closegraph();
}
3.vẽ đường tròn midpoint
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<stdio.h>
void Mycircle(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();
}
Trang 4#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 );
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();
}
void hoanvi(int*xa,int *ya,int *xb,int *yb)
{
int tam;
tam=*xa;
*xa=*xb;
*xb=tam;
4.thuật toán Bresenham vẽ đường thẳng:
Trang 5tam=*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");
Trang 6}
//************* -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");
}
Trang 7// *********** 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");
}
5.vẽ đường thẳng = Bresenham:
//ve duong thang
#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++;
}
}
int gd=0,gm=0,xa,ya,xb,yb,c;
printf("Nhap toa do diem A: \n");
printf("Xa= ");scanf("%d",&xa);
Trang 8printf("\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();
}
Câu 2: Viết hàm vẽ đường tròn tâm I(x,y), bán kính R
Đây là code bài vẽ đường tròn bằng thuật toán MICHENER ,cho phép nhập toạ độ tâm và bán kính,sao đó so sánh với đường tròn được vẽ bằng hàm có sẵn trong đồ hoạ
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include<stdio.h>
void InitGraph()
{
int gd=DETECT,gm,err;
initgraph(&gd,&gm,"C:\\TC\\BGI");
}
void Michener(int xc,int yc,int R,int c)
{
int x,y,d;
d=3-2*R;
for(x=0,y=R;x<=y;x++)
{
putpixel(xc+x,yc+y,c);
putpixel(xc-x,yc+y,c);
putpixel(xc+x,yc-y,c);
putpixel(xc-x,yc-y,c);
putpixel(xc+y,yc+x,c);
putpixel(xc-y,yc+x,c);
putpixel(xc+y,yc-x,c);
putpixel(xc-y,yc-x,c);
if(d<0) d+=4*x+6;
else {
d+=4*x-4*y+10;
Trang 9y ;
}
}
}
void main()
{
int xo,yo,R;
InitGraph();
printf("nhap toa do tam duong tron xO:\t");
scanf("%d",&xo);
printf("nhap toa do tam duong tron yO:\t");
scanf("%d",&yo);
printf("nhap ban kinh duong tron:");
scanf("%d",&R);
settextstyle(2,HORIZ_DIR,4);
outtextxy(xo+R+10,yo+R+10,"hinh tron tam O ban kinh R");
Michener(xo,yo,R,1);
getch();
cleardevice();
//dung ham co san do hoa de so sanh
setcolor(12);
circle(xo,yo,R);
outtextxy(xo+R+10,yo+R+10,"hinh tron tam O ban kinh R
ve bang ham co san");
getch();
closegraph();
}
Câu 1: Dùng thuật toán Breseham để vẽ đoạn thẳng nối 2 điểm
A(3,7); B(6,17)
Giải:
Ta có: Δx = 6 - 3 = 3; Δy = 17 - 7 = 10;
Trường hợp này hệ số góc m>1 Ta đổi vai trò của x,y
e1 = 2Δx - Δy = -4;
ekt = 2Δx = 6;
et = 2Δx - 2Δy = -14;
Áp dụng:
y 7 8 9 10 11 12 13 14 15 16 17
x 3 3 4 4 4 5 5 5 5 6 6
Trang 10e -4 2 -12 -6 0 -14 -8 -2 4 -10 -4
Code thi giữa kỳ đồ họa máy tính
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
void Nhap(int &n)
{
printf("n=");
scanf("%d",&n);
}
{
int mh=DETECT,mode=0;
initgraph(&mh,&mode,"D:\\TC\\BGI");
if(graphresult()!=0)
exit(1);
}
// Ve nhieu Hinh vuong dong tam(x,y) va Canh=2R void MultiRectangle(int x,int y,int n,int R)
{
if(n==0) return;
else
{
setcolor(random(14)+1);
rectangle(x-n*R,y-n*R,x+n*R,y+n*R);
delay(100);
MultiRectangle(x,y,n-1,R);
}
}
// Ve nhieu hinh tron dong tam(x,y) va Ban kinh 2R void MultiCircle(int x,int y,int n,int R)
{
if(n==0) return;
else
{
setcolor(random(14)+1);
circle(x,y,R*n);
delay(100);
MultiCircle(x,y,n-1,R);
}
}
{
int n;
Trang 11int R=20;
// Nhap
Nhap(n);
// Khoi tao do hoa
Init();
int x,y;
// Lay toa do tam man hinh
x=getmaxx()/2;
y=getmaxy()/2;
// Khoi tao random
randomize();
// Ve
MultiRectangle(x,y,n,R);
getch();
// Dong do hoa
closegraph();
}
Code sin cos tan
Code này có thể vẽ ở bất kỳ đâu ko như code của mấy bác kia khó áp dụng
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
int maxX, maxY;
int gd = DETECT, gm;
initgraph(&gd, &gm, " \\BGI");
maxX = getmaxx();
maxY = getmaxy();
}
// sx,sy dx,dy toa do hinh chu nhat ma do thi se ve trong d
o
void paint_Sin_Graph(int sx, int sy, int dx, int dy, int ch uky = 1, int color =
WHITE) {
int i, xpos, ypos, yconst, range = (dy - sy) / 2;
// range tan so khuyeck dai tuong ung voi nua chieu doc float angle = 0, speed;
xpos = sx;
yconst = sy + ((dy - sy) / 2);
//lay tao do diem giua cua khung
speed = ((2 * M_PI) / (dx - sx)) * chuky;
Trang 12//ve 1 chuky 2pi tuong ung voi chieu ngang, n chu ky th
i nhan them cho so chu ky
// ve truc toa do
setcolor(RED);
line(sx, sy, sx, dy);
line(xpos, yconst, dx, yconst);;
outtextxy(sx - 15, sy, "1");
outtextxy(sx - 15, dy, "-1");
outtextxy(xpos - 15, yconst, "0");
setcolor(color);
for (xpos = sx; xpos < dx; xpos++) {
angle += speed;
ypos = yconst + (sin( angle)) *range;
putpixel(xpos, ypos, color);
delay(10);
}
settextjustify(1, 1);
//nham dua vi tri dong chu vao giua
outtextxy(sx + (dx
-sx) / 2, dy + 2, "do thi ham sin");
}
// sx,sy dx,dy toa do hinh chu nhat ma do thi se ve trong d
o
void paint_Cos_Graph(int sx, int sy, int dx, int dy, int ch uky = 1, int color =
WHITE) {
int i, xpos, ypos, yconst, range = (dy - sy) / 2;
// range tan so khuyeck dai tuong ung voi nua chieu doc float angle = 0, speed;
xpos = sx;
yconst = sy + ((dy - sy) / 2);
//lay tao do diem giua cua khung
speed = ((2 * M_PI) / (dx - sx)) * chuky;
//ve 1 chuky 2pi tuong ung voi chieu ngang, n chu ky th
i nhan them cho so chu ky
// ve truc toa do
setcolor(RED);
line(sx, sy, sx, dy);
line(xpos, yconst, dx, yconst);
outtextxy(sx - 15, sy, "1");
outtextxy(sx - 15, dy, "-1");
outtextxy(xpos - 15, yconst, "0");
setcolor(color);
for (xpos = sx; xpos < dx; xpos++) {
angle += speed;
ypos = yconst + (cos( angle)) *range;
Trang 13putpixel(xpos, ypos, color);
delay(10);
}
settextjustify(1, 1);
//nham dua vi tri dong chu vao giua
outtextxy(sx + (dx
-sx) / 2, dy + 2, "do thi ham cos");
}
// sx,sy dx,dy toa do hinh chu nhat ma do thi se ve trong d
o
void paint_Tan_Graph(int sx, int sy, int dx, int dy, int ch uky = 1, int color =
WHITE) {
int i, xpos, ypos, yconst, range = (dy - sy) / 10;
// range tan so khuyeck dai do tan chay tu
-votan->+votan nen tan so khuyech dai cang nho cang tot
float angle = 0, speed;
xpos = sx;
yconst = sy + ((dy - sy) / 2);
//lay tao do diem giua cua khung
speed = ((2 * M_PI) / (dx - sx)) * chuky;
//ve 1 chuky 2pi tuong ung voi chieu ngang, n chu ky thi nhan them cho so chu ky
// ve truc toa do
setcolor(RED);
line(sx, sy, sx, dy);
line(xpos, yconst, dx, yconst);
outtextxy(xpos - 15, yconst, "0");
setcolor(color);
for (xpos = sx; xpos < dx; xpos++) {
angle += speed;
ypos = yconst + (tan( angle)) *range;
if (ypos > dy || ypos < sx)
continue;
putpixel(xpos, ypos, color);
delay(10);
}
settextjustify(1, 1);
//nham dua vi tri dong chu vao giua
outtextxy(sx + (dx - sx) / 2, dy, "do thi ham tan"); }
paint_Sin_Graph(20, 40, maxX, maxY / 2, 4);
getch();
Trang 14paint_Cos_Graph(20, 20, maxX / 2, maxY - 20, 2);
getch();
paint_Tan_Graph(20, 20, maxX / 2, maxY / 2, 2);
getch();
}
code hàm sin
#include <conio.h>
#include <graphics.h>
#include <math.h>
//=============
int gd=DETECT, gm,x,y;
initgraph(&gd,&gm," \\BGI");
//ve bang tung diem
outtextxy(10,10,"Ve bang POINT:");
for(x=0;x<=628;x++){// -pi -> pi
y=240+(int)(sin((x-314.0)/100.0)*200.0);
putpixel(x,y,15);
}
getch();
//ve bang doan thang
outtextxy(10,10,"Ve bang LINE:");
x=0;
y=240+(int)(sin((x-314.0)/100.0)*200.0);
moveto(x,y);
for(x=1;x<=628;x++){// -pi -> pi
y=240+(int)(sin((x-314.0)/100.0)*200.0);
lineto(x,y);
}
getch();
}
code hàm sin
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>