Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Trasarea segmentelor de dreapta in spatiul discret
- DDA line drawing algorithm
- */
- #include <iostream>
- #include <graphics.h>
- #include <math.h>
- struct punct
- {
- int x,y;
- };
- void BresGeneral(int, int, int, int);
- punct waitForLeftMouseClick2();
- int culoareCreion = GREEN;
- int culoareHartie = WHITE;
- void patr(int x,int y);
- void waitForLeftMouseClick();
- void DDALine(int x1,int y1,int x2,int y2,int iColor);
- int x,y; // coordonate mouse
- int latp=10; // marimea patratului
- punct A,B;
- int X,Y; // coordonate patrat
- int Xa,Ya;
- int main()
- {
- int x1,x2,y1,y2,iColor;
- x1 = 78;
- y1 = 120;
- x2 = 210;
- y2 = 350;
- initwindow(800,800); //open a 400x300 graphics window
- setcolor(GREEN);
- for(int i=0;i<=50;i++) line(50,50+i*latp,550,50+i*latp);
- for(int j=0;j<=50;j++) line(50+j*latp,50,50+j*latp,550);
- while(1){
- A=waitForLeftMouseClick2();
- B=waitForLeftMouseClick2();
- BresGeneral((A.x-50)/latp,(A.y-50)/latp,(B.x-50)/latp,(B.y-50)/latp);
- }
- //waitForLeftMouseClick();
- return 0;
- }
- void patr(int i,int j){
- // determin pozitia in grila
- int xp,yp;
- xp = 50 + i*latp;
- yp = 50 + j*latp;
- // determin culoarea celulei
- if(getpixel(xp+1,yp+1)==BLACK) setcolor(YELLOW);
- else setcolor(BLACK);
- for(int k=1;k<latp;k++) line(xp+k,yp+1,xp+k,yp+latp);
- setcolor(GREEN);
- }
- void DDALine(int x1,int y1,int x2,int y2,int iColor)
- {
- float dX,dY,iSteps;
- float xInc,yInc,iCount,x,y;
- dX = x1 - x2;
- dY = y1 - y2;
- if (fabs(dX) > fabs(dY))
- iSteps = fabs(dX);
- else
- iSteps = fabs(dY);
- xInc = dX/iSteps;
- yInc = dY/iSteps;
- x = x1;
- y = y1;
- circle((int)x,(int)y,1);
- for (iCount=1; iCount<=iSteps; iCount++)
- {
- putpixel((int)x,(int)y,iColor);
- x -= xInc;
- y -= yInc;
- }
- circle((int)x,(int)y,1);
- return;
- }
- void waitForLeftMouseClick()
- {
- clearmouseclick(WM_LBUTTONDOWN);
- const int DELAY = 50; // Milliseconds of delay between checks
- int x, y;
- while (!ismouseclick(WM_LBUTTONDOWN))
- delay(DELAY);
- getmouseclick(WM_LBUTTONDOWN, x, y);
- }
- punct waitForLeftMouseClick2()
- {
- clearmouseclick(WM_LBUTTONDOWN);
- const int DELAY = 50; // Milliseconds of delay between checks
- punct p;
- while (!ismouseclick(WM_LBUTTONDOWN))
- delay(DELAY);
- getmouseclick(WM_LBUTTONDOWN, p.x, p.y);
- return p;
- }
- // Bresenham generalizat
- void BresGeneral(
- int x1, int y1, int x2, int y2)
- {
- int oct, dx,dy, absdx, absdy,c1,c2,t;
- int culoare = culoareCreion;
- int x,y,i;
- if(x1==x2) // vertical
- {
- if (y1>y2)
- {
- y=y1;
- y1=y2;
- y2=y;
- }
- for(y=y1; y<=y2; y++)
- patr(x1,y);
- return;
- }
- if(y1==y2) // orizontal
- {
- if (x1>x2)
- {
- x=x1;
- x1=x2;
- x2=x;
- }
- for(x=x1; x<=x2; x++)
- patr(x,y1);
- return;
- }
- dx=x2-x1;
- dy=y2-y1;
- absdx=abs(dx);
- absdy=abs(dy);
- if(dx>0) // oct=1,2,7,8
- {
- if(dy>0) // oct 1,2
- if(dx>=dy)
- oct=1;
- else
- oct=2;
- else if(dx>=absdy)
- oct=8;
- else
- oct=7;
- }
- else // 3,4,5,6
- {
- if(dy>0) // oct 3,4
- if(absdx>=dy)
- oct=4;
- else
- oct=3;
- else if(absdx>=absdy)
- oct=5;
- else
- oct=6;
- }
- if(absdy>absdx)
- {
- x=absdx;
- absdx=absdy;
- absdy=x;
- }
- c1=absdy<<1;
- c2=c1-(absdx<<1);
- t=c1-absdx;
- patr(x1,y1);
- for(i=1, x=x1, y=y1; i<absdx; i++)
- {
- if(t<0) // deplasament O
- {
- t+=c1;
- switch(oct)
- {
- case 1:
- case 8:
- x++;
- break;
- case 4:
- case 5:
- x--;
- break;
- case 2:
- case 3:
- y++;
- break;
- case 6:
- case 7:
- y--;
- break;
- }
- }
- else
- {
- t+=c2; // deplasament D
- switch(oct)
- {
- case 1:
- case 2:
- x++;
- y++;
- break;
- case 3:
- case 4:
- x--;
- y++;
- break;
- case 5:
- case 6:
- x--;
- y--;
- break;
- case 7:
- case 8:
- x++;
- y--;
- break;
- }
- }
- patr(x,y);
- }
- patr(x2,y2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement