HELP WITH LESSON 12!!

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: HELP WITH LESSON 12!!

Post by LuX » June 9th, 2012, 4:55 pm

How you had it:

Code: Select all

void D3DGraphics::DrawLine ( int x1, int y1, int x2, int y2, int r, int g, int b )
{
	int dx = x2 - x1;
	int dy = y2 - y1;

	
	if ( abs ( dy ) > abs ( dx ) )
	{
		if ( y1 > y2 )
		{
			int temp = y2; 
			y2 = y1; 
			y1 = temp;
			temp = x2; 
			x2 = x1; 
			x1 = temp;
		}
		float m = (float)dx / (float)dy; 
		float b = x1 - m * y1;                        // <----- b = line equation
		for ( int y = y1; y <= y2; y++ )
	   {
		  int x = m*y + b + 0.5f;
		  PutPixel ( x,y,r,g,b );                       // <------ b = line equation
	   }
	}
	else
	{
		if ( x1 > x2 )
		{
			int temp = y2; 
			y2 = y1; 
			y1 = temp;
			temp = x2; 
			x2 = x1; 
			x1 = temp;
		}
	     float m =(float) dy / (float) dx; 
	     float b1 = y1 - m*x1;                            // <------ here you at least renamed it
    	 for ( int x = x1; x <= x2; x++ )
	   {
		  int y = m*x + b + 0.5f;                     // <------ but forgot to rename here
		  PutPixel ( x,y,r,g,b );                        // <------ still not blue here
	   }

	}
}
How it should be: (for simplicity I chose to change the "b" of the line equation to "k")

Code: Select all

void D3DGraphics::DrawLine ( int x1, int y1, int x2, int y2, int r, int g, int b )
{
	int dx = x2 - x1;
	int dy = y2 - y1;

	
	if ( abs ( dy ) > abs ( dx ) )
	{
		if ( y1 > y2 )
		{
			int temp = y2; 
			y2 = y1; 
			y1 = temp;
			temp = x2; 
			x2 = x1; 
			x1 = temp;
		}
		float m = (float)dx / (float)dy; 
		float k = x1 - m * y1;                                // "k" = line equation
		for ( int y = y1; y <= y2; y++ )
	   {
		  int x = m*y + k + 0.5f;                            // and here we use "k"
		  PutPixel ( x,y,r,g,b );                               // "b" is fine now since we didn't change it at all
	   }
	}
	else
	{
		if ( x1 > x2 )
		{
			int temp = y2; 
			y2 = y1; 
			y1 = temp;
			temp = x2; 
			x2 = x1; 
			x1 = temp;
		}
	     float m =(float) dy / (float) dx; 
	     float k = y1 - m*x1;                                 // Same stuff here
    	 for ( int x = x1; x <= x2; x++ )
	   {
		  int y = m*x + k + 0.5f;
		  PutPixel ( x,y,r,g,b );
	   }

	}
}
ʕ •ᴥ•ʔ

chilipotatoe
Posts: 7
Joined: June 9th, 2012, 12:17 pm

SUCCESS!!

Post by chilipotatoe » June 9th, 2012, 6:51 pm

Okay, i have no idea what i did, but i was trying to fix the problem. And i failed :(. So i redid the modifications i made, and for some reason IT STARTED WORKING. YAAAAY!!!

But thanks anyways for all the help you guys gave. I really appreciate it.
Attachments
success.png
(5.18 KiB) Downloaded 51 times

chilipotatoe
Posts: 7
Joined: June 9th, 2012, 12:17 pm

Success part 2

Post by chilipotatoe » June 9th, 2012, 6:58 pm

OH GOD! I feel like a COMPLETE idiot. I didn't even know that there was a second page, until i posted the comment above!
Thank you LUX i got the code you gave, and now i understand. The code fixed the problem even better!!!
THANKS A LOT!!!
Attachments
Chili DirectX Framework 2012-06-09 14-54-29-25.png
(5.14 KiB) Downloaded 51 times

Post Reply