Advertisement
SVXX

Bresenham's Algorithm

Sep 1st, 2012
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <graphics.h>
  2. #include <iostream.h>
  3. #include <math.h>
  4. #include <conio.h>
  5. #include <fstream.h>
  6. #include <iomanip.h>
  7.  
  8. void main()
  9. {
  10.    int x1, y1, x2, y2;
  11.    int gdriver = DETECT, gmode;
  12.    clrscr();
  13.    cout << "Enter the coordinates : ";
  14.    cout << "\nx1 : ";
  15.    cin >> x1;
  16.    cout << "\ny1 : ";
  17.    cin >> y1;
  18.    cout << "\nx2 : ";
  19.    cin >> x2;
  20.    cout << "\ny2 : ";
  21.    cin >> y2;
  22.    
  23.    int dx = abs(x1 - x2), dy = abs(y1 - y2);
  24.    int p = 2 * dy - dx;
  25.    int twoDy = 2 * dy, twoDyDx = 2 * (dy - dx);
  26.    int x, y, xEnd;
  27.    cout << "\nSlope is : " << (float)dy/dx;
  28.  
  29.    if((((float)dy/dx) >= 1) || (((float)dy/dx) == 0))
  30.    {
  31.       cout << "\nSlope m must be 0 < m < 1";
  32.       getch();
  33.       return;
  34.    }
  35.    
  36.    int StoreX[50], StoreY[50], i = 1;
  37.    cout << "\nPress Enter to continue...";
  38.    getch();
  39.    initgraph(&gdriver, &gmode, "F:\\TC\\BGI");
  40.    if(x1 > x2)
  41.    {
  42.       x = x2;
  43.       y = y2;
  44.       xEnd = x1;
  45.    }
  46.    else
  47.    {
  48.       x = x1;
  49.       y = y1;
  50.       xEnd = x2;
  51.    }
  52.    StoreX[0] = x;
  53.    StoreY[0] = y;
  54.    putpixel(x, y, RED);
  55.    while(x < xEnd)
  56.    {
  57.       x++;
  58.       if(p < 0)
  59.         p += twoDy;
  60.       else
  61.      {
  62.         y++;
  63.         p += twoDyDx;
  64.      }
  65.      StoreX[i] = x;
  66.      StoreY[i] = y;
  67.      i++;    
  68.      putpixel(x, y, RED);
  69.    }     
  70.    outtextxy(100, 10, "Press Enter pls");
  71.    getch();
  72.    closegraph();
  73.    ofstream fout;
  74.    fout.open("Bresen.txt");
  75.    for(int k = 0; k < i; k++)
  76.    {
  77.       fout << StoreX[k] << setw(2) << StoreY[k] << endl;
  78.    }
  79.    fout.close();
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement