Triangle - scanline filling

This is a discussion on Triangle - scanline filling within the C++ Programming forums, part of the General Programming Boards category; Hi guys I've come to a halt, i can't seem to figure out an efficient method of implementing a triangle ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Triangle - scanline filling

    Hi guys
    I've come to a halt, i can't seem to figure out an efficient method of implementing a triangle scan line algorithm or even a messy one that works. So far my function can take 3 points and obtain coordinates needed to connect the points, these coordinates are used for my triangle's edges. Now to use these edges in the scan line algorithm i need to figure out which edge coordinates belong on the left and right. I've managed to get a flat base triangle filled but any other triangle cases just have me stumped for logic :-(

    One idea i had was to concatenate the edge lists of 2 smaller sides of a triangle and somehow find which side it belongs to.

    Code:
    void Core::drawTriangle(Point a, Point b, Point c)
    {
    	vector<Point> AB;
    	vector<Point> AC;
    	vector<Point> BC;
    
    	vector<Point> lEdge;
    	vector<Point> rEdge;
    
    	makeLine(a, b, &AB);
    	makeLine(a, c, &AC);
    	makeLine(b, c, &BC);
    
            //this works for an equilateral triangle
            int i = 0;
            for (int y = a.y; y < b.y; y++)
    	{
    		for (int x = AC[i].x; x < AB[i].x; x++)
    		{
    			putpixel(x, y, 0, 200, 0, 0);
    		}
    		i++;
    	}
    }
    Last edited by alki; 03-20-2011 at 02:47 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 01:31 PM
  2. Help making Triangle
    By jensklemp in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2010, 02:59 PM
  3. Clipping a Triangle of Digits
    By towely in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2009, 07:00 AM
  4. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  5. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM

Tags for this Thread


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