my idea for a draw line function

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: my idea for a draw line function

Post by viruskiller » July 11th, 2012, 11:00 pm

i think i can;t rely on my pc stability to benchmark stuff.. not sure what changed now,but before the results were the same everytime i've tested it,now it just looks like all draw line function work about same speed :?
her's a simpler version of first function i posted

Code: Select all

void D3DGraphics::DrawLine2(int x1,int y1,int x2,int y2,int r,int g,int b)
{

	int dx;
	int dy;
	double x=0;
	double y=0;
	double xratio;
	double yratio;
	if(x1>x2)          //this makes sure it always draws from left to right;
		{
		   int temp=x2;
			x2=x1;
			x1=temp;
			temp=y2;
			y2=y1;
			y1=temp;
		}
	dx = abs( x2 - x1 );
	dy = abs( y2 - y1 );
	if(dx!=0&&dy!=0)
		{
		if(dx>dy)          //close to horizontal line
			{
			yratio=(double)dy/(double)dx;
			xratio=1;
			}
		else if(dx<dy)            //close to vertical line
			{
			xratio=(double)dx/(double)dy;
			yratio=1;
			}
		else if(dx==dy)       //line must be drawn at 45 degree's in this case.
			{
				xratio=1;
				yratio=1;
			}
		}
	if(dx!=0&&dy!=0&&y2<y1)
		{
		yratio=-yratio;
		}

 if(dx==0&&dy!=0)                     //special condition ,the line is paralel with either x or y axis so the above forumla doesn't apply because of division by 0 case;
 {                                     
	 xratio=0;                          
	 if(y1>y2)
	 {
		 yratio=-1;
	 }
	 else
	 {
		 yratio=1;
	 }
 }
 if (dy==0&&dx!=0)
 {
	 yratio=0;
	
	 xratio=1;
 }
 x=x1;
 y=y1;
	if(x1!=x2||y1!=y2)
	{
	if(y2>y1||y1==y2)                     //draws the lines that make an angle of less than 45 degree's with the x axis
		{
		for(;(int)x<=x2&&(int)y<=y2;)              //the line drawing loop,it loops trough both x and y based on they'r ratio and it stops when it meets the end pixel coords.
			 {                                                                  
				PutPixel(((int)(x+0.5f)),((int)(y+0.5)),r,g,b);     
			 
				 x=x+xratio;
				 y=y+yratio;
			 }
		}
   if(y1>y2||y1==y2)                    //draws the lines that make an angle of less than 45 degree's with the x axis
	   {
	   for(;((int)x)<=x2&&((int)y)>=y2;)             
			 {                                                                 
				 PutPixel(x+0.5f,y+0.5,r,g,b);  
			
				 x+=xratio;
				 y+=yratio;
			 }
	   }
	
	

  }
	
}
i've quit shifting base x and y to 0,and the integers i used to pass to the if statements,also now it only draws from left to right.

i'd like to know if there's any way to count computing cycles for a function,that would help me truly test this functions:)

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: my idea for a draw line function

Post by LuX » July 12th, 2012, 12:16 am

If you have done the lessons to where we write strings on the screen, you can use a timer to see how long drawing that part takes in milliseconds.

And if you want, convert that to frames per second, but for a general idea it's smart to check the execute time for the actual calculation in milliseconds.
ʕ •ᴥ•ʔ

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: my idea for a draw line function

Post by viruskiller » July 12th, 2012, 1:56 pm

yeah..the thing is even that would not be acurate because framerate is not stable either when drawing lots of lines,and that's the only way to amplify the difference between functions if ther's any.
if i leave them to run fast and time them the diference will be small and can be mistaken with just speed variation.

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: my idea for a draw line function

Post by LuX » July 12th, 2012, 4:33 pm

I don't think there's a better way to benchmark it. Just run a loop of 1000 draw lines and check the time for that. Sure it's not 100% stable, but it's as accurate as you can get it.

Using a separate frame counter, like fraps for example, is not accurate because of vsync.
ʕ •ᴥ•ʔ

Post Reply