You are on page 1of 10

#include "graphics.

h"
#include <iostream>
#pragma comment(lib, "graphics.lib")
using namespace std;
// hm lm trn s
int Round(float y) { return (int)(y + 0.5); }
// hm v ng thng bng thut ton DDA. Ta quy c: x1 <= x2
void LineDDA(int x1, int y1, int x2, int y2, int color)
{
float m = (float)(y2 - y1) / ((x2 - x1));
putpixel(x1, y1, color);
/*Trng hp 0<m<1 v -1<m<0
Do c 2 trng hp th khong bin thin ca x ln hn khong bin thin ca
Nn ta s cho vng lp for chy t x1 n x2.
Tnh y theo x nh sau: Y(i+1) = Y(i) + m
*/
if ((m>0 && m <1) || (m>-1 && m<0)){
float y = y1;
for (int x = x1; x<x2; x++)
{
y += m;
putpixel(x, Round(y), color);
}
}
/*Trng hp m > 1 v m < -1
Do c 2 trng hp th khong bin thin ca y ln hn khong bin thin ca
Nn ta s cho vng lp for chy t y1 n y2.
Tnh x theo y nhu sau:
m > 1: X(i+1) = Xi + 1/m
m <-1: X(i+1) = Xi - 1/m
*/
else if (m>1)
{
float x = x1;
float tam = 1 / m;
for (int y = y1; y<y2; y++)
{
x += tam;
putpixel(Round(x), y, color);
}
}
else if (m < -1)
{
float x = x1;
float tam = 1 / m;
for (int y = y1; y>y2; y--)
{
x -= tam;
putpixel(Round(x), y, color);
}
}
// Trng hp c bit: khi y = x + b

else if (m == 1)
{
int y = y1;
for (int x = x1; x< x2; x++)
{
y += 1;
putpixel(x, y, color);
}
}
// Trng hp c bit: khi y = -x + b
else if (m == -1)
{
float y = y1;
for (int x = x1; x< x2; x++)
{
y -= 1;
putpixel(x, y , color);
}
}
// Trng hp c bit: khi ng thng song song vi Oy -> x = a
else if(x1 == x2)
{
for (int y = y1; y < y2; y++)
putpixel(x1, y, color);
}
// Trng hp c bit: khi ng thng song song vi Ox -> y = b
else if (y1 == y2){
for (int x = x1; x < x2; x++)
putpixel(x, y1, color);
}
}
// hm v ng thng bng thut ton Bresenham
void LineBresenham(int x1, int y1, int x2, int y2, int color)
{
int Dx = x2 - x1;
int Dy = y2 - y1;
int twoDx = 2 * Dx;
int twoDy = 2 * Dy;
int twoDyDx = 2 * (Dy - Dx);
int twoDxDy = 2 * (Dx - Dy);
int twoDxy = 2 * (Dx + Dy);
float m = (float)Dy / Dx;

// delta x
// delta y

/*Trng hp 0 < m < 1:


P ti im bt u (X1,Y1) l: P = 2*Dy - Dx
Ti Pi
Nu Pi < 0 th ta chn im Yi v P(i+1) = Pi + twoDy
Ngc li ta chn im Y(i+1) = Yi + 1 v P(i+1) = Pi + twoDyDx
Tng t cho trng hp m > 1, m < -1 v -1 < m < 0
*/
if (m > 0 && m < 1)
{
int y = y1;
int P = twoDy - Dx;

putpixel(x1, y1, color);


for (int x = x1+1; x < x2; x++)
{
if (P < 0) P += twoDy;
else
{
P += twoDyDx;
y++;
}
putpixel(x, y, color);
}
}
else if (m > 1)
{
int x = x1;
int P = twoDx - Dy;
putpixel(x1, y1, color);
for (int y = y1+1; y < y2; y++){
if (P < 0) P += twoDx;
else{
P += twoDxDy;
x++;
}
putpixel(x, y, color);
}
}
else if (m < 0 && m >-1)
{
int y = y1;
int P = (twoDy + Dx);
putpixel(x1, y1, color);
for (int x = x1+1; x < x2; x++)
{
if (P >= 0) P += twoDy;
else{
P += twoDxy;
y--;
}
putpixel(x, y, color);
}
}
else if (m < -1)
{
int x = x1;
int P = twoDx + Dy;
putpixel(x1, y1, color);
for (int y = y1 - 1; y > y2; y--)
{
if (P < 0) P += twoDx;
else{
P += 2 * (Dx + Dy);
x++;
}
putpixel(x, y, color);
}
}

// Trng hp c bit: khi y = x + b


else if (m == 1)
{
int y = y1;
for (int x = x1; x< x2; x++)
{
y += 1;
putpixel(x, y, color);
}
}
// Trng hp c bit: khi y = -x + b
else if (m == -1)
{
float y = y1;
for (int x = x1; x< x2; x++)
{
y -= 1;
putpixel(x, y, color);
}
}
// Trng hp c bit: khi ng thng song song vi Oy -> x = a
else if (x1 == x2)
{
for (int y = y1; y < y2; y++)
putpixel(x1, y, color);
}
// Trng hp c bit: khi ng thng song song vi Ox -> y = b
else if (y1 == y2){
for (int x = x1; x < x2; x++)
putpixel(x, y1, color);
}
}
/*T 1 im c ta (x,y) thuc 1/8 th nht (tnh theo chiu kim ng h)
Ta ly i xng qua cc cung xc nh 7 im 7 cung 1/8 cn li.
Tnh tin cc im theo vecto OM v v chng ln mn hnh
*/
void Put8(int x0, int y0, int x, int y, int color)
{
putpixel(x0 + x, y0 + y, color);
putpixel(x0 - x, y0 + y, color);
putpixel(x0 + x, y0 - y, color);
putpixel(x0 - x, y0 - y, color);
putpixel(x0 + y, y0 + x, color);
putpixel(x0 - y, y0 + x, color);
putpixel(x0 + y, y0 - x, color);
putpixel(x0 - y, y0 - x, color);
}
void Put4(int x0,int y0,int
{
putpixel(x0 + x, y0
putpixel(x0 - x, y0
putpixel(x0 + x, y0
putpixel(x0 - x, y0
}

x,int y, int color)


+
+
-

y,
y,
y,
y,

color);
color);
color);
color);

/* Hm v ng trn bng thut ton Bresenham


tng: V ng trn (C1): tm M(x0,y0), bn knh R
- Bc 1: V ng trn (C) c: tm O(0,0) v bn knh R.
- Bc 2: Tnh tin tm ng trn (C) v mi im trn ng trn (C) theo vecto OM
*/
void CircleBresenham(int x0, int y0, int R, int color)
{
int p = 3 - 2 * R; int x = 0; int y = R;
Put8(x0, y0, x, y, color);
while (x < y)
{
x++;
if (p < 0) p += 4 * x + 6;
else {
y--;
p += 4 * (x - y) + 10;
}
Put8(x0, y0, x, y, color);
delay(50);
}
}
/*Hm v ng trn bng thut ton Mid-point*/
void CircleMidPoint(int x0, int y0, int R, int color)
{
int x = 0, y = R, F = 1 - R;
Put8(x0, y0, x, y, color);
while (x < y)
{
x++;
if (F < 0){
F+= 2*x + 3;
}
else{
F += 2 * (x - y) + 5;
y -= 1;
}
Put8(x0, y0, x, y, color);
delay(50);
}
}
/*Hm v ng elip bng thut ton midpoint*/
void ElipMidPoint(int x0, int y0, int a, int b, int color)
{
int x = 0, y = b;
int b2 = b*b, a2 = a*a;
double f = b2 - a2*b + (a2 / 4);
Put4(x0, y0, x, y, color);
while (b2*x < a2*y)
{
x++;
if (f < 0){
f += b2*(x + x + 3);
}
else{

y--;
f += b2*(x + x + 3) + a2*(2 - y - y);
}
Put4(x0, y0, x, y, color);
delay(50);
}
f = b2*(x + 0.5)*(x + 0.5) + a2*(y - 1)*(y - 1) - a2*b2;
while (y > 0)
{
y--;
if (f>=0){
f += a2*(3-2*y);
}
else{
x++;
f += 2 * b2*(x + 1) + a2*(3 - 2 * y);
}
Put4(x0, y0, x, y, color);
delay(50);
}
}
/*
- Hm v ng elip bng thut ton Bresenham
- Vng 2 c bt u v t im (a,0)
*/
void ElipBresenham1(int x0, int y0, int a, int b, int color)
{
int x = 0, y = b;
int b2 = b*b, a2 = a*a;
double c1 = 2 * ((double)b2 / (double)a2);
double c2 = 2 * ((double)a2 / (double)b2);
double p = c1 - 2 * b + 1;
Put4(x0, y0, x, y, color);
while (b2*x < a2*y)
{
x++;
if (p < 0){
p += c1 *(2 * x + 3);
}
else{
p += c1 *(2 * x + 3) - 4 * y;
y--;
}
Put4(x0, y0, x, y, color);
delay(50);
}
p = c2 - 2 * a + 1;
y = 0, x = a;
Put4(x0, y0, x, y, color);
while (b2*x >= a2*y)
{
y++;
if (p < 0){

p += c2 *(2 * y + 3);
}
else{
x--;
p += c2 *(2 * y + 3) - 4 * x;
}
Put4(x0, y0, x, y, color);
delay(50);
}
}
/*
- Hm v ng elip bng thut ton Bresenham
- im u vng 2 c v ni tip t im cui vng 1
*/
void ElipBresenham(int x0, int y0, int a, int b, int color)
{
int x = 0, y = b;
int b2 = b*b, a2 = a*a;
double c1 = 2 * ((double)b2 / (double)a2);
double c2 = 2 * ((double)a2 / (double)b2);
double p = c1 - 2 * b + 1;
Put4(x0, y0, x, y, color);
while (b2*x < a2*y)
{
x++;
if (p < 0){
p += c1 *(2 * x + 3);
}
else{
p += c1 *(2 * x + 3) - 4 * y;
y--;
}
Put4(x0, y0, x, y, color);
delay(50);
}
p = c2*(y - 1)*(y - 1) + (x + 1)*(x + 1) + x*x - 2 * a2;
while (y>0)
{
y--;
if (p >= 0){
p += c2 *(-2 * y + 3);
}
else{
p += c2 *(-2 * y + 3) + 4*(x+1);
x++;
}
Put4(x0, y0, x, y, color);
delay(50);
}
}
void FloodFill4(int x, int y, int bc, int nc)
{
int color; color = getpixel(x, y);
if ((color != bc) && (color != nc))
{

putpixel(x, y, nc);
delay(1);
FloodFill4(x - 1, y,
FloodFill4(x + 1, y,
FloodFill4(x, y - 1,
FloodFill4(x, y + 1,

bc,
bc,
bc,
bc,

nc);
nc);
nc);
nc);

}
}
void FloodFillD(int x, int y, int bc, int nc)
{
int color; color = getpixel(x, y);
if ((color != bc) && (color != nc))
{
putpixel(x, y, nc);
delay(5);
FloodFillD(x + 1, y + 1, bc, nc);
FloodFillD(x + 1, y - 1, bc, nc);
FloodFillD(x - 1, y - 1, bc, nc);
FloodFillD(x - 1, y + 1, bc, nc);
}
}
void FloodFill8(int x, int y, int bc, int nc)
{
int color;
color = getpixel(x, y);
if ((color != bc) && (color != nc))
{
putpixel(x, y, color);
FloodFill4(x, y, bc, nc);
FloodFillD(x, y, bc, nc);
}
}
void FillCircle(int Xc, int Yc, int R, int bc, int nc)
{
CircleBresenham(Xc, Yc, R, bc);
FloodFill4(Xc, Yc, bc, nc);
}
int main()
{
int gd = DETECT, gm;
int x0,y0,x1, y1, x2, y2, R, a, b;
char c;
while (true)
{
system("cls");
cout << "\n\n\n\t\t\t\tBAI TAP THUC HANH 1 \n\t\t( Ban nhap mot
so de chon chuc nang phu hop)\n"<<endl;
cout << "\t1. Ve duong thang bang thuat toan DDA\n";
cout << "\t2. Ve duong thang bang thuat toan Bresenham\n";
cout << "\t3. Ve duong tron bang thuat toan Bresenham\n";
cout << "\t4. Ve duong elip bang thuat toan Midpoint\n";
cout << "\t0. Thoat\n";
cin >> c;
if (c == '1'){
cout << "\n\t Ve duong thang bang thuat toan DDA\n\n";

cout << "Moi nhap toa do x1, y1: ";


cin >> x1 >> y1;
cout << "Moi nhap toa do x2, y2: ";
cin >> x2 >> y2;
cout << "\nVe hinh";
initgraph(&gd, &gm, "c:\\tc\\bgi");

// Do quy c v x1 v x2. Nn nu x1 >= x2 th ta thay chuyn ta


nhau. Tng t i vi hm LineBresenham
if (x1 < x2)
LineDDA(x1,y1,x2,y2, WHITE);
else LineDDA(x2, y2, x1, y1, WHITE);
getch();
closegraph();
}
else if (c == '2'){
cout << "\n\t Ve duong thang bang thuat toan Bresenham\n
\n";
cout << "Moi nhap toa do x1, y1: ";
cin >> x1 >> y1;
cout << "Moi nhap toa do x2, y2: ";
cin >> x2 >> y2;
cout << "\nVe hinh";
initgraph(&gd, &gm, "c:\\tc\\bgi");
if (x1 < x2)
LineBresenham(x1, y1, x2, y2, WHITE);
else LineBresenham(x2, y2, x1, y1, WHITE);
getch();
closegraph();
}
else if (c == '3'){
cout << "\n\t Ve duong tron bang thuat toan Bresenham\n\
n";
//cout << "Moi nhap toa do tam duong tron: ";
//cin >> x0 >> y0;
//cout << "Moi nhap ban kinh: ";
//cin >> R;
cout << "\nVe hinh";
initgraph(&gd, &gm, "c:\\tc\\bgi");
//CircleBresenham(250, 250, 200, RED);
FillCircle(250, 250,50, BLUE, WHITE);
//CircleMidPoint(250, 250, 200, RED);
getch();
closegraph();
}
else if (c == '4'){
cout << "\n\t Ve duong elip bang thuat toan Midpoint\n\n
";
//cout << "Moi nhap du lieu cho elip: ";
//cin >> x0>>y0>>a >> b;
cout << "\nVe hinh";
initgraph(&gd, &gm, "c:\\tc\\bgi");
ElipMidPoint(200,200,150, 100, BLUE);
ElipBresenham(200, 200, 150, 100, RED);
getch();
closegraph();

}
else if (c == '0') break;
else {
cout << "Ban nhap sai! Moi nhap lai!";
system("pause");
};
}
return 0;
}

You might also like