Friday, 22 January 2010

Chunk 21 - FINAL

Coordinate Systems and Pixels
A pixel (from the words picture element) is generally thought to be the smallest part of a digital image, be that a picture or text. Although this isn't strictly true (as there is such as thing as a sub-pixel) it is the smallest part over which we have control when creating a digital image. Each pixel is a block of colour that, when combined with many others, make up an image. Digital cameras have brought the word 'pixel' into popular use by describing the quality of the images the camera produces in terms of 'megapixels'. A megapixel is 1 million pixels – the greater the number of pixels that constitute an image, the greater the quality of that image.

Computer displays are often described in terms of their 'resolution' which is measured in the number of pixels horizontally and vertically. For example, a resolution of 1920 x 1200 would contain 2,304,000 pixels. In Processing, the viewable area is also defined in this way using the size() function, which takes 2 arguments: the number of horizontal pixels and the number of vertical pixels (there is also an optional 3rd argument that sets the display mode, but we shall use the 2 argument version) for example

size(600, 400);

If the size() function is not used, Processing uses a default size of 100 x 100 pixels. The displayed screen is like a map of pixels. Each pixel is defined as its position along each axis – the horizontal (x) axis and the vertical (y) axis in the form (x, y), for example the pixel at (100,200) is 100 pixels along the horizontal axis, and 200 pixels along the vertical axis

The displayable area is like a grid, with the number of pixels measured from the top-left point, position (0, 0) and increasing along each axis. As shown in figure 21.1, the coordinates of any single pixel can ascertained by providing the x and y coordinates in relation to the starting point at position (0, 0). As examples, the bottom right of a 600 x 400 display is at position (600, 400) and the middle is at position (300, 200).

Figure 21.1 Coordinates of the Processing screen

We can use the coordinates to draw a point by providing them as the arguments to the point() function in the form point(x, y). For example

size(600, 400);
point(300, 200);

will draw a point in the middle of the screen. Any set of coordinates can be used to draw a point. Provided the coordinates are within the range of those of the screen, then the point will be visible. If coordinates are used that are not within that range, for example a negative number or a number greater than the greatest x or y position, the point will not be visible. The coordinates at the limit of the screen's displayable area are available in 2 of Processing's built-in variables; height and width. These are set automatically after the size() function, as shown above, so can be used to find out what the limit of the coordinates are. Therefore the point

point(width, height);

is the point at the bottom-right of the screen.

To help us identify the coordinates of a pixel, we can write a small program that will allow us to click inside the window and have it tell us the coordinates of where we clicked.

void draw() {

}

void mouseClicked() {
// draw the point
point(mouseX, mouseY);
// output the coordinates
println(mouseX + "," + mouseY);

}

The mouse functions are explained later in the book, so you don't need to understand them now, other than that mouseX and mouseY hold the current coordinates of the mouse pointer. If you click the mouse inside the window, a dot will be printed at the coordinates and the coordinates themselves will be printed in the output text area of the Processing window. You can click as many times as you want and see the coordinates of the point you just created


Figure 21.2 Creating points on the screen


Figure 21.3 The Processing window

As each point is a single pixel, it is also the smallest part of an image for which we can specify the colour. The stroke() function can be used to set the colour of the next pixel drawn by providing a set of arguments that specify the colour. Colour in Processing is explored fully later in the book, so for now all you need to know is that the colour can be set by providing 3 arguments to the stroke() function. We can alter our program above to display different colour points by adding the stroke() function above the point() function call.

void setup() {
// set background to black
background(0);
}

void draw() {

}

void mouseClicked() {
// create a random colour
stroke(random(255), random(255), random(255));
// draw the point
point(mouseX, mouseY);
// output the coordinates
println(mouseX + "," + mouseY);
}

As you can see, we have added the stroke() function call and, to make it a little more interesting, randomised the colours. Each time the mouse is clicked, a new random colour is generated and the point is drawn in that colour. The only other change is that we have set the background to be black as it makes the colours easier to see

Figure 21.4 Pixels in random colours

Processing offers another way of changing individual pixels. As we explained before, the pixels in the displayable area are like a grid. The grid is the size of the number of pixels that are displayed, so a 600 x 400 screen will have 240,000 pixels. The grid of pixels can be retrieved and altered using the loadPixels() and updatePixels() functions. Using the loadPixels() function means that an array of pixels, called pixels[], can be accessed.
Each element in the pixels[] array contains the colour of the associated pixel, where element pixels[0] is the top-left pixel, and element pixels[pixels.length -1] is the bottom-right pixel (where pixels.length is the size of the pixels[] array – we need to remove 1 because the array starts from 0, so the last element is 1 less that the size).
We can use this to access and update the pixels by altering the colour value of pixels, as we have done below

// set background to black
background(0);
// create the colour magenta
color magenta = color(255, 0, 255);
// load the pixels
loadPixels();
// loop through half of the pixel array
for (int i = 0; i < pixels.length/2; i++) {
// set the pixel to be magenta
pixels[i] = magenta;
}
// update the pixels so they are displayed
updatePixels();

If you run this, you will see that we have changed half of the pixels to a lovely magenta colour, which has the affect of changing half of the screen to that colour, and leaving the rest as the colour of the background. To be able to do this, you need to both load and update the pixels. Attempting to access the pixels[] array without first loading them will result in an error. To display your changes to the pixels[] array, they must then be updated in order to tell Processing to display them – if you miss this out then the changes are simply not displayed.

Figure 21.5 Half the pixels altered

In order to draw images, such as a square, the relative coordinates of the lines would need to be calculated. Processing has a specific function for drawing shapes such as squares, which are covered later in the book, so we won't explain how it works, but we will use it to draw a square 100 pixels wide and then explain how it works in terms of pixels.

size(400, 400);
rect(100, 100, 100, 100);

The above will result in a square being drawn at coordinates (100,100) of size 100 x 100 (the 4 arguments to the rect() function above).


Figure 21.6 A square drawn at coordinates (100, 100)

The size of the square, determined by the arguments, is also specified in pixels, so the square we drew above is 100 pixels wide by 100 pixels high. We have specified that the square should be drawn at coordinates (100, 100). By default, Processing will take that to mean the top-left corner of the square

As a final way of demonstrating coordinates and pixels, we will create a small program that will allow you to click the coordinates of a quadrilateral (4 sided) shape, which will then be drawn on the screen. Again, you needn't understand all of this code at this point in the book, except to understand that it stores the coordinates you click with the left mouse button until you have clicked 4 times, then draws a shape using the coordinates you have clicked.

// arrays for storing the coordinates
int[] xCoordinates;
int[] yCoordinates;
// the current stored coordinate
int currentCoordinate;

void setup() {
// set the window size
size(400, 400);
// black background
background(0);
// white lines
stroke(255);
// initialise the arrays
xCoordinates = new int[4];
yCoordinates = new int[4];
// set the current coordinate to less than 0 to show none
currentCoordinate = -1;
// smooth lines
smooth();
}

void draw() {
// if we have entered all 4 coordinates
if (currentCoordinate == 3) {
// draw a 4-sided shape using the coordinates
quad(xCoordinates[0], yCoordinates[0], xCoordinates[1], yCoordinates[1], xCoordinates[2], yCoordinates[2],
xCoordinates[3], yCoordinates[3]);
}
}

void mouseClicked() {
// if the left mouse button is clicked
if (mouseButton == LEFT) {
// advance the current coordinate by 1
currentCoordinate++;
// store the coordinates
xCoordinates[currentCoordinate] = mouseX;
yCoordinates[currentCoordinate] = mouseY;
// print the clickec coordinates
println(mouseX + ", " + mouseY);
// if the right button is clicked
} else if (mouseButton == RIGHT) {
// clear the screen
background(0);
// reinitialise the shape
currentCoordinate = -1;
}

}


If you run this you will see that that you can click any 4 coordinates (which are displayed in the text area in the Processing window) and a shape will be drawn using the coordinates you clicked. You can right-click the mouse to clear the current shape and start again

Figure 21.7 A 4-sided shape drawn using coordinates

2nd Program

For the 2nd program I have gone for a space battle simulation. The target ship moves across the screen, displaying its coordinates. The target sensor can be moved by the mouse and also displays its current coordinates.

Left click to fire
Right click for a new ship





/**
* Chunk 21 program for demonstrating coordinates.
* The program simulates a space battle where the current coordinates
* of the target sensor and the target ship are displayed.
* If, on clicking the left mouse button, the coordinates of the click
are
* approximate to that of the ship, 1 is scored. Click the right mouse
* button for a new ship
* @author Antony Lees
*/

// colours used in the program
int white = color(255);
int black = color(0);
// ship direction
float xDirection;
float yDirection;
// coordinates of the ship
float[] shipCoordinates;
// whether the ship should be shown
boolean showShip;
// current score
int score;

void setup() {
// screen size
size (600,400);
// black background
background(black);
// white lines
stroke(white);
// smooth edges
smooth();
// draw rectangles from the centre
rectMode(CENTER);
// load font for the text
hint(ENABLE_NATIVE_FONTS);
PFont font = loadFont();
textFont(font);
// initialise the ship
initialiseShip();
// set score to 0
score = 0;
}

/**
* Initialise the ship for display
*/
void initialiseShip() {
// choose random direction
xDirection = random(-2, 2);
yDirection = random(-2, 2);
// start in the middle of the screen
shipCoordinates = new float[]{width/2, height/2};
// set the ship to be shown
showShip = true;
}

/**
* Loads a font for the text
* @return the font
*/
PFont loadFont() {
// get all the available font names
String[] fonts = PFont.list();
// try and find Times New Roman
for (int i = 0; i < fonts.length; i++) {
if (fonts[i].equals("Times New Roman")) {
// return the font, size 12
return createFont("Times New Roman", 12);
}
}
// if we got here, Times New Roman wasn't there
// just load the first one, size 12
return createFont(fonts[0], 12);
}

void draw() {
// redraw the background
background(black);
// display the current mouse coordinates
text(mouseX + "," + mouseY, mouseX, mouseY);
// draw the targeting lines so that they leave a space around the mouse pointer
line(0, 0, mouseX - 20, mouseY- 20);
line(width, 0, mouseX + 20, mouseY - 20);
line(0, height, mouseX - 20, mouseY + 20);
line(width, height, mouseX + 20, mouseY + 20);
// display the ship
drawSpaceship();
// display the score
displayScore();
}

/**
* Draws the ship if required, and controls whether it has left the
screen or not
*/
void drawSpaceship() {
// if we should show the ship
if (showShip) {
// draw the ship
rect(shipCoordinates[0], shipCoordinates[1], 5, 5);
line(shipCoordinates[0] - 10, shipCoordinates[1] - 10,
shipCoordinates[0] - 10, shipCoordinates[1] + 10);
line(shipCoordinates[0] + 10, shipCoordinates[1] - 10,
shipCoordinates[0] + 10, shipCoordinates[1] + 10);
line(shipCoordinates[0], shipCoordinates[1], shipCoordinates[0]
- 10, shipCoordinates[1]);
line(shipCoordinates[0], shipCoordinates[1], shipCoordinates[0]
+ 10, shipCoordinates[1]);
// display its coordinates
text((int) shipCoordinates[0] + "," + (int) shipCoordinates[1],
shipCoordinates[0], shipCoordinates[1] + 25);
// update coordinates for next time
shipCoordinates[0] = shipCoordinates[0] + xDirection;
shipCoordinates[1] = shipCoordinates[1] + yDirection;
}
// if current coordinates mean the ship has left the screen
if (shipCoordinates[0] < 0 || shipCoordinates[0] > width ||
shipCoordinates[1] < 0 || shipCoordinates[1] > height) {
// set to not be shown next time
showShip = false;
}

}

/**
* Display the current score
*/
void displayScore() {
// display near the top of the screen
text("Score: " + score, width/2, 10);
}


void mouseClicked() {
// if the left mouse button was clicked
if (mouseButton == LEFT) {
// if the button was pressed in the regiomn of the ship
if (mouseX > (shipCoordinates[0] - 10) && mouseX <
(shipCoordinates[0] + 10)) {
// add 1 to the score
score++;
// set the ship to not be shown
showShip = false;
}
}

// if the right mouse button was clicked
// and we aren't currently displaying the ship
if (mouseButton == RIGHT && showShip == false) {
// create a new ship
initialiseShip();
}
}





First 2000 words or so (draft)

Coordinate Systems and Pixels
A pixel (from the words picture element) is generally thought to be the smallest part of a digital image, be that a picture or text. Although this isn't strictly true (as there is such as thing as a sub-pixel) it is the smallest part over which we have control when creating a digital image. Each pixel is a block of colour that, when combined with many others, make up an image. Digital cameras have brought the word 'pixel' into popular use by describing the quality of the images the camera produces in terms of 'megapixels'. A megapixel is 1 million pixels – the greater the number of pixels that constitute an image, the greater the quality of that image.

Computer displays are often described in terms of their 'resolution' which is measured in the number of pixels horizontally and vertically. For example, a resolution of 1920 x 1200 would contain 2,304,000 pixels. In Processing, the viewable area is also defined in this way using the size() function, which takes 2 arguments: the number of horizontal pixels and the number of vertical pixels (there is also an optional 3rd argument that sets the display mode, but we shall use the 2 argument version) for example

size(600, 400);

If the size() function is not used, Processing uses a default size of 100 x 100 pixels. The displayed screen is like a map of pixels. Each pixel is defined as its position along each axis – the horizontal (x) axis and the vertical (y) axis in the form (x, y), for example the pixel at (100,200) is 100 pixels along the horizontal axis, and 200 pixels along the vertical axis

The displayable area is like a grid, with the number of pixels measured from the top-left point, position (0, 0) and increasing along each axis. As shown in figure 21.1, the coordinates of any single pixel can ascertained by providing the x and y coordinates in relation to the starting point at position (0, 0). As examples, the bottom right of a 600 x 400 display is at position (600, 400) and the middle is at position (300, 200).

Figure 21.1 Coordinates of the Processing screen

We can use the coordinates to draw a point by providing them as the arguments to the point() function in the form point(x, y). For example

size(600, 400);
point(300, 200);

will draw a point in the middle of the screen. Any set of coordinates can be used to draw a point. Provided the coordinates are within the range of those of the screen, then the point will be visible. If coordinates are used that are not within that range, for example a negative number or a number greater than the greatest x or y position, the point will not be visible. The coordinates at the limit of the screen's displayable area are available in 2 of Processing's built-in variables; height and width. These are set automatically after the size() function, as shown above, so can be used to find out what the limit of the coordinates are. Therefore the point

point(width, height);

is the point at the bottom-right of the screen.

To help us identify the coordinates of a pixel, we can write a small program that will allow us to click inside the window and have it tell us the coordinates of where we clicked.

void draw() {

}

void mouseClicked() {
// draw the point
point(mouseX, mouseY);
// output the coordinates
println(mouseX + "," + mouseY);

}

The mouse functions are explained later in the book, so you don't need to understand them now, other than that mouseX and mouseY hold the current coordinates of the mouse pointer. If you click the mouse inside the window, a dot will be printed at the coordinates and the coordinates themselves will be printed in the output text area of the Processing window. You can click as many times as you want and see the coordinates of the point you just created


Figure 21.2 Creating points on the screen


Figure 21.3 The Processing window

As each point is a single pixel, it is also the smallest part of an image for which we can specify the colour. The stroke() function can be used to set the colour of the next pixel drawn by providing a set of arguments that specify the colour. Colour in Processing is explored fully later in the book, so for now all you need to know is that the colour can be set by providing 3 arguments to the stroke() function. We can alter our program above to display different colour points by adding the stroke() function above the point() function call.

void setup() {
// set background to black
background(0);
}

void draw() {

}

void mouseClicked() {
// create a random colour
stroke(random(255), random(255), random(255));
// draw the point
point(mouseX, mouseY);
// output the coordinates
println(mouseX + "," + mouseY);
}

As you can see, we have added the stroke() function call and, to make it a little more interesting, randomised the colours. Each time the mouse is clicked, a new random colour is generated and the point is drawn in that colour. The only other change is that we have set the background to be black as it makes the colours easier to see

Figure 21.4 Pixels in random colours

Processing offers another way of changing individual pixels. As we explained before, the pixels in the displayable area are like a grid. The grid is the size of the number of pixels that are displayed, so a 600 x 400 screen will have 240,000 pixels. The grid of pixels can be retrieved and altered using the loadPixels() and updatePixels() functions. Using the loadPixels() function means that an array of pixels, called pixels[], can be accessed.
Each element in the pixels[] array contains the colour of the associated pixel, where element pixels[0] is the top-left pixel, and element pixels[pixels.length -1] is the bottom-right pixel (where pixels.length is the size of the pixels[] array – we need to remove 1 because the array starts from 0, so the last element is 1 less that the size).
We can use this to access and update the pixels by altering the colour value of pixels, as we have done below

// set background to black
background(0);
// create the colour magenta
color magenta = color(255, 0, 255);
// load the pixels
loadPixels();
// loop through half of the pixel array
for (int i = 0; i < pixels.length/2; i++) {
// set the pixel to be magenta
pixels[i] = magenta;
}
// update the pixels so they are displayed
updatePixels();

If you run this, you will see that we have changed half of the pixels to a lovely magenta colour, which has the affect of changing half of the screen to that colour, and leaving the rest as the colour of the background. To be able to do this, you need to both load and update the pixels. Attempting to access the pixels[] array without first loading them will result in an error. To display your changes to the pixels[] array, they must then be updated in order to tell Processing to display them – if you miss this out then the changes are simply not displayed.

Figure 21.5 Half the pixels altered

In order to draw images, such as a square, the relative coordinates of the lines would need to be calculated. Processing has a specific function for drawing shapes such as squares, which are covered later in the book, so we won't explain how it works, but we will use it to draw a square 100 pixels wide and then explain how it works in terms of pixels.

size(400, 400);
rect(100, 100, 100, 100);

The above will result in a square being drawn at coordinates (100,100) of size 100 x 100 (the 4 arguments to the rect() function above).

Figure 21.6 A square drawn at coordinates (100, 100)

The size of the square, determined by the arguments, is also specified in pixels, so the square we drew above is 100 pixels wide by 100 pixels high. We have specified that the square should be drawn at coordinates (100, 100). By default, Processing will take that to mean the top-left corner of the square

As a final way of demonstrating coordinates and pixels, we will create a small program that will allow you to click the coordinates of a quadrilateral (4 sided) shape, which will then be drawn on the screen. Again, you needn't understand all of this code at this point in the book, except to understand that it stores the coordinates you click with the left mouse button until you have clicked 4 times, then draws a shape using the coordinates you have clicked.

// arrays for storing the coordinates
int[] xCoordinates;
int[] yCoordinates;
// the current stored coordinate
int currentCoordinate;

void setup() {
// set the window size
size(400, 400);
// black background
background(0);
// white lines
stroke(255);
// initialise the arrays
xCoordinates = new int[4];
yCoordinates = new int[4];
// set the current coordinate to less than 0 to show none
currentCoordinate = -1;
// smooth lines
smooth();
}

void draw() {
// if we have entered all 4 coordinates
if (currentCoordinate == 3) {
// draw a 4-sided shape using the coordinates
quad(xCoordinates[0], yCoordinates[0], xCoordinates[1], yCoordinates[1], xCoordinates[2], yCoordinates[2],
xCoordinates[3], yCoordinates[3]);
}
}

void mouseClicked() {
// if the left mouse button is clicked
if (mouseButton == LEFT) {
// advance the current coordinate by 1
currentCoordinate++;
// store the coordinates
xCoordinates[currentCoordinate] = mouseX;
yCoordinates[currentCoordinate] = mouseY;
// print the clickec coordinates
println(mouseX + ", " + mouseY);
// if the right button is clicked
} else if (mouseButton == RIGHT) {
// clear the screen
background(0);
// reinitialise the shape
currentCoordinate = -1;
}

}


If you run this you will see that that you can click any 4 coordinates (which are displayed in the text area in the Processing window) and a shape will be drawn using the coordinates you clicked. You can right-click the mouse to clear the current shape and start again

Figure 21.7 A 4-sided shape drawn using coordinates

Thursday, 21 January 2010

First 1000 words (draft)

Coordinate Systems and Pixels
A pixel (from the words picture element) is generally thought to be the smallest part of a digital image, be that a picture or text. Although this isn't strictly true (as there is such as thing as a sub-pixel) it is the smallest part over which we have control when creating a digital image. Each pixel is a block of colour that, when combined with many others, make up an image. Digital cameras have brought the word 'pixel' into popular use by describing the quality of the images the camera produces in terms of 'megapixels'. A megapixel is 1 million pixels – the greater the number of pixels that constitute an image, the greater the quality of that image.

Computer displays are often described in terms of their 'resolution' which is measured in the number of pixels horizontally and vertically. For example, a resolution of 1920 x 1200 would contain 2,304,000 pixels. In Processing, the viewable area is also defined in this way using the size() function, which takes 2 arguments: the number of horizontal pixels and the number of vertical pixels (there is also an optional 3rd argument that sets the display mode, but we shall use the 2 argument version) for example


size(600, 400);


If the size() function is not used, Processing uses a default size of 100 x 100 pixels. The displayed screen is like a map of pixels. Each pixel is defined as its position along each axis – the horizontal (x) axis and the vertical (y) axis in the form (x, y), for example the pixel at (100,200) is 100 pixels along the horizontal axis, and 200 pixels along the vertical axis

The displayable area is like a grid, with the number of pixels measured from the top-left point, position (0, 0) and increasing along each axis. As shown in figure 21.1, the coordinates of any single pixel can ascertained by providing the x and y coordinates in relation to the starting point at position (0, 0). As examples, the bottom right of a 600 x 400 display is at position (600, 400) and the middle is at position (300, 200).

Figure 21.1

We can use the coordinates to draw a point by providing them as the arguments to the point() function in the form point(x, y). For example


size(600, 400);
point(300, 200);


will draw a point in the middle of the screen. Any set of coordinates can be used to draw a point. Provided the coordinates are within the range of those of the screen, then the point will be visible. If coordinates are used that are not within that range, for example a negative number or a number greater than the greatest x or y position, the point will not be visible. The coordinates at the limit of the screen's displayable area are available in 2 of Processing's built-in variables; height and width. These are set automatically after the size() function, as shown above, so can be used to find out what the limit of the coordinates are. Therefore the point


point(width, height);


is the point at the bottom-right of the screen.

To help us identify the coordinates of a pixel, we can write a small program that will allow us to click inside the window and have it tell us the coordinates of where we clicked.


void draw() {

}

void mouseClicked() {
// draw the point
point(mouseX, mouseY);
// output the coordinates
System.out.println(mouseX + "," + mouseY);

}


The mouse functions are explained later in the book, so you don't need to understand them now, other than that mouseX and mouseY hold the current coordinates of the mouse pointer. If you click the mouse inside the window, a dot will be printed at the coordinates and the coordinates themselves will be printed in the output text area of the Processing window. You can click as many times as you want and see the coordinates of the point you just created


Figure 21.2 Creating points on the screen


Figure 21.3 The Processing window

As each point is a single pixel, it is also the smallest part of an image for which we can specify the colour. The stroke() function can be used to set the colour of the next pixel drawn by providing a set of arguments that specify the colour. Colour in Processing is explored fully later in the book, so for now all you need to know is that the colour can be set by providing 3 arguments to the stroke() function. We can alter our program above to display different colour points by adding the stroke() function above the point() function call.


void setup() {
// set background to black
background(0);
}

void draw() {

}

void mouseClicked() {
// create a random colour
stroke(random(255), random(255), random(255));
// draw the point
point(mouseX, mouseY);
// output the coordinates
System.out.println(mouseX + "," + mouseY);
}


As you can see, we have added the stroke() function call and, to make it a little more interesting, randomised the colours. Each time the mouse is clicked, a new random colour is generated and the point is drawn in that colour. The only other change is that we have set the background to be black as it makes the colours easier to see

Figure 21.4 Pixels in random colours


Processing offers another way of changing individual pixels. As we explained before, the pixels in the displayable area are like a grid. The grid is the size of the number of pixels that are displayed, so a 600 x 400 screen will have 240,000 pixels. The grid of pixels can be retrieved and altered using the loadPixels() and updatePixels() functions. Using the loadPixels() function means that an array of pixels, called pixels[], can be accessed.
Each element in the pixels[] array contains the colour of the associated pixel, where element pixels[0] is the top-left pixel, and element pixels[pixels.length -1] is the bottom-right pixel (where pixels.length is the size of the pixels[] array – we need to remove 1 because the array starts from 0, so the last element is 1 less that the size).
We can use this to access and update the pixels by altering the colour value of pixels, as we have done below


// set background to black
background(0);
// create the colour magenta
color magenta = color(255, 0, 255);
// load the pixels
loadPixels();
// loop through half of the pixel array
for (int i = 0; i < pixels.length/2; i++) {
// set the pixel to be magenta
pixels[i] = magenta;
}
// update the pixels so they are displayed
updatePixels();


If you run this, you will see that we have changed half of the pixels to a lovely magenta colour, which has the affect of changing half of the screen to that colour, and leaving the rest as the colour of the background. To be able to do this, you need to both load and update the pixels. Attempting to access the pixels[] array without first loading them will result in an error. To display your changes to the pixels[] array, they must then be updated in order to tell Processing to display them – if you miss this out then the changes are simply not displayed.

Tuesday, 19 January 2010

First 500 words (draft)

Coordinate Systems and Pixels
A pixel (from the words picture element) is generally thought to be the smallest part of a digital image, be that a picture or text. Although this isn't strictly true (as there is such as thing as a sub-pixel) it is the smallest part over which we have control when creating a digital image. Each pixel is a block of colour that, when combined with many others, make up an image. Digital cameras have brought the word 'pixel' into popular use by describing the quality of the images the camera produces in terms of 'megapixels'. A megapixel is 1 million pixels – the greater the number of pixels that constitute an image, the greater the quality of that image.

Computer displays are often described in terms of their 'resolution' which is measured in the number of pixels horizontally and vertically. For example, a resolution of 1920 x 1200 would contain 2,304,000 pixels. In Processing, the viewable area is also defined in this way using the size() function, which takes 2 arguments: the number of horizontal pixels and the number of vertical pixels (there is also an optional 3rd argument that sets the display mode, but we shall use the 2 argument version) for example

size(600, 400);

If the size() function is not used, Processing uses a default size of 100 x 100 pixels. The displayed screen is like a map of pixels. Each pixel is defined as its position along each axis – the horizontal (x) axis and the vertical (y) axis in the form (x, y), for example the pixel at (100,200) is 100 pixels along the horizontal axis, and 200 pixels along the vertical axis

Figure 21.1


The displayable area is like a grid, with the number of pixels measured from the top-left point, position (0, 0). As shown in figure 21.1, the coordinates of any single pixel can ascertained by providing the x and y coordinates in relation to the starting point at position (0, 0). As examples, the bottom right of a 600 x 400 display is at position (600, 400) and the middle is at position (300, 200).

We can use the coordinates to draw a point by providing them as the arguments to the point() function in the form point(x, y). For example

size(600, 400);
point(300, 200);

will draw a point in the middle of the screen. Any set of coordinates can be used to draw a point. Provided the coordinates are within the range of those of the screen, then the point will be visible. If coordinates are used that are not within that range, for example a negative number or a number greater than the greatest x or y position, the point will not be visible. The coordinates at the limit of the screen's displayable area are available in 2 of Processing's built-in variables; height and width. These are set automatically after the size() function, as shown above, so can be used to find out what the limit of the coordinates are. Therefore the point

point(width, height);

is the point at the bottom-right of the screen

Tuesday, 8 December 2009

Chunk 21

CHUNK 21
TITLE Coordinate systems and pixels
DESCRIPTION Write an interesting introduction to coordinate systems and pixels. Dont worry about the BufferedImage class
OUTCOMES

* Be able to locate a point using its coordinates.
* Be able to describe what a pixel is.

REFERENCE Greenberg 109--114
HINT This can be deadly dull. Might be worth showing some small program fragments which use coordinates.
PROGRAM Write a program that allows the student to explore the notion of a pixel and its coordinates.