Thuật toán tô màu trong C++
March 07, 2023
Viết thuật toán tô màu trong C++
Bài Giải
/*
Name: Thuat toan to mau trong thu vien Graphics
Copyright: None
Author: Tran Anh
Description: https://gimi.vn
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
void VeHV(int TLx,int TLy, int BRx,int BRy, int c){
setcolor(c);
line(TLx,TLy,BRx,TLy);
line(TLx,BRy,BRx,BRy);
line(TLx,TLy,TLx,BRy);
line(BRx,TLy,BRx,BRy);
}
void ToMauHVScanLine(int TLx,int TLy, int BRx,int BRy, int c){
setcolor(c);
for(int y=TLy+50;y<BRy;y++){
line(TLx+1,y,BRx-1,y);
delay(10);
}
}
void ToMauPutPixel(int TLx,int TLy, int BRx,int BRy, int c){
for(int y=TLy+1;y<BRy;y++)
for(int x=TLx+1;x<BRx;x++){
putpixel(x,y,c);
delay(5);
}
}
void ToMauDuongTron(int xc,int yc, int R,int c){
int i=1;
while(1){
for(int y=yc-R;y<=yc+R;y++)
for(int x=xc-R;x<=xc+R;x++){
// Tu diem (x,y)-->tam (xc,yc)
float d=sqrt((x-xc)*(x-xc)+(y-yc)*(y-yc));
if(d<R){
if(x<xc)
putpixel(x,y,i);
else
putpixel(x,y,(i+1)%15);
}
//delay(5);
}
i=(i+1)%15;
}
}
void DDA(int xA,int yA,int xB,int yB,int color){
int dx=xB-xA;
int dy=yB-yA;
int step;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
float x_inc=(float)dx/step;
float y_inc=(float)dy/step;
float x=xA;
float y=yA;
putpixel(x,y,color);
for(int k=1;k<=step;k++){
x=x+x_inc;
y=y+y_inc;
putpixel(round(x),round(y),color);
delay(5);
}
}
typedef struct Dinh{
int x,y;
};
void VeDaGiac(Dinh d[],int n, int color){
for(int i=0;i<n;i++){
int j = (i+1)%n;
DDA(d[i].x,d[i].y,d[j].x,d[j].y,color);
}
}
void ToLan(int x,int y,int mauto,int maubien){
int mauht=getpixel(x,y);
if(mauht!=mauto && mauht!=maubien){
putpixel(x,y,mauto);
ToLan(x-1,y,mauto,maubien);
//ToLan(x+1,y,mauto,maubien);
//ToLan(x,y-1,mauto,maubien);
ToLan(x,y+1,mauto,maubien);
}
}
void ToLan2(int x,int y,int mauto,int maubien){
int mauht=getpixel(x,y);
if(mauht!=mauto && mauht!=maubien){
putpixel(x,y,mauto);
//ToLan2(x-1,y,mauto,maubien);
ToLan2(x+1,y,mauto,maubien);
ToLan2(x,y-1,mauto,maubien);
//ToLan2(x,y+1,mauto,maubien);
}
}
int main(){
initwindow(640,480);
// VeHV(200,200,400,400,15);
// ToMauHVScanLine(200,200,400,400,13);
// ToMauPutPixel(200,200,400,400,2);
// circle(320,240,100);
//ToMauDuongTron(320,240,150,13);
// ToLan(221,240,RED,WHITE);
Dinh d[]={{100,300},{150,200},{270,350},{300,200}};
VeDaGiac(d,4,15);
ToLan2(103,297,RED,WHITE);
ToLan(298,202,BLUE,WHITE);
// VeDaGiac(d,4,13);
getch();
return 0;
}
Name: Thuat toan to mau trong thu vien Graphics
Copyright: None
Author: Tran Anh
Description: https://gimi.vn
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
void VeHV(int TLx,int TLy, int BRx,int BRy, int c){
setcolor(c);
line(TLx,TLy,BRx,TLy);
line(TLx,BRy,BRx,BRy);
line(TLx,TLy,TLx,BRy);
line(BRx,TLy,BRx,BRy);
}
void ToMauHVScanLine(int TLx,int TLy, int BRx,int BRy, int c){
setcolor(c);
for(int y=TLy+50;y<BRy;y++){
line(TLx+1,y,BRx-1,y);
delay(10);
}
}
void ToMauPutPixel(int TLx,int TLy, int BRx,int BRy, int c){
for(int y=TLy+1;y<BRy;y++)
for(int x=TLx+1;x<BRx;x++){
putpixel(x,y,c);
delay(5);
}
}
void ToMauDuongTron(int xc,int yc, int R,int c){
int i=1;
while(1){
for(int y=yc-R;y<=yc+R;y++)
for(int x=xc-R;x<=xc+R;x++){
// Tu diem (x,y)-->tam (xc,yc)
float d=sqrt((x-xc)*(x-xc)+(y-yc)*(y-yc));
if(d<R){
if(x<xc)
putpixel(x,y,i);
else
putpixel(x,y,(i+1)%15);
}
//delay(5);
}
i=(i+1)%15;
}
}
void DDA(int xA,int yA,int xB,int yB,int color){
int dx=xB-xA;
int dy=yB-yA;
int step;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
float x_inc=(float)dx/step;
float y_inc=(float)dy/step;
float x=xA;
float y=yA;
putpixel(x,y,color);
for(int k=1;k<=step;k++){
x=x+x_inc;
y=y+y_inc;
putpixel(round(x),round(y),color);
delay(5);
}
}
typedef struct Dinh{
int x,y;
};
void VeDaGiac(Dinh d[],int n, int color){
for(int i=0;i<n;i++){
int j = (i+1)%n;
DDA(d[i].x,d[i].y,d[j].x,d[j].y,color);
}
}
void ToLan(int x,int y,int mauto,int maubien){
int mauht=getpixel(x,y);
if(mauht!=mauto && mauht!=maubien){
putpixel(x,y,mauto);
ToLan(x-1,y,mauto,maubien);
//ToLan(x+1,y,mauto,maubien);
//ToLan(x,y-1,mauto,maubien);
ToLan(x,y+1,mauto,maubien);
}
}
void ToLan2(int x,int y,int mauto,int maubien){
int mauht=getpixel(x,y);
if(mauht!=mauto && mauht!=maubien){
putpixel(x,y,mauto);
//ToLan2(x-1,y,mauto,maubien);
ToLan2(x+1,y,mauto,maubien);
ToLan2(x,y-1,mauto,maubien);
//ToLan2(x,y+1,mauto,maubien);
}
}
int main(){
initwindow(640,480);
// VeHV(200,200,400,400,15);
// ToMauHVScanLine(200,200,400,400,13);
// ToMauPutPixel(200,200,400,400,2);
// circle(320,240,100);
//ToMauDuongTron(320,240,150,13);
// ToLan(221,240,RED,WHITE);
Dinh d[]={{100,300},{150,200},{270,350},{300,200}};
VeDaGiac(d,4,15);
ToLan2(103,297,RED,WHITE);
ToLan(298,202,BLUE,WHITE);
// VeDaGiac(d,4,13);
getch();
return 0;
}
Post a Comment