Thuật toán DDA Line trường hợp 0<=k<=1
Code Demo vẽ đoạn thằng AB với A(100,100); B(300,200)
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<graphics.h> #include<math.h> // ham lam tron int Round(float a){ return (int)floor(a+0.5); } // DDA Line void DDA_Line(int x1,int y1, int x2, int y2, int color){ int x=x1; float y=y1,m=(float)(y2-y1)/(x2-x1); putpixel(x1,y1,color); for(x=x1;x<=x2;x++) { y=y+m; putpixel(Round(x),y,color); } } // ham main void main(){ clrscr(); int driver=DETECT, mode; initgraph(&driver,&mode,"C:\\TC\\BGI"); cout<<"\n Ve duong thang:\n"; gotoxy(100,100); DDA_Line(100,100,300,200,4); getch(); }
Thuật toán Bresenham Line trường hợp 0<=k<=1
Code Demo vẽ đoạn thằng AB với A(100,100); B(300,200)
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<graphics.h> #include<math.h> // ham lam tron int Round(float a){ return (int)floor(a+0.5); } // Bresenham_Line void Bre_Line(int x1, int y1, int x2, int y2, int c) { int x, y, dx, dy,p,const1,const2; y = y1; dx = x2 - x1; dy = y2 - y1; p = 2*dy - dx; const1 = 2*dy; const2 = 2*(dy-dx); for (x=x1; x<=x2; x++) { putpixel(x, y, c); if (p < 0) p += const1; // p=p + 2dy else { p +=const2; //p=p+2dy-2dx y++; } } } // ham main void main(){ clrscr(); int driver=DETECT, mode; initgraph(&driver,&mode,"C:\\TC\\BGI"); cout<<"\n Ve duong thang:\n"; gotoxy(100,100); Bre_Line(100,100,300,200,4); getch(); }
Thuật toán MidPoint Line trường hợp 0<=k<=1
Code Demo vẽ đoạn thằng AB với A(100,100); B(300,200)
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<graphics.h> #include<math.h> // ham lam tron int Round(float a){ return (int)floor(a+0.5); } // MidPoint_Line void Mid_Line(int x1, int y1, int x2, int y2, int c) { int x, y, dx, dy,d; y = y1; dx = x2 - x1; dy = y2 - y1; d= dy - dx/2; for (x=x1; x<=x2; x++) { putpixel(x, y, c); if (d <= 0) d = d + dy; else { y ++; d = d + dy - dx; } } } // ham main void main(){ clrscr(); int driver=DETECT, mode; initgraph(&driver,&mode,"C:\\TC\\BGI"); cout<<"\n Ve duong thang:\n"; gotoxy(100,100); Mid_Line(100,100,300,200,4); getch(); }