Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Thuat toan ve duong thang DDA

#include<conio.h> #include<graphics.h> #include<math.h> #define round(a) int(a+0.5) int color = GREEN; void LineDDA(int x1, int y1, int x2, int y2) { int x = x1; float y = y1; float m = (y2-y1)/(x2-x1); // Bat dau ve putpixel(x,round(y),color); for (int i=x1; i<x2; i++) { x++; y += m; putpixel(x,round(y),color); } } void main() { int mh = 0, mode = 0; initgraph(&mh,&mode,"C:\\TC\\BGI"); LineDDA(100,100,200,0); LineDDA(100,100,200,100); line(100,100,600,400); getch(); closegraph(); } Link: http://www.ddth.com/showthread.php/168788-Thuat-toan-ve-duong-thangDDA#ixzz1pTThut ton DDA v ng thng trong C++?

y l code pascal procedure dda(x1,y1,x2,y2, color: integer); var dx,dy,step:integer; x_inc,y_inc,x,y:real; begin dx:=x2-x1; dy:=y2-y1; if abs(dx)>abs(dy) then step:=abs(dx) else step:=abs(dy); x_inc:=dx/step; y_inc:=dy/step;

x:=x1; y:=y1; putpixel(round(x),round(y),color); for k:=1 to step do begin x:=x+x_inc y:=y+y_inc putpixel(round(x),round(y),color); end; end; void CLine::drawLine_DDA(CDC *pDC, int color) { if(this->m_p1 == this->m_p2) pDC->SetPixel(this->m_p1, color); if(abs(m_p2.x - m_p1.x) > abs(m_p2.y - m_p1.y)) { if(m_p1.x > m_p2.x) { CPoint temp; temp = m_p1; m_p1 = m_p2; m_p2 = temp; } drawLine1_DDA(pDC, color); } else { if(m_p1.y > m_p2.y) { CPoint temp; temp = m_p1; m_p1 = m_p2; m_p2 = temp; } drawLine2_DDA(pDC, color); } } void CLine::drawLine1_DDA(CDC *pDC, int color) { int x = this->m_p1.x; float y = this->m_p1.y; float m = (float)(m_p2.y - m_p1.y)/(m_p2.x - m_p1.x); while(x < m_p2.x) { pDC->SetPixel(x,Round(y), color); x++; y += m; //Sleep(50); } }

void DDAAlgo::DrawLine(int x1, int y1, int x2, int y2, DWORD c) { // kim tra nu x1>x2 th han v if (x1 > x2) { Swap(x1, x2); Swap(y1, y2); } // tnh Dx v Dy int Dx = x2 - x1; int Dy = y2 - y1; // tnh h s gc m float m = Dy / (float)Dx; // nu h s gc |m| > 1 if (abs(m) > 1) { // hon chuyn gi tr ca x v y Swap(x1, y1); Swap(x2, y2); } // tnh b float b = -m * x1 + y1; // v ..... for (int x = x1; x < x2; ++x) { SetPixel(hdc, x, (int)floor(m * x + b), c); } }

Code:

Thut ton v ng thng Bresenham

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> #include<string.h> #include<stdlib.h> #define PATH "D:\\BORLANDC\\BGI" void LineBres(int x1, int y1, int x2, int y2,int color) { int Dx, Dy, p, Const1, Const2; int x, y; int i; Dx = x2 - x1; Dy = y2 - y1;

p = 2*Dy - Dx; // Dy <<1 - Dx Const1 = 2*Dy; // Dy <<1 Const2 = 2*(Dy-Dx); // (Dy-Dx) <<1 x = x1; y = y1; putpixel(x, y,color); for(i=x1; i<x2; i++) { if (p<0) p += Const1; else { p += Const2; y++; } x++; putpixel(x, y,color); } } // LineBres void main() { int mh,mode; mh=mode=0; initgraph(&mh,&mode,PATH); LineBres(200,100,300,450,WHITE); getch(); closegraph(); }

Share Topic

Tm kim

Ty chn ch

S dng thut ton Bresenham v on thng.


ngvannam5

Administrator

gia nhp: 08/04/2008 a ch: Qung Tnh trng: Offl Bi vit: 157 c cm n: 1 trong 41 bi vit

Tr li

ng: 10/10/2008 lc 16:36

ho vo t file line.in l mt file text c 2 dng. u tin cha ta ca im u on thng. ip theo cha ta ca im cui on thng. m bo im u tin lun bn tri ca im cui - tc c honh nh hn (nu 2 im cng honh th m c tung ln hn).

Xut ra file vn bn line.out gm nhiu dng cha danh sch ta cc im trn on thng bao gm c 2 i dng cha 2 s nguyn ng vi ta cc im.

stem; stem.Collections.Generic; stem.Text; stem.IO;

ce Bresenham struct DIEM

lic int x; lic int y;

Bresenham

lic static DIEM A; lic static DIEM B; lic static int dx; lic static int dy; lic static int n;

lic static DIEM[]MyDiemArray = new DIEM[1000];

lic static void ReadFileInput(string FileName)

treamReader fi = File.OpenText(FileName); tring str; tring[] temp;

tr = fi.ReadLine(); emp = str.Split(' '); A.x = Convert.ToInt32(temp[0]); A.y = Convert.ToInt32(temp[1]);

tr = fi.ReadLine(); emp = str.Split(' '); B.x = Convert.ToInt32(temp[0]); B.y = Convert.ToInt32(temp[1]);

.Close();

lic static void WriteFileOuput()

treamWriter fo = new StreamWriter("line.out"); or (int i = 0; i <= n; i++) fo.WriteLine(MyDiemArray.x + " " + MyDiemArray.y); fo.Close();

lic static DIEM HoanVi(int x, int y)

DIEM C ; C.x = y; C.y = x; eturn C;

lic static void Bresenham1(DIEM N, DIEM M)

nt p = 2 * dy - dx; nt two_Dy = 2 * dy;

nt two_DyDx = 2 * (dy - dx); nt x = N.x; nt y = N.y; = 0; MyDiemArray[n].x = x; MyDiemArray[n].y = y; while (x < M.x) if (p >= 0) { if (N.y > M.y) y--; else y++; p += two_DyDx; } else { p += two_Dy; } x++; n++; MyDiemArray[n].x = x; MyDiemArray[n].y = y;

lic static void Bresenham2(DIEM N, DIEM M)

nt p = 2 * dx - dy; nt two_Dx = 2 * dx; nt two_DxDy = 2 * (dx - dy);

nt x = N.x; nt y = N.y;

= 0; MyDiemArray[n].x = x; MyDiemArray[n].y = y;

while (y < M.y) if (p >= 0) { if (N.x > M.x) x--; else x++; p += two_DxDy; } else { p += two_Dx; } y++; n++; MyDiemArray[n].x = x; MyDiemArray[n].y = y;

lic static void TTBresenham()

x = Math.Abs((B.x - A.x)); y = Math.Abs((B.y - A.y)); (dx > dy)

if (A.x < B.x) { Bresenham1(A, B); } else { Bresenham1(B, A); }

lse

if (A.y < B.y) { Bresenham2(A, B); } else { Bresenham2(B, A); }

ng trnh stem; stem.Collections.Generic; stem.Text;

ce Bresenham

Program

c void Main(string[] args)

resenham.ReadFileInput("line.in"); resenham.TTBresenham(); resenham.WriteFileOuput();

Code chng trnh #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int color = RED,x,y,R; void DDX(int x,int y) {

putpixel(x,y,color); putpixel(y,x,color); putpixel(y,-x,color); putpixel(x,-y,color); putpixel(-x,-y,color); putpixel(-y,-x,color); putpixel(-y,x,color); putpixel(-x,y,color); } void MidPoint(int x0,int y0, int R) { int x,y, p; x=0; y=R; DDX(x,y); p=5/4-R; while (x<y) { if (p<0) p=p+2*x+3 ; else { p= p+2*(x-y)+5; y=y-1; } x=x+1; DDX(x+x0,y+y0); DDX(x+x0,-y+y0); DDX(-x+x0,y+y0); DDX(-x+x0,-y+y0); } } void KhoiTao() { //int n,m; int n = DETECT; int m; initgraph(&n,&m,""); } void main() { KhoiTao(); printf("\nNhap tam (x,y)="); scanf("%d%d",&x,&y); printf("\nNhap ban kinh R="); scanf("%d",&R); MidPoint(x,y,R) ; getch(); closegraph(); }
//Ve doan thang bang thuat toan DDA voi he so goc bat ky #include #include #include #include <iostream.h> <conio.h> <math.h> <graphics.h> 17-05-2010, 05:21 PM

#define round(a) int(a + 0.5) int C = RED; void Line_DDA(float x1, float y1, float x2, float y2) //Ham ve doan thang co HSG m>0. { float x, y; if(x2>x1) {

x = x1; y = y1; } else { x = x2; y = y2; } putpixel(x, round(y), C); float m = float (y2 - y1)/(x2 - x1); //Ve doan thang co HSG 0<m<1 if(0 <= m && m <= 1) { for(int i = 1; i < abs(x2 - x1); i++) { x++; y += m; putpixel(x, round(y), C); } cout<<"m1= "<<m; } //Ve doan thang co HSG m>1 if(m > 1) { for(int i = 1; i < abs(y2 - y1); i++) { y++; x += 1/m; putpixel(x, round(y), C); } cout<<"m2= "<<m; } } void Line_DDA_DX(float x1, float y1, float x2, float y2) //Ham ve doan thang co HSG m<0. { float x, y; if(x2 > x1) { x = x1; y = y1; } else { x = x2; y = y2; } putpixel(-x, round(y), C); float m = float (y2 - y1)/(x2 - x1); //Ve doan thang co HSG -1<m<0 //Day la doan thang doi xung voi doan thang co HSG 0<m<1 qua Oy if(0 <= m && <= 1) { for(int i = 1; i < abs(x2 - x1); i++) { x++; y += m; putpixel(-x, round(y), C);

} cout<<"m3= "<<-m; } //Ve doan thang co HSG -1<m<0 //Day la doan thang doi xung voi doan thang co HSG m>1 qua Oy if(m > 1) { for(int i = 1; i < abs(y2 - y1); i++) { y++; x += 1/m; putpixel(-x, round(y), C); } cout<<"m4= "<<-m; } } void main() { int mh = 0, mode = 0; initgraph(&mh, &mode, "D:\\TC\\BGI"); float x1, y1, x2 ,y2; cout<<"Nhap vao toa do diem A(x1,y1): \n"; cin>>x1>>y1; cout<<"\n\nNhap vao toa do diem B(x2,y2): \n"; cin>>x2>>y2; float m = float (y2 - y1)/(x2 - x1); if(m>0) Line_DDA(x1, y1, x2, y2); else Line_DDA_DX(-x1, y1, -x2, y2); getch(); closegraph(); }

THUT TON MIDPOINT VE NG TRN*/


mercredi 27 mai 2009 05:09:49 void circlemidpoint(POINT tamI,int r,int color) { int x,y,P; POINT M; x=0; y=r; P=1-r; putpixel(tamI.x,tamI.y,color);//Ve tam hinh tron while(y>=x) { put8pixels(tamI.x,tamI.y,x,y,color); if(P<0) P+=2*x+3; else { P+=2*x-2*y+5; y--; } x++;

} }

/*THUT TON MIDPOINT VE NG TRN*/ #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> typedef struct { int x; int y; }POINT; /*Vo Thanh Hai*/ void put8pixels(int xc,int yc,int x,int y,int color) { putpixel(xc+x,yc+y,color); putpixel(xc-x,yc+y,color); putpixel(xc-y,yc+x,color); putpixel(xc-y,yc-x,color); putpixel(xc-x,yc-y,color); putpixel(xc+x,yc-y,color); putpixel(xc+y,yc-x,color); putpixel(xc+y,yc+x,color); } void circlemidpoint(int xc,int yc,int r,int color) { int x,y,P; x=0; y=r; P=1-r; while(y>=x) { put8pixels(xc,yc,x,y,color); if(P<0) P+=2*x+3; else { P+=2*x-2*y+5; y--; } x++; } } void circlemidpoint(POINT tamI,int r,int color) { int x,y,P;

POINT M; x=0; y=r; P=1-r; putpixel(tamI.x,tamI.y,color);//Ve tam hinh tron while(y>=x) { put8pixels(tamI.x,tamI.y,x,y,color); if(P<0) P+=2*x+3; else { P+=2*x-2*y+5; y--; } x++; } }

void main() { POINT tam; randomize(); int gd=0,gm; int r; char *sss; initgraph(&gd,&gm,"c:/borlandc/bgi"); //Test cach ve giua circlemidpoint va circle xem giong nhau ??? for(int i=1;i<=15;i++) { tam.x=random(640); tam.y=random(480); r=random(200); cleardevice(); sprintf(sss,"Toa do tam: (%d;%d)",tam.x,tam.y); outtextxy(50,50,sss); sprintf(sss,"Ban kinh r: %d",r); outtextxy(50,70,sss); circle(tam.x,tam.y,r); circlemidpoint(tam,r,YELLOW); delay(1000); } closegraph(); }

You might also like