Greetings! My task is to impement Bresenham's line algorithm by mapping floats to integers, but be able to draw lines of any slope, including horizontal (zero slope) and vertical (infinite slope) lines. (The near-bottom of the page has an integer-only version of this.) My implementation of the linked pseudocode works partially and I'm almost certain the problem is in my mapping or/and rasterization function(s).

The line begins at x0,y0 and ends at x1,y1. The command line reads in a square image resolution like 999 for 999x999 pixels ABS (absolute value), MAX, and MIN are defined elsewhere in my program.

Function headers are bolded for easy finding.

[b]My Line Rasterization Function[/b]
/*
RASTERIZELINE
	Rasterize lines and call Pixel to display them.  My version.
*/

//MY BRESENHAM VERSION: This is my version of Bresenham's algorithm which doesn't entirely work.
[b]void rasterizeLine( Image &I, int x0, int y0, int x1, int y1 )[/b]
{
	int dx = x1-x0;
	int x0mx1 = x0-x1;
	int dy = y1-y0;
	int y0my1 = y0-y1;
	int slope = dy/dx;
	int d = 2*(y0-y1)*(1+x0)+(x1-x0)*(2*y0+1)+(2*x0*y1)-(2*x1*y0);	//from the Tiger book, p60
	int D = 2*(y1-y0) - (x1-x0);
 	bool steep = (ABS(dy) > ABS(dx));		//higher rise than run?
	int delta_x = x1 - x0;					//
	int delta_y = ABS(dy);					//
	int error = delta_x/2;					//pixel to color
	int x = x0;								//
	int y = y0;								//
	int ystep;								//line moves up or down
	int xhold, yhold;						//for swapping

	//not slanting down right
	if (steep)
	{	//swap x values
//		cout << "\nSTEEP: swapping x and y values\n";

		xhold=x0;
		x0=x1;
		x1=xhold;

		//swap y values
		yhold=y0;
		y0=y1;
		y1=yhold;		
	}

	//right to left
	if(x0 > x1)
	{
//		cout << "\nRIGHT TO LEFT: swapping x and y values to draw left to right\n";

		xhold=x0;
		x0=x1;
		x1=xhold;

		//swap y values
		yhold=y0;
		y0=y1;
		y1=yhold;	
	}

	//line moves up to down - check midpoint
	if(y0 < y1)
	{
//		cout << "\nYSTEP IS POSITIVE\n";
		ystep=1;
	}

	//line moves down to up - check midpoint
	else
	{
//		"\nYSTEP IS NEGATIVE\n";
		ystep=-1;
	}

	for(x; x < x1; x++)
	{
		//x0 and y0 swapped with x1 and y1
		if(steep)
		{
//			"\nSTEEP PIXELIZATION\n";
			I(y, x) = Pixel(255, 255, 255);
		}
	
		//no swapping
		else
		{
//			"\nPIXELIZATION, NOT STEEP\n";
			I(x, y) = Pixel(255, 255, 255);
		}

		error = error - delta_y;
		
		if(error < 0)
		{
			y = y + ystep;
			error += delta_x;
		}
	}
}

My code's resulting image

My instructor has a working version of rasterizeLine for all slopes, but this uses floats.

[b]My Instructor's Line Rasterization Function[/b]

[b]void rasterizeLine( Image &I, int x0, int y0, int x1, int y1 )[/b]
{
  int delta_x = x1 - x0;
  int delta_y = y1 - y0; //slope defined
  int steps = MAX( ABS(delta_x), ABS(delta_y) ); //greater rise or run?
  double c = 1.0 / MAX( 1, steps ); //c is always at least 1

  for( int i = 0; i <= steps; i++ )
  {
    int x = int( x0 + ( i * delta_x * c ) + 0.5 ); //x is the int version of the counter * change in x * (i) or (the greater absolute value of delta x or delta y) rounded up
    int y = int( y0 + ( i * delta_y * c ) + 0.5 );
    I( x, y ) = Pixel( 125, 255, 255 ); //plot point on screen
  }
}

My instructor's code's resulting image. (The color difference is OK.)

[b]Float to Integer Mapping[/b]
/*
IMAGEX
	Convert to image space's x coordinate
*/
[B]int imagex(int resolution, float xcoord)[/B] //coord is x0 or x1[/b]
{
	int n = resolution;
	int hn = resolution/2; //half n

	if(xcoord >= 0) //quad 1 or 4 - at least half n
	{
		return(rti((hn) + (xcoord*hn)));
	}

	else //quad 2 or 3
	{
		return(rti((hn) - (ABS(xcoord*hn))));
	}
}

/*
IMAGEY
	Convert to image space's y coordinate
*/
[b]int imagey(int resolution, float ycoord)[/b] //coord is y0 or y1
{
	int n = resolution;
	int hn = resolution/2; //half n

	if(ycoord >= 0) //quad 3 or 4 - at least half n
	{
		return(rti((hn) + (ABS(ycoord*hn))));
	}

	else //quad 1 or 2
	{
		return(rti((hn) - (ABS(ycoord*hn))));
	}
}

/*
RTI
	Round a float to the nearest integer.  I couldn't find an official round() function in a standard library.
*/
[b]int rti(float floater)[/b]
{
	if(floater >= 0.0)
	{
		return static_cast<int> (floater+0.5);
	}

	else
	{
		return static_cast<int> (floater-0.5);
	}
}

[b]int main(int argc, char *argv[])[/b] //arguments are unimportant for your purposes
{
    <snip>

        //N is input image resolution, like 999 for a 999x999 pixel image
        //ix0, etc. are the integer versions of their respective floats, x0, etc.
	ix0=imagex(N, x0); 
	ix1=imagex(N, x1);
	iy0=imagey(N, y0);
	iy1=imagey(N, y1);

        rasterizeLine( I, ix0, iy0, ix1, iy1 );
    <snip>
}

Attachments
data.txt is my instructor's test data which he used to produce a sphere. cli_bresenham.cpp is my primary file containing this problem code. getopt.h and getopt-long.c are files my instructor included with this assignment; don't mess with them.

Attachments
/* Greg Campbell
 * theelward@yahoo.com
 * CS 484
 * September 2, 2008
 *
 * Pseudocode used from Wikipedia.org
 *
 * $Id: cli_bresenham.cpp 963 2008-09-02 17:27:00Z mshafae $
 *
 * This file should help you to get started writing your
 * program. You need to figure out how to create integer
 * coordinates from the floating point data (mapping
 * [-1,1]x[-1,1] onto the square NxN image) and implement
 * Bresenham's line rasterization.
 *
 */

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#ifdef _MSC_VER
#define myStrdup(x) _strdup(x)
#else
#define myStrdup(x) strdup(x)
#endif

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#include "getopt.h"

using namespace std;

#define MIN( x, y ) ((x) <= (y) ? (x) :  (y))
#define MAX( x, y ) ((x) >= (y) ? (x) :  (y))
#define ABS( x )    ((x) >= 0.0 ? (x) : -(x))

char gDEBUG_FLAG;

#if __GNUC__
#define debug( format, ... ) if( gDEBUG_FLAG ) _debug( stderr, __FILE__, __FUNCTION__, __LINE__, (format), ## __VA_ARGS__ )
#endif

#define MAX_DEBUG_MSG_SIZE 256

void _debug( FILE *out, const char *filename, const char *function, int line, const char *format, ... ){

  char msg[MAX_DEBUG_MSG_SIZE];
  
  va_list vargs;
//  va_start(vargs, format);
//  vsnprintf( msg, sizeof(msg), format, vargs );
//  va_end( vargs );
  
  fprintf( out, "DEBUG:%s:%s:%d: %s\n", filename, function, line, msg );
}

//PROTOTYPES//
int imagex(int resolution, float xcoord);
int imagey(int resolution, float ycoord);
int rti(float floater);
//END PROTOTYPES//

typedef unsigned char channel;  

class Pixel {
public:
  Pixel( ) { r = 0; g = 0; b = 0; };
  Pixel( channel _r, channel _g, channel _b ) { r = _r; g = _g; b = _b; };
  channel r;
  channel g;
  channel b;
};

class Image {
public:
  inline Image( int x_res, int y_res );
  inline ~Image( ) { delete[] pixels; }
  inline bool write( const char *file_name );
  inline Pixel &operator()( int i, int j ) { return *( pixels + ( i * width + j ) ); }  
  Pixel *pixels;
  int    width;
  int    height;
};

inline Image::Image( int x_res, int y_res ){
  width  = x_res;
  height = y_res;
  pixels = new Pixel[ width * height ];
  Pixel *p = pixels;
  for( int i = 0; i < width * height; i++ ) *p++ = Pixel(0,0,0);
}

inline bool Image::write( const char *file_name ){
  Pixel *p = pixels;
  FILE  *fp = fopen( file_name, "w+b" );
  if( fp == NULL ){
    return false;
  }
  fprintf( fp, "P6\n%d %d\n255\n", width, height );
  for( int i = 0; i < width * height; i++ ){
    fprintf( fp, "%c%c%c", p->r, p->g, p->b );
    p++;
  }
  fclose( fp );
  return true;
}

void usage( char *msg = NULL ){
  if( msg ){
    fprintf( stderr, msg );
  }
  fprintf( stderr, "USAGE: bresenham [-h] [-v] -i inputfile -o outputfile -r size\n" );
  fprintf( stderr, "                 or\n" );
  fprintf( stderr, "       bresenham [--help] [--verbose] --input inputfile --output outputfile --resolution size\n" );
}

/*
IMAGEX
	Convert to image space's x coordinate
*/
int imagex(int resolution, float xcoord) //coord is x0 or x1
{
	int n = resolution;
	int hn = resolution/2; //half n

	if(xcoord >= 0) //quad 1 or 4 - at least half n
	{
		return(rti((hn) + (xcoord*hn)));
	}

	else //quad 2 or 3
	{
		return(rti((hn) - (ABS(xcoord*hn))));
	}
}

/*
IMAGEY
	Convert to image space's y coordinate
*/
int imagey(int resolution, float ycoord) //coord is y0 or y1
{
	int n = resolution;
	int hn = resolution/2; //half n

	if(ycoord >= 0) //quad 3 or 4 - at least half n
	{
		return(rti((hn) + (ABS(ycoord*hn))));
	}

	else //quad 1 or 2
	{
		return(rti((hn) - (ABS(ycoord*hn))));
	}
}

/*
RTI
	Round a float to the nearest integer.  I couldn't find an official round() function in a standard library.
*/
int rti(float floater)
{
	if(floater >= 0.0)
	{
		return static_cast<int> (floater+0.5);
	}

	else
	{
		return static_cast<int> (floater-0.5);
	}
}

/*
RASTERIZELINE
	Rasterize lines and call Pixel to display them.
*/

//MY BRESENHAM VERSION: This is my version of Bresenham's algorithm which doesn't entirely work.
void rasterizeLine( Image &I, int x0, int y0, int x1, int y1 )
{
	int dx = x1-x0;
	int x0mx1 = x0-x1;
	int dy = y1-y0;
	int y0my1 = y0-y1;
	int slope = dy/dx;
	int d = 2*(y0-y1)*(1+x0)+(x1-x0)*(2*y0+1)+(2*x0*y1)-(2*x1*y0);	//from the Tiger book, p60
	int D = 2*(y1-y0) - (x1-x0);
 	bool steep = (ABS(dy) > ABS(dx));		//higher rise than run?
	int delta_x = x1 - x0;					//
	int delta_y = ABS(dy);					//
	int error = delta_x/2;					//pixel to color
	int x = x0;								//
	int y = y0;								//
	int ystep;								//line moves up or down
	int xhold, yhold;						//for swapping

//	cout << "\nPRE-STEEP TEST\n";

	//not slanting down right
	if (steep)
	{	//swap x values
//		cout << "\nSTEEP: swapping x and y values\n";

		xhold=x0;
		x0=x1;
		x1=xhold;

		//swap y values
		yhold=y0;
		y0=y1;
		y1=yhold;		
	}

	//right to left
	if(x0 > x1)
	{
//		cout << "\nRIGHT TO LEFT: swapping x and y values to draw left to right\n";

		xhold=x0;
		x0=x1;
		x1=xhold;

		//swap y values
		yhold=y0;
		y0=y1;
		y1=yhold;	
	}

	//line moves up to down - check midpoint
	if(y0 < y1)
	{
//		cout << "\nYSTEP IS POSITIVE\n";
		ystep=1;
	}

	//line moves down to up - check midpoint
	else
	{
//		"\nYSTEP IS NEGATIVE\n";
		ystep=-1;
	}

	for(x; x < x1; x++)
	{
		//x0 and y0 swapped with x1 and y1
		if(steep)
		{
//			"\nSTEEP PIXELIZATION\n";
			I(y, x) = Pixel(255, 255, 255);
		}
	
		//no swapping
		else
		{
//			"\nPIXELIZATION, NOT STEEP\n";
			I(x, y) = Pixel(255, 255, 255);
		}

		error = error - delta_y;
		
		if(error < 0)
		{
			y = y + ystep;
			error += delta_x;
		}
	}
	
}

/*
//HANDOUT VERSION: This code was based on the pseudocode from the 2 page handout you gave us.  It draws different parts of the image than my above Bresenham implementation but is still incomplete.
void rasterizeLine(Image &I, int x0, int y0, int x1, int y1)
{
	int dx = x1-x0;
	int x0mx1 = x0-x1;
	int dy = y1-y0;
	int y0my1 = y0-y1;
	int slope = dy/dx;
	int d = 2*(y0-y1)*(1+x0)+(x1-x0)*(2*y0+1)+(2*x0*y1)-(2*x1*y0);	//from the Tiger book, p60
	int D = 2*(y1-y0) - (x1-x0);
 	bool steep = (ABS(dy) > ABS(dx));	//slanting down right?
	int delta_x = x1 - x0;					//
	int delta_y = ABS(dy);				//
	int error = delta_x/2;					//pixel to color
	int x = x0;								//
	int y = y0;								//
	int ystep;								//line moves up or down
	int xhold, yhold;						//for swapping

	I(x, y) = Pixel(255, 255, 255);

	while(x < x1)
	{
		x+=1;
		if(D<0)
		{
			D+=2*dy;
		}

		else
		{
			y++;
			D+=2*(dy-dx);
		}

		I(x, y) = Pixel(255, 255, 255);
	}
}
*/

/**************************************************
 * This version of rasterizeLine is very crude.  It
 * uses rounded floating-point arithmetic to determine
 * which pixels between (x0,y0) and (x1,y1) to set
 * in order to best approximate the line between these
 * points. Your version should implement Bresenham's
 * algorithm, and should therefore contain no floating
 * point arithmetic at all. Be sure to handle all
 * cases of (x0,y0) and (x1,y1).
 **************************************************/

/*
//SAMPLE CODE VERSION: This version displays images correctly but doesn't use Breenham's algorithm.
void rasterizeLine( Image &I, int x0, int y0, int x1, int y1 )
{
  int delta_x = x1 - x0;
  int delta_y = y1 - y0; //slope defined
  int steps = MAX( ABS(delta_x), ABS(delta_y) ); //greater rise or run?
  double c = 1.0 / MAX( 1, steps ); //c is always at least 1

  for( int i = 0; i <= steps; i++ )
  {
    int x = int( x0 + ( i * delta_x * c ) + 0.5 ); //x is the int version of the counter * change in x * (i) or (the greater absolute value of delta x or delta y) rounded up
    int y = int( y0 + ( i * delta_y * c ) + 0.5 );
    I( x, y ) = Pixel( 125, 255, 255 ); //plot point on screen
  }
}
*/

int main( int argc, char *argv[] )
{
  char buff[512], *line;
  float x0, y0, x1, y1;
  int ix0, iy0, ix1, iy1;
  int ch;
  char *inputFile = NULL, *outputFile = NULL;
  FILE *inputFileHandle;
  int N = 0;

  static struct option longopts[] = {
    { "input", required_argument, NULL, 'i' },
    { "output", required_argument, NULL, 'o' },
    { "resolution", required_argument, NULL, 'r' },
    { "help", required_argument, NULL, 'h' },
    { "verbose", required_argument, NULL, 'v' },
    { NULL, 0, NULL, 0 }
  };

  gDEBUG_FLAG = 0;

  // Process the command line arguments
  if( argc < 7){
    usage( "You must specify the correct number of parameters.\n" );
    exit(1);
  }

  while( (ch = getopt_long(argc, argv, "i:o:r:hv", longopts, NULL)) != -1 ){
    switch( ch )
	{
      case 'i':
        /* input file */
        if( !(inputFile = myStrdup( optarg )) ){
          fprintf( stderr, "Could not allocate memory for stdrup.\n" );
          exit(1);
        }
        if( !(inputFileHandle = fopen( inputFile, "r" )) ){
          fprintf( stderr, "Could not open file %s.\n", inputFile );
          exit(1);
        }
      break;
      case 'o':
        /* output file */
        if( !(outputFile = myStrdup( optarg )) ){
          fprintf( stderr, "Could not allocate memory for stdrup.\n" );
          exit(1);
        }
      break;
      case 'r':
        N = atoi( optarg );
      break;
      case 'h':
        usage( );
      break;
      case 'v':
        // Turn on/off your debugging messages
//        debug("Debug mode off."); // remove this, shown as an example
        gDEBUG_FLAG = 1;
//        debug("Debug mode on starting NOW!"); // remove this, shown as an example
      break;
      default:
        // do nothing
        fprintf( stderr, "Ignoring unknown option: %c\n", ch );
      break;
    } 
  }

  if( N <= 0 ){
    usage( "Specify a reasonable output image resolution such as 400.\n" );
  }
  if( inputFile == NULL || outputFile == NULL ){
    usage( "Specify valid input and output image file names.\n" ); 
  }
-0.06675  -0.64522  0.06675  -0.64522
 -0.13387  -0.58513  0.00000  -0.59370
  0.13387  -0.58513  0.00000  -0.59370
 -0.06675  -0.64522  0.00000  -0.59370
  0.00000  -0.59370  0.06675  -0.64522
 -0.26240  -0.42458 -0.13352  -0.44319
  0.00000  -0.44949 -0.13352  -0.44319
 -0.19968  -0.51108 -0.06858  -0.52421
 -0.13352  -0.44319 -0.06858  -0.52421
 -0.13352  -0.44319 -0.19968  -0.51108
  0.26240  -0.42458  0.13352  -0.44319
  0.06858  -0.52421  0.19968  -0.51108
  0.00000  -0.44949  0.13352  -0.44319
  0.06858  -0.52421  0.13352  -0.44319
  0.19968  -0.51108  0.13352  -0.44319
 -0.13387  -0.58513 -0.06858  -0.52421
 -0.06858  -0.52421  0.00000  -0.59370
  0.00000  -0.44949  0.06858  -0.52421
  0.00000  -0.44949 -0.06858  -0.52421
  0.00000  -0.59370  0.06858  -0.52421
  0.13387  -0.58513  0.06858  -0.52421
 -0.06858  -0.52421  0.06858  -0.52421
 -0.44949   0.00000 -0.34855   0.00000
 -0.23799   0.00000 -0.34855   0.00000
 -0.41516  -0.11244 -0.31003  -0.11394
 -0.34855   0.00000 -0.31003  -0.11394
 -0.34855   0.00000 -0.41516  -0.11244
  0.00000   0.00000 -0.12072   0.00000
 -0.19185  -0.11574 -0.07194  -0.11640
 -0.23799   0.00000 -0.12072   0.00000
 -0.19185  -0.11574 -0.12072   0.00000
 -0.07194  -0.11640 -0.12072   0.00000
 -0.32029  -0.32762 -0.20543  -0.33239
 -0.37170  -0.22264 -0.26038  -0.22825
 -0.14123  -0.22852 -0.26038  -0.22825
 -0.32029  -0.32762 -0.26038  -0.22825
 -0.26038  -0.22825 -0.20543  -0.33239
 -0.23799   0.00000 -0.19185  -0.11574
 -0.23799   0.00000 -0.31003  -0.11394
 -0.26038  -0.22825 -0.19185  -0.11574
 -0.14123  -0.22852 -0.19185  -0.11574
 -0.37170  -0.22264 -0.31003  -0.11394
 -0.31003  -0.11394 -0.26038  -0.22825
 -0.19185  -0.11574 -0.31003  -0.11394
  0.44949   0.00000  0.34855   0.00000
  0.31003  -0.11394  0.41516  -0.11244
  0.23799   0.00000  0.34855   0.00000
  0.31003  -0.11394  0.34855   0.00000
  0.41516  -0.11244  0.34855   0.00000
  0.20543  -0.33239  0.32029  -0.32762
  0.14123  -0.22852  0.26038  -0.22825
  0.37170  -0.22264  0.26038  -0.22825
  0.20543  -0.33239  0.26038  -0.22825
  0.26038  -0.22825  0.32029  -0.32762
  0.00000   0.00000  0.12072   0.00000
  0.23799   0.00000  0.12072   0.00000
  0.07194  -0.11640  0.19185  -0.11574
  0.12072   0.00000  0.19185  -0.11574
  0.12072   0.00000  0.07194  -0.11640
  0.26038  -0.22825  0.31003  -0.11394
  0.37170  -0.22264  0.31003  -0.11394
  0.14123  -0.22852  0.19185  -0.11574
  0.19185  -0.11574  0.26038  -0.22825
  0.23799   0.00000  0.31003  -0.11394
  0.23799   0.00000  0.19185  -0.11574
  0.19185  -0.11574  0.31003  -0.11394
 -0.26240  -0.42458 -0.20543  -0.33239
 -0.20543  -0.33239 -0.13352  -0.44319
 -0.14123  -0.22852 -0.07111  -0.34519
 -0.14123  -0.22852 -0.20543  -0.33239
 -0.13352  -0.44319 -0.07111  -0.34519
  0.00000  -0.44949 -0.07111  -0.34519
 -0.20543  -0.33239 -0.07111  -0.34519
  0.00000   0.00000  0.07194  -0.11640
  0.00000   0.00000 -0.07194  -0.11640
  0.00000  -0.23209  0.07194  -0.11640
  0.14123  -0.22852  0.07194  -0.11640
 -0.14123  -0.22852 -0.07194  -0.11640
 -0.07194  -0.11640  0.00000  -0.23209
  0.07194  -0.11640 -0.07194  -0.11640
  0.13352  -0.44319  0.20543  -0.33239
  0.26240  -0.42458  0.20543  -0.33239
  0.00000  -0.44949  0.07111  -0.34519
  0.07111  -0.34519  0.13352  -0.44319
  0.14123  -0.22852  0.20543  -0.33239
  0.14123  -0.22852  0.07111  -0.34519
  0.07111  -0.34519  0.20543  -0.33239
 -0.14123  -0.22852  0.00000  -0.23209
  0.14123  -0.22852  0.00000  -0.23209
 -0.07111  -0.34519  0.07111  -0.34519
  0.00000  -0.23209  0.07111  -0.34519
  0.00000  -0.23209 -0.07111  -0.34519
 -0.10580  -0.69747 -0.06675  -0.64522
  0.00000  -0.69038 -0.06675  -0.64522
 -0.20794  -0.69023 -0.17412  -0.64791
 -0.17412  -0.64791 -0.10580  -0.69747
 -0.13387  -0.58513 -0.06675  -0.64522
 -0.13387  -0.58513 -0.17412  -0.64791
 -0.17412  -0.64791 -0.06675  -0.64522
 -0.39251  -0.63509 -0.37596  -0.60831
 -0.37596  -0.60831 -0.30418  -0.66916
 -0.34857  -0.56399 -0.28051  -0.63488
 -0.34857  -0.56399 -0.37596  -0.60831
 -0.30418  -0.66916 -0.28051  -0.63488
 -0.20794  -0.69023 -0.28051  -0.63488
 -0.37596  -0.60831 -0.28051  -0.63488
 -0.26240  -0.42458 -0.19968  -0.51108
 -0.26240  -0.42458 -0.31050  -0.50240
 -0.24489  -0.58103 -0.19968  -0.51108
 -0.13387  -0.58513 -0.19968  -0.51108
 -0.34857  -0.56399 -0.31050  -0.50240
 -0.31050  -0.50240 -0.24489  -0.58103
 -0.19968  -0.51108 -0.31050  -0.50240
 -0.28051  -0.63488 -0.17412  -0.64791
 -0.34857  -0.56399 -0.24489  -0.58103
 -0.13387  -0.58513 -0.24489  -0.58103
 -0.28051  -0.63488 -0.24489  -0.58103
 -0.24489  -0.58103 -0.17412  -0.64791
 -0.67222  -0.33810 -0.61335  -0.44203
 -0.53903  -0.53269 -0.61335  -0.44203
 -0.65396  -0.24979 -0.60967  -0.36518
 -0.65396  -0.24979 -0.67003  -0.29777
 -0.61849  -0.40745 -0.60967  -0.36518
 -0.54990  -0.46967 -0.60967  -0.36518
 -0.67222  -0.33810 -0.67003  -0.29777
 -0.67003  -0.29777 -0.61849  -0.40745
 -0.60967  -0.36518 -0.67003  -0.29777
 -0.39251  -0.63509 -0.47670  -0.56040
 -0.55024  -0.50723 -0.47125  -0.58915
 -0.54990  -0.46967 -0.47670  -0.56040
 -0.54990  -0.46967 -0.55024  -0.50723
 -0.55024  -0.50723 -0.47670  -0.56040
 -0.47125  -0.58915 -0.47670  -0.56040
 -0.67222  -0.33810 -0.61849  -0.40745
 -0.61849  -0.40745 -0.61335  -0.44203
 -0.54990  -0.46967 -0.61849  -0.40745
 -0.61335  -0.44203 -0.55024  -0.50723
 -0.53903  -0.53269 -0.55024  -0.50723
 -0.61849  -0.40745 -0.55024  -0.50723
 -0.44949   0.00000 -0.41516  -0.11244
 -0.44949   0.00000 -0.52064  -0.06857
 -0.48379  -0.18041 -0.41516  -0.11244
 -0.37170  -0.22264 -0.41516  -0.11244
 -0.57901  -0.13405 -0.52064  -0.06857
 -0.52064  -0.06857 -0.48379  -0.18041
 -0.41516  -0.11244 -0.52064  -0.06857
 -0.38232  -0.39448 -0.32029  -0.32762
 -0.26240  -0.42458 -0.32029  -0.32762
 -0.48946  -0.35417 -0.43645  -0.29272
 -0.43645  -0.29272 -0.38232  -0.39448
 -0.37170  -0.22264 -0.32029  -0.32762
 -0.37170  -0.22264 -0.43645  -0.29272
 -0.43645  -0.29272 -0.32029  -0.32762
 -0.65396  -0.24979 -0.62361  -0.19488
 -0.62361  -0.19488 -0.58075  -0.30532
 -0.57901  -0.13405 -0.54033  -0.24705
 -0.57901  -0.13405 -0.62361  -0.19488
 -0.58075  -0.30532 -0.54033  -0.24705
 -0.48946  -0.35417 -0.54033  -0.24705
 -0.62361  -0.19488 -0.54033  -0.24705
 -0.37170  -0.22264 -0.48379  -0.18041
 -0.54033  -0.24705 -0.43645  -0.29272
 -0.57901  -0.13405 -0.48379  -0.18041
 -0.54033  -0.24705 -0.48379  -0.18041
 -0.43645  -0.29272 -0.48379  -0.18041
 -0.47670  -0.56040 -0.37596  -0.60831
 -0.54990  -0.46967 -0.45706  -0.52374
 -0.34857  -0.56399 -0.45706  -0.52374
 -0.47670  -0.56040 -0.45706  -0.52374
 -0.45706  -0.52374 -0.37596  -0.60831
 -0.65396  -0.24979 -0.58075  -0.30532
 -0.48946  -0.35417 -0.58075  -0.30532
 -0.60967  -0.36518 -0.52793  -0.41917
 -0.58075  -0.30532 -0.52793  -0.41917
 -0.58075  -0.30532 -0.60967  -0.36518
 -0.26240  -0.42458 -0.38232  -0.39448
 -0.42466  -0.46675 -0.31050  -0.50240
 -0.48946  -0.35417 -0.38232  -0.39448
 -0.42466  -0.46675 -0.38232  -0.39448
 -0.31050  -0.50240 -0.38232  -0.39448
 -0.54990  -0.46967 -0.52793  -0.41917
 -0.52793  -0.41917 -0.45706  -0.52374
 -0.48946  -0.35417 -0.42466  -0.46675
 -0.48946  -0.35417 -0.52793  -0.41917
 -0.45706  -0.52374 -0.42466  -0.46675
 -0.34857  -0.56399 -0.42466  -0.46675
 -0.52793  -0.41917 -0.42466  -0.46675
  0.00000  -0.69038  0.00000  -0.72871
  0.00000  -0.69038 -0.10580  -0.69747
 -0.10413  -0.73103  0.00000  -0.72871
  0.00000  -0.75108  0.00000  -0.72871
 -0.20794  -0.69023 -0.10580  -0.69747
 -0.10580  -0.69747 -0.10413  -0.73103
  0.00000  -0.72871 -0.10580  -0.69747
 -0.20188  -0.73042 -0.10286  -0.74858
 -0.10286  -0.74858 -0.10100  -0.74967
  0.00000  -0.75108  0.00000  -0.75792
  0.00000  -0.75108 -0.10286  -0.74858
 -0.10286  -0.74858  0.00000  -0.75792
 -0.39251  -0.63509 -0.30418  -0.66916
 -0.30418  -0.66916 -0.29997  -0.69210
 -0.20794  -0.69023 -0.20707  -0.71842
 -0.20794  -0.69023 -0.30418  -0.66916
 -0.29997  -0.69210 -0.20707  -0.71842
 -0.20188  -0.73042 -0.20707  -0.71842
 -0.30418  -0.66916 -0.20707  -0.71842
  0.00000  -0.75108 -0.10413  -0.73103
 -0.20707  -0.71842 -0.10286  -0.74858
 -0.20794  -0.69023 -0.10413  -0.73103
 -0.20707  -0.71842 -0.10413  -0.73103
 -0.10286  -0.74858 -0.10413  -0.73103
 -0.39251  -0.63509 -0.47125  -0.58915
 -0.53903  -0.53269 -0.47125  -0.58915
 -0.47125  -0.58915 -0.47069  -0.59209
 -0.39844  -0.64469 -0.47125  -0.58915
 -0.39251  -0.63509 -0.29997  -0.69210
 -0.39251  -0.63509 -0.39844  -0.64469
 -0.30331  -0.69297 -0.29997  -0.69210
 -0.20188  -0.73042 -0.29997  -0.69210
 -0.29997  -0.69210 -0.39844  -0.64469
  0.00000  -0.69038  0.10580  -0.69747
  0.00000  -0.69038  0.00000  -0.72871
  0.10413  -0.73103  0.10580  -0.69747
  0.20794  -0.69023  0.10580  -0.69747
  0.00000  -0.75108  0.00000  -0.72871
  0.00000  -0.72871  0.10413  -0.73103
  0.10580  -0.69747  0.00000  -0.72871
  0.29997  -0.69210  0.30418  -0.66916
  0.39251  -0.63509  0.30418  -0.66916
  0.20188  -0.73042  0.20707  -0.71842
  0.20707  -0.71842  0.29997  -0.69210
  0.20794  -0.69023  0.30418  -0.66916
  0.20794  -0.69023  0.20707  -0.71842
  0.20707  -0.71842  0.30418  -0.66916
  0.00000  -0.75108  0.10286  -0.74858
  0.00000  -0.75108  0.00000  -0.75792
  0.10100  -0.74967  0.10286  -0.74858
  0.20188  -0.73042  0.10286  -0.74858
  0.00000  -0.75792  0.10286  -0.74858
  0.20794  -0.69023  0.10413  -0.73103
  0.10286  -0.74858  0.20707  -0.71842
  0.00000  -0.75108  0.10413  -0.73103
  0.10286  -0.74858  0.10413  -0.73103
  0.20707  -0.71842  0.10413  -0.73103
  0.39251  -0.63509  0.47125  -0.58915
  0.47069  -0.59209  0.47125  -0.58915
  0.53903  -0.53269  0.47125  -0.58915
  0.47125  -0.58915  0.39844  -0.64469
  0.39251  -0.63509  0.39844  -0.64469
  0.39251  -0.63509  0.29997  -0.69210
  0.20188  -0.73042  0.29997  -0.69210
  0.29997  -0.69210  0.30331  -0.69297
  0.39844  -0.64469  0.29997  -0.69210
  0.00000  -0.69038  0.06675  -0.64522
  0.06675  -0.64522  0.10580  -0.69747
  0.13387  -0.58513  0.17412  -0.64791
/*	$OpenBSD: getopt_long.c,v 1.21 2006/09/22 17:22:05 millert Exp $	*/
/*	$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $	*/

/*
 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Sponsored in part by the Defense Advanced Research Projects
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
 */
/*-
 * Copyright (c) 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Dieter Baron and Thomas Klausner.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "getopt.h"

#ifdef __cplusplus
extern "C" {
#endif

#if 0
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#endif

#ifdef NOCDEFS
/*#include "cdefs.h"*/
#else
#include <sys/cdefs.h>
#endif
#ifdef FBSD
__FBSDID("$FreeBSD: src/lib/libc/stdlib/getopt_long.c,v 1.15 2006/09/23 14:48:31 ache Exp $");
#endif

#ifndef NOCDEFS
#include <err.h>
#include <errno.h>
#endif
#include "getopt.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#define GNU_COMPATIBLE		/* Be more compatible, configure's use us! */

#if 0				/* we prefer to keep our getopt(3) */
#define	REPLACE_GETOPT		/* use this getopt as the system getopt(3) */
#endif

#ifdef REPLACE_GETOPT
int	opterr = 1;		/* if error message should be printed */
int	optind = 1;		/* index into parent argv vector */
int	optopt = '?';		/* character checked for validity */
int	optreset;		/* reset getopt */
char    *optarg;		/* argument associated with option */
#endif

#define PRINT_ERROR	((opterr) && (*options != ':'))

#define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
#define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
#define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */

/* return values */
#define	BADCH		(int)'?'
#define	BADARG		((*options == ':') ? (int)':' : (int)'?')
#define	INORDER 	(int)1

#define	EMSG		""

#ifdef GNU_COMPATIBLE
#define NO_PREFIX	(-1)
#define D_PREFIX	0
#define DD_PREFIX	1
#define W_PREFIX	2
#endif

static int getopt_internal(int, char * const *, const char *,
			   const struct option *, int *, int);
static int parse_long_options(char * const *, const char *,
			      const struct option *, int *, int, int);
static int gcd(int, int);
static void permute_args(int, int, int, char * const *);

static char *place = EMSG; /* option letter processing */

/* XXX: set optreset to 1 rather than these two */
static int nonopt_start = -1; /* first non option argument (for permute) */
static int nonopt_end = -1;   /* first option after non options (for permute) */

/* Error messages */
static const char recargchar[] = "option requires an argument -- %c";
static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
#ifdef GNU_COMPATIBLE
static int dash_prefix = NO_PREFIX;
static const char gnuoptchar[] = "invalid option -- %c";

static const char recargstring[] = "option `%s%s' requires an argument";
static const char ambig[] = "option `%s%.*s' is ambiguous";
static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
static const char illoptstring[] = "unrecognized option `%s%s'";
#else
static const char recargstring[] = "option requires an argument -- %s";
static const char ambig[] = "ambiguous option -- %.*s";
static const char noarg[] = "option doesn't take an argument -- %.*s";
static const char illoptstring[] = "unknown option -- %s";
#endif

#ifdef NOCDEFS
void warnx( const char *fmt, ... );
void vwarnx( const char *fmt, va_list ap );

void
warnx(const char *fmt, ...)
{
        va_list ap;
        va_start(ap, fmt);
        vwarnx(fmt, ap);
        va_end(ap);
}
void
vwarnx(const char *fmt, va_list ap)
{
        fprintf(stderr, "vwarnx: ");
        if (fmt != NULL)
                vfprintf(stderr, fmt, ap);
        fprintf(stderr, "\n");
}

#endif

/*
 * Compute the greatest common divisor of a and b.
 */
static int
gcd(int a, int b)
{
	int c;

	c = a % b;
	while (c != 0) {
		a = b;
		b = c;
		c = a % b;
	}

	return (b);
}

/*
 * Exchange the block from nonopt_start to nonopt_end with the block
 * from nonopt_end to opt_end (keeping the same order of arguments
 * in each block).
 */
static void
permute_args(int panonopt_start, int panonopt_end, int opt_end,
	char * const *nargv)
{
	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
	char *swap;

	/*
	 * compute lengths of blocks and number and size of cycles
	 */
	nnonopts = panonopt_end - panonopt_start;
	nopts = opt_end - panonopt_end;
	ncycle = gcd(nnonopts, nopts);
	cyclelen = (opt_end - panonopt_start) / ncycle;

	for (i = 0; i < ncycle; i++) {
		cstart = panonopt_end+i;
		pos = cstart;
		for (j = 0; j < cyclelen; j++) {
			if (pos >= panonopt_end)
				pos -= nnonopts;
			else
				pos += nopts;
			swap = nargv[pos];
			/* LINTED const cast */
			((char **) nargv)[pos] = nargv[cstart];
			/* LINTED const cast */
			((char **)nargv)[cstart] = swap;
		}
	}
}

/*
 * parse_long_options --
 *	Parse long options in argc/argv argument vector.
 * Returns -1 if short_too is set and the option does not match long_options.
 */
static int
parse_long_options(char * const *nargv, const char *options,
	const struct option *long_options, int *idx, int short_too, int flags)
{
	char *current_argv, *has_equal;
#ifdef GNU_COMPATIBLE
	char *current_dash;
#endif
	size_t current_argv_len;
	int i, match, exact_match, second_partial_match;

	current_argv = place;
#ifdef GNU_COMPATIBLE
	switch (dash_prefix) {
		case D_PREFIX:
			current_dash = "-";
			break;
		case DD_PREFIX:
			current_dash = "--";
			break;
		case W_PREFIX:
			current_dash = "-W ";
			break;
		default:
			current_dash = "";
			break;
	}
#endif
	match = -1;
	exact_match = 0;
	second_partial_match = 0;

	optind++;

	if ((has_equal = strchr(current_argv, '=')) != NULL) {
		/* argument found (--option=arg) */
		current_argv_len = has_equal - current_argv;
		has_equal++;
	} else
		current_argv_len = strlen(current_argv);

	for (i = 0; long_options[i].name; i++) {
		/* find matching long option */
		if (strncmp(current_argv, long_options[i].name,
		    current_argv_len))
			continue;

		if (strlen(long_options[i].name) == current_argv_len) {
			/* exact match */
			match = i;
			exact_match = 1;
			break;
		}
		/*
		 * If this is a known short option, don't allow
		 * a partial match of a single character.
		 */
		if (short_too && current_argv_len == 1)
			continue;

		if (match == -1)        /* first partial match */
			match = i;
		else if ((flags & FLAG_LONGONLY) ||
			 long_options[i].has_arg !=
			     long_options[match].has_arg ||
			 long_options[i].flag != long_options[match].flag ||
			 long_options[i].val != long_options[match].val)
			second_partial_match = 1;
	}
	if (!exact_match && second_partial_match) {
		/* ambiguous abbreviation */
		if (PRINT_ERROR)
			warnx(ambig,
#ifdef GNU_COMPATIBLE
			     current_dash,
#endif
			     (int)current_argv_len,
			     current_argv);
		optopt = 0;
		return (BADCH);
	}
	if (match != -1) {		/* option found */
		if (long_options[match].has_arg == no_argument
		    && has_equal) {
			if (PRINT_ERROR)
				warnx(noarg,
#ifdef GNU_COMPATIBLE
				     current_dash,
#endif
				     (int)current_argv_len,
				     current_argv);
			/*
			 * XXX: GNU sets optopt to val regardless of flag
			 */
			if (long_options[match].flag == NULL)
				optopt = long_options[match].val;
			else
				optopt = 0;
#ifdef GNU_COMPATIBLE
			return (BADCH);
#else
			return (BADARG);
#endif
		}
		if (long_options[match].has_arg == required_argument ||
		    long_options[match].has_arg == optional_argument) {
			i
/*	$NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $	*/
/*	$FreeBSD: src/include/getopt.h,v 1.6 2004/02/24 08:09:20 ache Exp $ */

/*-
 * Copyright (c) 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Dieter Baron and Thomas Klausner.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifdef __cplusplus
extern "C" {
#endif

#ifndef _GETOPT_H_
#define _GETOPT_H_

#ifdef _MSC_VER
#define NOCDEFS
#endif

#ifdef NOCDEFS
/*#include "cdefs.h"*/
#if defined(__cplusplus)
#define __BEGIN_DECLS   extern "C" {
#define __END_DECLS     }
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif

#define _CRT_SECURE_NO_WARNINGS
#define REPLACE_GETOPT

#else
#include <sys/cdefs.h>
#endif

/*
 * GNU-like getopt_long()/getopt_long_only() with 4.4BSD optreset extension.
 * getopt() is declared here too for GNU programs.
 */
#define no_argument        0
#define required_argument  1
#define optional_argument  2

struct option {
	/* name of long option */
	const char *name;
	/*
	 * one of no_argument, required_argument, and optional_argument:
	 * whether option takes an argument
	 */
	int has_arg;
	/* if not NULL, set *flag to val when option found */
	int *flag;
	/* if flag not NULL, value to set *flag to; else return value */
	int val;
};

__BEGIN_DECLS
int	getopt_long(int, char * const *, const char *,
	const struct option *, int *);
int	getopt_long_only(int, char * const *, const char *,
	const struct option *, int *);
#ifndef _GETOPT_DECLARED
#define	_GETOPT_DECLARED
int	 getopt(int, char * const [], const char *);

extern char *optarg;			/* getopt(3) external variables */
extern int optind, opterr, optopt;
#endif
#ifndef _OPTRESET_DECLARED
#define	_OPTRESET_DECLARED
extern int optreset;			/* getopt(3) external variable */
#endif
__END_DECLS
 
#endif /* !_GETOPT_H_ */

#ifdef __cplusplus
}
#endif

Bresenhamn's optimized algorithm initializes deltax, deltay and error AFTER swappings. Your program initializes them BEFORE swappings.
Why?..

I moved the appropriate declarations after checking steep and swapping x and y, but the image is still far wrong.

[b]My rasterizeLine Version 2[/b]

/*
RASTERIZELINE
	Rasterize lines and call Pixel to display them.
*/

//MY BRESENHAM VERSION: This is my version of Bresenham's algorithm which doesn't entirely work.
void rasterizeLine( Image &I, int x0, int y0, int x1, int y1 )
{
	int dx = x1-x0;
	int dy = y1-y0;
	int slope = dy/dx;
	//int d = 2*(y0-y1)*(1+x0)+(x1-x0)*(2*y0+1)+(2*x0*y1)-(2*x1*y0);	//from the Tiger book, p60
	//int D = 2*(y1-y0) - (x1-x0);
 	bool steep = (ABS(dy) > ABS(dx));		//higher rise than run?
	//int x = x0;								//
	//int y = y0;								//
	//int ystep;								//line moves up or down
	int xhold, yhold;						//for swapping

//	cout << "\nPRE-STEEP TEST\n";

	//not slanting down right
	if (steep)
	{	//swap x values
//		cout << "\nSTEEP: swapping x and y values\n";

		xhold=x0;
		x0=x1;
		x1=xhold;

		//swap y values
		yhold=y0;
		y0=y1;
		y1=yhold;		
	}

	//right to left
	if(x0 > x1)
	{
//		cout << "\nRIGHT TO LEFT: swapping x and y values to draw left to right\n";

		xhold=x0;
		x0=x1;
		x1=xhold;

		//swap y values
		yhold=y0;
		y0=y1;
		y1=yhold;	
	}


	int delta_x = x1 - x0;					//
	int delta_y = ABS(y1-y0);				//
	int error = delta_x/2;					
	int x = x0;								//
	int y = y0;								//
	int ystep;								//line moves up or down

	//line moves up to down - check midpoint
	if(y0 < y1)
	{
//		cout << "\nYSTEP IS POSITIVE\n";
		ystep=1;
	}

	//line moves down to up - check midpoint
	else
	{
//		"\nYSTEP IS NEGATIVE\n";
		ystep=-1;
	}

	for(x; x < x1; x++)
	{
		//x0 and y0 swapped with x1 and y1
		if(steep)
		{
//			"\nSTEEP PIXELIZATION\n";
			I(y, x) = Pixel(255, 255, 255);
			//I(x, y) = Pixel(255, 255, 255);
		}
	
		//no swapping
		else
		{
//			"\nPIXELIZATION, NOT STEEP\n";
			I(x, y) = Pixel(255, 255, 255);
		}

		error = error - delta_y;
		
		if(error < 0)
		{
			y = y + ystep;
			error += delta_x;
		}
	}	
}

With your suggestion and other tweaks, I got it to work! I noticed I didn't properly swap (x0 and y0) and (x1 and y1) the first time around. I tried swapping (x0 and x1) and (y0 and y1) both times to produce ickiness.

I changed imagex to this which allowed it to work.

int imagex(int resolution, float xcoord) //coord is x0 or x1
{
	int hn = resolution/2; //half resolution (half n)

	if(xcoord >= 0) //quad 1 or 4 - at least half n
	{
		return(rti((hn) + (xcoord*hn)));
	}

	else //quad 2 or 3
	{
		return(rti((hn) - (ABS(xcoord*hn))));
	}
}
This article has been dead for over six months. Start a new discussion instead.