Circle Algorithm

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
DoubleImpact
Posts: 4
Joined: July 3rd, 2012, 11:09 am

Circle Algorithm

Post by DoubleImpact » July 6th, 2012, 3:18 am

Chili,

I was reviewing the circle algorithm (lesson 11) and I was wondering, why do we bother adding in...

Code: Select all

          int x0 = 0.7071068f * rad + 0.5f;
I commented it out and a circle was produced just fine. The code that we put in D3DGraphics.cpp doesn't even make use of the x0 variable aside from that calculation.

Here's the code:

Code: Select all

void D3DGraphics::DrawCircle( int cx,int cy,int rad,int r,int g,int b )
{
	float radSqr = rad * rad;
	//int x0 = 0.7071068f * rad + 0.5f;  //cos(45deg)=0.7071068
		for( int x = 0; x <= rad; x++ )
		{
			int y = sqrt( radSqr - x * x ) + 0.5f;
			PutPixel( cx + x,cy + y,r,g,b ); //quadrant 1
			PutPixel( cx + y,cy + x,r,g,b ); //quadrant 1
			PutPixel( cx - x,cy + y,r,g,b ); //quadrant 2
			PutPixel( cx - y,cy + x,r,g,b ); //quadrant 2
			PutPixel( cx - x,cy - y,r,g,b ); //quadrant 3
			PutPixel( cx - y,cy - x,r,g,b ); //quadrant 3
			PutPixel( cx + x,cy - y,r,g,b ); //quadrant 4
			PutPixel( cx + y,cy - x,r,g,b ); //quadrant 4
		}
}

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: Circle Algorithm

Post by codinitup » July 6th, 2012, 8:11 am

I'm guessing it's for precision on the circle, but you may want to check the video again and see what he does. If I remember correctly he got that number by doing the following function

Code: Select all

sqrt(2) / 2
Now this isn't exactly what he did, but the equation is the same (root of 2, divided by 2).
MOOOOOO

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Circle Algorithm

Post by chili » July 6th, 2012, 9:27 am

Code: Select all

for( int x = 0; x <= rad; x++ )
This is wasteful brah. You missed something in the video. ;)
Chili

DoubleImpact
Posts: 4
Joined: July 3rd, 2012, 11:09 am

Re: Circle Algorithm

Post by DoubleImpact » July 8th, 2012, 1:39 am

I think I see now; so x to x0 represents all of the x coordinates of 1/8th of the circle? But x to rad represents all of the x coordinates of 1/4th of the circle, yet the pixels begin to taper off when travelling steeper along the y-axis. I guess what I'm missing is why does the formula with "rad" space out the pixels when they travel closely along the y-axis as opposed to the formula with "x0", which doesn't have that tapering problem.

And yes, looking at the code that I posted again, I do see how it's wasteful considering pixels would overlap other pixels.

One more thing: I don't know if you've done this already, but would you consider making a brief collection of notes/summaries of each lesson as a post on this forum? Sometimes a written explanation is easier to process, not to mention easier than jumping through a video to track down the specific point where something is explained.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Circle Algorithm

Post by chili » July 8th, 2012, 2:54 am

DoubleImpact wrote:I guess what I'm missing is why does the formula with "rad" space out the pixels when they travel closely along the y-axis as opposed to the formula with "x0", which doesn't have that tapering problem.
Not exactly sure what you're asking here brah.
DoubleImpact wrote:I don't know if you've done this already, but would you consider making a brief collection of notes/summaries of each lesson as a post on this forum? Sometimes a written explanation is easier to process, not to mention easier than jumping through a video to track down the specific point where something is explained.
Probably not. Just making these videos already eats up more of my free time than I can rightly justify, the only balancing factor being that I get some enjoyment out of doing it. I would not enjoy compiling a collection of notes. However, there is nothing to stop you from taking your own notes if you need them. In fact, the act of taking notes itself might even aid your studies. I wouldn't know, because I never found use for the damn things while I was at school. ;)
Chili

DoubleImpact
Posts: 4
Joined: July 3rd, 2012, 11:09 am

Re: Circle Algorithm

Post by DoubleImpact » July 8th, 2012, 3:33 am

chili wrote:
DoubleImpact wrote:I guess what I'm missing is why does the formula with "rad" space out the pixels when they travel closely along the y-axis as opposed to the formula with "x0", which doesn't have that tapering problem.
Not exactly sure what you're asking here brah.

This is the "tapering" I was referring to when neglecting to use the x0 variable:

Image

Code:

Code: Select all

void D3DGraphics::DrawCircle( int cx,int cy,int rad,int r,int g,int b )
{
	float radSqr = rad * rad;	
	for( int x = 0; x <= rad; x++ )			
	{		
		int y = sqrt( radSqr - x * x ) + 0.5f;	
		PutPixel( cx + x,cy + y,r,g,b );		//quadrant 1
	}
}
No tapering when using x0:

Image

Here is the "x0" code:

Code: Select all

void D3DGraphics::DrawCircle( int cx,int cy,int rad,int r,int g,int b )
{
	float radSqr = rad * rad;
	int x0 = 0.7071068f * rad + 0.5f;		
	for( int x = 0; x <= x0; x++ )		
	{
		int y = sqrt( radSqr - x * x ) + 0.5f;
		PutPixel( cx + x,cy + y,r,g,b );		//quadrant 1
		PutPixel( cx + y,cy + x,r,g,b );		//quadrant 1
	}
}
So why do pixels stretch further apart in the first case?

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Circle Algorithm

Post by chili » July 8th, 2012, 3:58 am

When the slope is greater than 1, you're moving more than 1 pixel in the y direction for every pixel of motion in the x direction, and thus the pixels become disconnected (in the y direction).
Chili

Post Reply