Professional Documents
Culture Documents
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
y,
y,
y,
y,
color);
color);
color);
color);
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";
}
else if (c == '0') break;
else {
cout << "Ban nhap sai! Moi nhap lai!";
system("pause");
};
}
return 0;
}