You can use Pythagorean’s theorem to calculate the distance from one point to another. For example, this code would calculate the distance from the point (8,18) to (200, 250):
distance = sqrt ( (8-200)*(8-200) + (18-250)*(18-250))
sqrt() is the square root function
.
Here’s some code I wrote:
float dist_from_center;
size(500,500);
background(0);
loadPixels();
for(int row = 0; row < 500; row++)
{
for (int column = 0; column < 500; column ++)
{
dist_from_center = sqrt((row-250)*(row-250) + (column-250)*(column-250));
pixels[row*width+column] = color(dist_from_center/354.0*255, 0, 0);
}
}
updatePixels();
Type in my code and run it.
Why did I divide dist_from_center by 354?
Change the program to shade from the bottom right corner instead. First create a variable called “dist_from_corner” and figure out the equation for that distance. And you won’t divide by 354 anymore, right? What will you divide by now?