Question

I am trying to write a bresenhams program for dy>dx. Our teacher has told us to write bresenhans line drawing program in C++ which will satisfy all possible condition and she had thought us the algo for just dx>dy by providing this formula's

 - G=G+2*dy-2dx; 
 - G=G+2*dy;

and leaving us the rest of the program as a research or H.W.

I however already figured out this program (only for dx>dy) using algorithm.I Just want to if those formula's are enough for the latter part or what modification is required?

#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
  int i,x,y,x0,y0,x1,y1,G,dx,dy,gd=DETECT,gm;
  initgraph(&gd,&gm,"d://tc//bgi//");
  cout<<" Enter the first coordinate of a line : ";
  cin>>x0>>y0;
  cout<<"Enter the last coordinate of a line : ";
  cin>>x1>>y1;
  dx=x1-x0;
  dy=y1-y0;
  dx=abs(dx);
  dy=abs(dy);
  putpixel(x0,y0,WHITE);
  G=2*dy-dx;
  x=x0;y=y0;
  if(dx>=dy)
  {
   for(x=x+1;x<=x1;x++)
    {
      if(G>0)
      {
          y=y+1;
     G=G+2*dy-2*dx;
      }
      else
      {
        G=G+2*dy;
       }
  putpixel(x,y,WHITE);
 }

}

No correct solution

OTHER TIPS

Actually it is the same for dy>dx as dx>dy. But you should probably check the order in which the points are given. How I would do it:

Consider start and end to be input points, mem is the index of pixel in a framebuffer (or image or whatever) with given width and height.

k = d_y / d_x;

if (-1 <= k && k <= 1) {
    c0 = 2 * abs(d_y);
    c1 = c0 - 2 * abs(d_x);
    p = c0 - abs(d_x);

    if (start->x <= end->x) {
        for (int i = start->x + 1; i < end->x; ++i) {
            if (p < 0) {
                p += c0;
                ++mem;
            } else {
                p += c1;
                if (k >= 0) {
                    mem += width + 1;
                } else {
                    mem += (1 - width);
                }
            }
            colorBuffer[mem] = currentColor;
        }
    } else {
        for (int i = start->x - 1; i > end->x; --i) {
            if (p < 0) {
                p += c0;
                --mem;
            } else {
                p += c1;
                if (k >= 0) {
                    mem -= width + 1;
                } else {
                    mem += width - 1;
                }
            }
            colorBuffer[mem] = currentColor;
        }
    }
}
else { 
    c0 = 2 * abs(d_x);
    c1 = c0 - 2 * abs(d_y);
    p = c0 - abs(d_y);
    //this is the same as dx >= dy, just switch x and y
   ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top