Calculating points of a line

This is a discussion on Calculating points of a line within the C++ Programming forums, part of the General Programming Boards category; I am writing a function that takes two coordinates, then calculates the points in between and draw the line. The ...

  1. #1
    Wen Resu
    Join Date
    May 2003
    Posts
    219

    Calculating points of a line

    I am writing a function that takes two coordinates, then calculates the points in between and draw the line.
    The coordinates represent points in my array, the array is viewed by a print comand that prints it into a grid in console.
    The function works,
    output on
    0,2 -> 3,9 in a 10 by 10 grid would look like


    0000000000
    0000000000
    1000000000
    1000000000
    0100000000
    0100000000
    0100000000
    0010000000
    0010000000
    0001000000

    I'm wondering if i am doing this properly, or if there would be a better way to do it.
    Links to similar functions or the code to them would help, trying to learn how best to do this

    my code- i commented as best i could


    Code:
    void Line(int xs, int ys, int xe, int ye, int vl, int* array)
    {
    	// first calculate slope using  rise/run
    	float oy; // store last y coord		
    	bool first = true; //  to handle first time in loop
    
    	if (xe != xs) // can't have a slope of infinity
    	{
    
    		float s =  ((float)ye - (float)ys) / ((float)xe - (float)xs); // slope
    		float b = ys - ((float) xs * s); // the B in the equation y = ax + b
    		
    		for( float c = float(xs); c <= (float)xe; c++) // drawing loop
    		{
    			float y = (c * s) + b;  // calculate the y
    			if ( (y + .5) >= ceil(y) ) // means it was  x.5 or greater
    			{
    				y = ceil(y); //  round up
    			}
    			else
    			{
    				y = floor(y); // its less then x.5  so  round down
    			}
    			if (first) // first time in the loop
    			{
    				write(c, (int)y, vl, array);
    				oy = y;
    				first = false;
    			}
    			else // not first time in loop
    			{
    				for( int ty = oy ; ty < y; ty++)  // draw from last coord, draw line towards  next y, stop 1 before
    				{
    					write(c - 1, ty, vl, array);
    				}
    				oy = y;
    			}
    
    			
    		}
    		// last  point, should be xe, ye
    		write(xe, ye, vl, array);
    
    
    	}
    }
    Thanks for your time

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    If you stick with integers, you wouldnt have to worry about rounding the y, and I think you'll get pretty good results.

    Code:
    if (xe != xs)
    if this fails, won't you still want to draw a straight line up and down?

    why are you checking for the first iteration of the loop?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Are you trying to implement this
    http://www.google.com/search?q=bresenham+line+algorithm
    or something of your own?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Something of my own, but i had read up on Bresenham algo about 6 months ago, so you could say it was inspire.

    First iteration of loop thing is gone now, it was just a solution to a problem bu i found a better way.
    This work, draws the line way i figured it would. But i'll read s'more on Bresenham's algo to see how i can improve mine.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 02:22 PM
  2. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21