-2

First question, I have tried to calculate the expression, di+1=di+2*Δy−2*Δx(yi+1−yi) for the four quadrants. Irrespective of the quadrant, the expression was found to be the same, including signs. Am I right, or, there has been some mistakes in my calculations (hence, I am wrong)?

Second question, if this expression is only applicable for the first octet, how can I apply this to other octets? To me, there is no way to determine which octet I am working on. Coz, the value of m always represent two opposite octets. For example, if 0<m<1, it represents 1st and 5th octet. Right?

Thirdly, how can we determine the initial/starting value of di?

#include <iostream>
#include "utils.h"

void BresenhamLine(double x1, double y1, double x2, double y2, int color)
{
    if(x1>x2 || y1>y2)
    {
        Swap(x1, x2);
        Swap(y1, y2);
    }
    double x = x1;
    double y = y1;
    double dx = x2 - x1;
    double dy = y2 - y1;
    double dt = 2 * (dy - dx);
    double ds = 2 * dy;
    double d = 2*dy - dx;

    PlotPixel(x, y, color);

    if(dx>=dy)
    {
        while(x<=x2)
        {
            x++;
            if(d<0)
            {
                d = d + ds;
            }
            else
            {
                y++;
                d = d + dt;
            }
            PlotPixel(x, y, color);
        }
    }
    else
    {
        while(y<=y2)
        {
            y++;
            if(d<0)
            {
                x++;
                d = d + dt;
            }
            else
            {               
                d = d + ds;
            }
            PlotPixel(x, y, color);
        }
    }
}

int main()
{
int gm = DETECT;
int gd = DETECT;

initgraph(&gm, &gd, "");

double x1 = 0;
double y1 = 0;
double r = 50;
double x2 = 0;
double y2 = 0;
double signx = 0;
double signy = 0;

for(int theta=0 ; theta<=360 ; theta++)
{
    x2 = r * cos(DegreeToRad((double) theta));
    y2 = r * sin(DegreeToRad((double) theta));

    x1 = 5 * cos(DegreeToRad((double) theta));
    y1 = 5 * sin(DegreeToRad((double) theta));

    BresenhamLine(x1, y1, x2, y2, YELLOW);
}

getch();
closegraph();
return 0;
}

The lines that go through 2nd and 4th quadrant are not showing up.

How to fix that with some minor changes in my code?

genpfault
  • 51,148
  • 11
  • 85
  • 139
user366312
  • 16,949
  • 65
  • 235
  • 452

3 Answers3

4

With this input: x1: 100 y1: -100 x2: -100 y2: 100

this logic:

if(x1>x2 || y1>y2)
{   
    Swap(x1, x2);
    Swap(y1, y2);
}   

fails.

perreal
  • 94,503
  • 21
  • 155
  • 181
1

This page is a good place to start. It shows code as well for 1 of the octants:

http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html

I think you need to swap the x if x1 > x2 and swap the y if y1 > y2 but not swap both if only 1 of those is true.

The external links section of the Wikipedia page contains several links to ready-made implementations that you can study.

alle_meije
  • 2,424
  • 1
  • 19
  • 40
1

Try this:

void BresenhamLine( double x1, double y1, double x2, double y2, int color )
{
  const bool steep = (std::abs(y2 - y1) > std::abs(x2 - x1));
  if(steep)
  {
    std::swap(x1, y1);
    std::swap(x2, y2);
  }

  if(x1 > x2)
  {
    std::swap(x1, x2);
    std::swap(y1, y2);
  }

  double dx = x2 - x1;
  double dy = std::abs(y2 - y1);

  double error = dx / 2;
  int ystep = (y1 < y2) ? 1 : -1;
  int y = (int)y1;

  int maxX = (int)x2;

  for(int x=(int)x1; x<maxX; x++)
  {
    if(steep)
    {
        PlotPixel(y, x, color);
    }
    else
    {
        PlotPixel(x, y, color);
    }

    error -= dy;
    if(error < 0)
    {
        y += ystep;
        error += dx;
    }
  }
}

I got this by slightly modifying the code here:http://rosettacode.org/wiki/Bitmap/Bresenham's_line_algorithm#C.2B.2B

Isaac
  • 816
  • 5
  • 12
  • Ok. It works. But, I was talking about some minor modification of my code so that I can understand the theory. – user366312 Aug 29 '15 at 10:10
  • then mainly you need to change `if(x1>x2 || y1>y2) { Swap(x1, x2); Swap(y1, y2); }` into `if(x1>x2) Swap(x1, x2); if(y1>y2) Swap(y1, y2);` – alle_meije Sep 09 '15 at 08:26