Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <conio.h>
- #include <graphics.h>
- void startGraphics(){
- int gd=DETECT, gm;
- initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
- }
- void dda(int x1, int y1, int x2, int y2){
- int i, length, absx = abs(x2-x1), absy = abs(y2-y1);
- float dx, dy, x, y;
- absx>absy?(length=absx):(length=absy);
- dx = absx/length; dy = absy/length;
- dx>0?(x=x1+0.5):(x=x1-0.5); dy>0?(y=y1+0.5):(y=y1-0.5);
- putpixel((int)x, (int)y, 2);
- for(i=0; i<length; i++){
- x+=dx; y+=dy;
- putpixel((int)x, (int)y, 2);
- }
- }
- void bres(x1, y1, x2, y2){
- int i, x=x1, y=y1, dx = abs(x2-x1), dy = abs(y2-y1);
- float e = 2 * dy - dx;
- putpixel(x, y, 3);
- for(i=0; i<dx; i++){
- for(; e >=0; e-= 2*dx) y++;
- x++;
- e += 2*dy;
- putpixel(x, y, 3);
- }
- }
- void swap(int *a, int *b){
- int t = *a;
- *a = *b;
- *b = t;
- }
- void gbres(x1, y1, x2, y2){
- int i, x=x1, y=y1, dx = abs(x2-x1), dy = abs(y2-y1), flag = 0, sx, sy;
- float e;
- ((x2-x1)>0)?(sx=1):(sx=-1); ((y2-y1)>0)?(sy=1):(sy=-1);
- if(dx < dy){swap(&dx, &dy); flag = 1;}
- e = 2 * dy - dx;
- putpixel(x, y, 4);
- for(i=0; i<dx; i++){
- for(; e >=0; e -= 2*dx)
- if(flag) x+=sx;
- else y+=sy;
- if(flag) y+=sy;
- else x+=sx;
- e += 2*dy;
- putpixel(x, y, 4);
- }
- }
- void main(){
- char ch;
- startGraphics();
- cleardevice();
- ch = getch();
- if(ch=='d') dda(200, 200, 300, 300);
- else if(ch=='b') bres(200, 200, 300, 300);
- else if(ch=='g') gbres(200, 200, 300, 300);
- getch();
- closegraph();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement