Thuat Toan Ve Duong Thang DDA

17
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-thang- DDA#ixzz1pTThuật toán DDA vẽ đường thẳng 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;

Transcript of Thuat Toan Ve Duong Thang DDA

Page 1: Thuat Toan Ve Duong Thang DDA

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 veputpixel(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-thang-DDA#ixzz1pTThuật toán DDA vẽ đường thẳng trong C++?

đây là code pascalprocedure dda(x1,y1,x2,y2, color: integer);var dx,dy,step:integer;x_inc,y_inc,x,y:real;begindx:=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);

Page 2: Thuat Toan Ve Duong Thang DDA

for k:=1 to step dobegin x:=x+x_incy:=y+y_incputpixel(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)

Page 3: Thuat Toan Ve Duong Thang DDA

{// kiểm tra nếu x1>x2 thì hóan vịif (x1 > x2){

Swap(x1, x2);Swap(y1, y2);

}

// tính Dx và Dyint Dx = x2 - x1;int Dy = y2 - y1;

// tính hệ số góc mfloat m = Dy / (float)Dx;

// nếu hệ số góc |m| > 1if (abs(m) > 1){

// hoán chuyển giá trị của x và ySwap(x1, y1);Swap(x2, y2);

}

// tính bfloat b = -m * x1 + y1;

// vẽ .....for (int x = x1; x < x2; ++x){

SetPixel(hdc, x, (int)floor(m * x + b), c);}

}

Code:  Thuật toán vẽ đường thẳng 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;

Page 4: Thuat Toan Ve Duong Thang DDA

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

Page 5: Thuat Toan Ve Duong Thang DDA

Chủ đề Tìm kiếm    Tùy chọn chủ đề

Chủ đề : Sử dụng thuật toán Bresenham vẽ đoạn thằng.

ngvannam5 

Administrator 

Đã gia nhập: 08/04/2008 Địa chỉ: Quãng NgãiTình trạng: OfflineBài viết: 157Được cảm ơn: 131 lần trong 41 bài viết

Trích dẫn   Trả lời  Đã đăng: 10/10/2008 lúc 16:36

Input: Cho vào từ file line.in là một file text có 2 dòng.+ Dòng đầu tiên chứa tọa độ của điểm đầu đoạn thẳng.

Page 6: Thuat Toan Ve Duong Thang DDA

+ Dòng tiếp theo chứa tọa độ của điểm cuối đoạn thẳng.Input đảm bảo điểm đầu tiên luôn ở bên trái của điểm cuối - tức có hoành độ nhỏ hơn (nếu 2 điểm cùng hoành độ thì điềm đầu là điểm có tung độ lớn hơn).

Output: Xuất ra file văn bản line.out gồm nhiều dòng chứa danh sách tọa độ các điểm trên đoạn thẳng bao gồm cả 2 đầu mút. Mỗi dòng chứa 2 số nguyên ứng với tọa độ các điểm.

using System;using System.Collections.Generic;using System.Text;using System.IO;

namespace Bresenham 

public struct DIEM

public int x; public int y;

class Bresenham

public static DIEM A; public static DIEM B; public static int dx; public static int dy; public static int n; public static DIEM[]MyDiemArray = new DIEM[1000];

public static void ReadFileInput(string FileName)

StreamReader fi = File.OpenText(FileName); string str; string[] temp;

Page 7: Thuat Toan Ve Duong Thang DDA

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

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

fi.Close();

public static void WriteFileOuput()

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

public  static DIEM HoanVi(int x, int y) 

DIEM C ; C.x = y; C.y = x; return C;

public  static void Bresenham1(DIEM N, DIEM M)

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

Page 8: Thuat Toan Ve Duong Thang DDA

    {        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; }

public static void Bresenham2(DIEM N, DIEM M)

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

int x = N.x; int y = N.y;

n = 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;    }

Page 9: Thuat Toan Ve Duong Thang DDA

    else    {        p += two_Dx;    }    y++;

    n++;    MyDiemArray[n].x = x;    MyDiemArray[n].y = y; }

public static void TTBresenham()

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

if (dx > dy) {

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

Page 10: Thuat Toan Ve Duong Thang DDA

//file chương trình using System;using System.Collections.Generic;using System.Text;

namespace Bresenham 

class Program

static void Main(string[] args)

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

Code chương trình

#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)

Page 11: Thuat Toan Ve Duong Thang DDA

{ 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();}

17-05-2010, 05:21 PM//Ve doan thang bang thuat toan DDA voi he so goc bat ky

#include <iostream.h>#include <conio.h>#include <math.h>#include <graphics.h>

#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<1if(0 <= m && m <= 1) {for(int i = 1; i < abs(x2 - x1); i++){x++;

Page 12: Thuat Toan Ve Duong Thang DDA

y += m;putpixel(x, round(y), C);}cout<<"m1= "<<m;}

//Ve doan thang co HSG m>1if(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 Oyif(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 Oyif(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");

Page 13: Thuat Toan Ve Duong Thang DDA

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);elseLine_DDA_DX(-x1, y1, -x2, y2);

getch();closegraph();}

THUẬT TOÁN MIDPOINT VE ĐƯỜNG TRÒN*/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++;

}

}

/*THUẬT TOÁN MIDPOINT VE ĐƯỜNG TRÒN*/

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

#include<dos.h>

typedef struct

{ int x;

int y;

}POINT;

Page 14: Thuat Toan Ve Duong Thang DDA

/*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++;

}

Page 15: Thuat Toan Ve Duong Thang DDA

}

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();

}