Page 1 of 2

Lesson 9 Question Bonus Solution

Posted: February 13th, 2012, 2:23 pm
by chili

Code: Select all

	int loopy = y;

	while( loopy < y + size )
	{
		int loopx = x;

		while( loopx < x + size )
		{
			gfx.PutPixel( loopx,loopy,
				((loopx-200)*(loopx-200)+(loopy-200)*(loopy-200)) / 100,
				((loopx-500)*(loopx-500)+(loopy-400)*(loopy-400)) / 100,
				((loopx-300)*(loopx-300)+(loopy-100)*(loopy-100)) / 100 );
			loopx++;
		}
		loopy++;
	}
The basic idea is to use the distance from some point as the color for each pixel.
Distance between two points is √ ( (x - x0)² + (y - y0)² ), where the points are (x,y) and (x0,y0).
Since we didn't cover square roots yet I just didn't compute the final root, so what we actually have is distance². That's why the rings get progressively closer and closer spaced as you move further from the center point. That's also why I divide by 100 at the end. If you don't divide, the numbers are so large that the rings get too close together and it looks insane. Try it!

Re: Lesson 9 Question Bonus Solution

Posted: April 24th, 2012, 8:50 pm
by Windo
Thanks for sharing the code of measuring distance between two points. I think this is very effective code for measuring distance between two points. So, i want to try it.

Re: Lesson 9 Question Bonus Solution

Posted: September 7th, 2012, 4:21 pm
by KillerDonkey
I was wondering which variable is changed to make the colors move, do you have to make variables for the colors? i tried changing x and y at the very beginning of compose frame and nothing happened, please help!

Re: Lesson 9 Question Bonus Solution

Posted: October 1st, 2012, 10:18 pm
by thetoddfather
Ya, I couldn't figure out animating it either. I think it may be because my computer can't handle it. Tried incrementing the loopx and loopy variables with keyboard up, didn't change anything. Still looks cool though even if it isn't moving :)

Perhaps we need the enhancements Chili told us about to make it run more smoothly, but those aren't coming for a few more lessons if I understand correctly.

Re: Lesson 9 Question Bonus Solution

Posted: October 2nd, 2012, 6:52 am
by LuX
Probably just your code.... : -P

Re: Lesson 9 Question Bonus Solution

Posted: October 9th, 2012, 12:23 am
by MChapman85
Hi I'm new here and I was wondering about the actual animation for this lesson. I have got some kind of an animation but not the shrinking circles like what chilli has with out the square moving.

Thanks for responding and sorry to bring up an old topic but I searched everywhere on this forum and the web and came up short.

Re: Lesson 9 Question Bonus Solution

Posted: January 23rd, 2013, 4:16 pm
by Chipolata
I'm also wondering about the animation.

Re: Lesson 9 Question Bonus Solution

Posted: March 26th, 2013, 12:09 pm
by Mr Superbad
Hey guys - first post :)

I tinkered with this for a while - I had the old trig' books out and everything. I cant say I get Chilli's solution but I did manage to get it to animate. Stuck a variable at the end of the sum e.g '/ 100 - x3' and increased/decreased x3 by 1 when space/enter pressed. Seems to do what Chilli's does but my machine is from the arc so its not as smooth.

Re: Lesson 9 Question Bonus Solution

Posted: February 10th, 2014, 8:42 pm
by Mr. Serious
Can anyone show all the Game.h and Game.cpp code used in chili's loopy rectangle (pun intended)? It's way beyond my knowledge and comprehension of programming thus far (I'm only on lesson 10 4 beginners) and I do not have a good grasp of intermediate let alone advanced geometry, algebra, trig, nor calculus so if anyone can help explain all the details please help!

Re: Lesson 9 Question Bonus Solution

Posted: February 10th, 2014, 10:37 pm
by Pindrought
Mr. Serious wrote:Can anyone show all the Game.h and Game.cpp code used in chili's loopy rectangle (pun intended)? It's way beyond my knowledge and comprehension of programming thus far (I'm only on lesson 10 4 beginners) and I do not have a good grasp of intermediate let alone advanced geometry, algebra, trig, nor calculus so if anyone can help explain all the details please help!
Does this help?

Code: Select all

int TopLeftXOfSquare=100;
int TopLeftYOfSquare=100;
int SquareWidth=100;
int SquareHeight=150;
for (int i=0;i<SquareWidth;i++)
{
	for (int a=0;a<SquareHeight;a++)
	{
		int MidPointX=TopLeftXOfSquare+SquareWidth/2; 
		//Sets the X coordinate to draw circle from
		int MidPointY=TopLeftYOfSquare+SquareHeight/2; 
		//Sets the Y coordinate to draw circle from
		int XPoint = i+TopLeftXOfSquare; 
		//X Coordinate for pixel
		int YPoint = a+TopLeftYOfSquare; 
		//Y Coordinate for pixel
		int XDistance = (i+TopLeftXOfSquare-MidPointX)*(i+TopLeftXOfSquare-MidPointX); 
		//Calculates the x^2 to plug into the equation Distance = sqrt(x^2 + y^2)
		int YDistance = (a+TopLeftXOfSquare-MidPointY)*(a+TopLeftXOfSquare-MidPointY); 
		//Calculates the y^2 to plug into the equation Distance = sqrt(x^2 + y^2)
		gfx.PutPixel(
			XPoint, //First Parameter, this is the value of our X coordinate for the pixel we're drawing
			YPoint, //2nd Parameter, this is the value of the y coordinate for the pixel we're drawing
			(XDistance+YDistance) / 100, //Red value
			(XDistance+YDistance) / 100, //Green Value
			(XDistance+YDistance) / 100 ); //Blue Value
	}
}
for a neat blue circle pattern, try this out

Code: Select all

int TopLeftXOfSquare=100;
int TopLeftYOfSquare=100;
int SquareWidth=100;
int SquareHeight=150;
for (int i=0;i<SquareWidth;i++)
{
	for (int a=0;a<SquareHeight;a++)
	{
		int MidPointX=TopLeftXOfSquare+SquareWidth/2; 
		//Sets the X coordinate to draw circle from
		int MidPointY=TopLeftYOfSquare+SquareHeight/2; 
		//Sets the Y coordinate to draw circle from
		int XPoint = i+TopLeftXOfSquare; 
		//X Coordinate for pixel
		int YPoint = a+TopLeftYOfSquare; 
		//Y Coordinate for pixel
		int XDistance = (i+TopLeftXOfSquare-MidPointX)*(i+TopLeftXOfSquare-MidPointX); 
		//Calculates the x^2 to plug into the equation Distance = sqrt(x^2 + y^2)
		//Since we used 150, it calculates the distance from the X point 150
		int YDistance = (a+TopLeftXOfSquare-MidPointY)*(a+TopLeftXOfSquare-MidPointY); 
		//Calculates the x^2 to plug into the equation Distance = sqrt(x^2 + y^2)
		//Since we used 150, it calculates the distance from the Y point 150
		gfx.PutPixel(
			XPoint, //First Parameter, this is the value of our X coordinate for the pixel we're drawing
			YPoint, //2nd Parameter, this is the value of the y coordinate for the pixel we're drawing
			(XDistance+YDistance) / 100, //Red value
			(XDistance+YDistance) / 100, //Green Value
			(XDistance+YDistance)*200 / 100 ); //Blue Value
	}
}