The Cartesian Coordinate System

So far, the pixels in our images always had the same color. When we produce images with multiple colors, we usually want to determine the color based on the position of the pixel in question.

MathMap allows access to the coordinates of the pixel being calculated. It supports two coordinate systems. The first one, which you are certainly familiar with, is the cartesian coordinate system. Each pixel has two coordinates, called x and y. The following figure illustrates the cartesian coordinate system:

The point labeled "O" is the origin of the coordinate system. Both its coordinates (x and y) are zero. The origin is always in the center of the image. The point "p" in the illustration has a value of 50 for the coordinate x and 20 for y. That's because it's 50 pixels to the right of the origin and 20 pixels above. As you can see, x increases by one as you go one pixel to the right, while y increases by one if you go one pixel up. Similarly, they decrease when going in the opposite directions. That means that pixels to the left of the origin have negative x coordinates and pixels below it have negative y coordinates.

Let's use this knowledge to produce an image which is black on the left, white on the right and has gray levels in between. We want to produce this image:

For the creation of this image, it helps to know that the constant W contains the width of the image to be created.

We know that we can use grayColor to produce a gray level. However, we need a number between 0 and 1 to get colors between black and white. Let's look at what we have. At the right edge of the image the value of x is half the width of the image (since the origin is always at the center), i.e. W/2. Similarly, at the left edge, it has the same absolute value, only it's negative, i.e. -W/2. In order to get 0 (for black) for the left edge, we can simply add W/2 to x. That gives us x+W/2 which is 0 at the left edge of the image. At the right edge, however, its value is W, instead of 1. We can solve this problem by simply dividing our expression by W, giving (x+W/2)/W. Now we have what we want, and we can give this expression as an argument to grayColor:

grayColor((x+W/2)/W)

Try to do the same for the y coordinate, i.e. make a gradient from bottom to top instead of from left to right. You might need to know that the constant H holds the height of the image. You could also try to combine both coordinates to produce a gradient which goes from the bottom left corner to the top right corner.

Next topic: Input Images