## Lesson 9 Question Bonus Solution

The Partridge Family were neither partridges nor a family. Discuss.
chili
Posts: 3910
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

### Lesson 9 Question Bonus Solution

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!
Chili

Windo
Posts: 1
Joined: April 24th, 2012, 8:39 pm

### Re: Lesson 9 Question Bonus Solution

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.

KillerDonkey
Posts: 19
Joined: September 6th, 2012, 6:53 pm

### Re: Lesson 9 Question Bonus Solution

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!

thetoddfather
Posts: 338
Joined: October 1st, 2012, 9:53 pm

### Re: Lesson 9 Question Bonus Solution

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.

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

### Re: Lesson 9 Question Bonus Solution

Probably just your code.... : -P
ʕ •ᴥ•ʔ

MChapman85
Posts: 1
Joined: October 8th, 2012, 11:53 pm

### Re: Lesson 9 Question Bonus Solution

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.

Chipolata
Posts: 6
Joined: January 10th, 2013, 1:41 pm

### Re: Lesson 9 Question Bonus Solution

I'm also wondering about the animation.

Posts: 9
Joined: March 26th, 2013, 11:29 am

### Re: Lesson 9 Question Bonus Solution

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.

Mr. Serious
Posts: 4
Joined: February 10th, 2014, 6:38 pm
Location: Earth, USA

### Re: Lesson 9 Question Bonus Solution

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!

Pindrought
Posts: 432
Joined: September 26th, 2013, 4:57 pm
Location: Kentucky
Contact:

### Re: Lesson 9 Question Bonus Solution

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
}
}``````
PM me if you need to contact me. Thanks to all the helpful people on this forum especially to Chili.