← Back to Book Detail

20 Neighborhoods (20/11) -- Patterns for Beginning Programmers

Browse
181%

20 Neighborhoods

20 Neighborhoods The subarrays pattern, discussed in Chapter 19, considers some problems in which calculations need to be performed on only some of the elements of an array. The solution in that chapter is appropriate only for problems in which the subarray is defined using an offset and a length. This chapter again considers situations in which calculations need to be performed on only some of the elements, but those elements are now conceptualized as a neighborhood around a particular element. Motivation To blur a discretized audio track or visual image, you must calculate the (weighted) average of the elements that are in the neighborhood of a particular element. For an array (which might, for example, contain a sequence of amplitude measurements of an audio track), such neighborhoods have an odd number of elements and are centered on the element of interest. Such a neighborhood is illustrated in Figure 20.1. For an array of arrays (which might, for example, contain the color values of the pixels in an image), such neighborhoods are square with an odd number of elements, and are centered around the element of interest. Such a neighborhood is illustrated in Figure 20.2. Review If you were to use the subarrays pattern from Chapter 19, you would describe the subset of the elements in Figure 20.1 using an offset of 3 and a length of 3 . Similarly, you would describe the subset of the elements in Figure 20.2 using a roffset (row offset) of 1 , a coffset (column offset) of 1 , a rlength (row length) of 5 , and a clength (column length) of 5 . While there would be nothing technically wrong with this solution, it is not consistent with the conceptual notion of a neighborhood around an element. In other words, it is not consistent with the way domain experts think about the problem. Thinking About The Problem When domain experts think about the blurring problem, they think about calculating the weighted average of the elements that are near a center element. Exactly what this means differs with the domain and the dimensionality of the data. For an array (e.g., a sequence of amplitude measurements from a discretized sound wave), each element is identified by a single index. So, you need one value to represent the center of the neighborhood and one (odd) value to represent the size of the neighborhood. For a rectangular array of arrays (e.g., a raster/grid of color measurements), each element is identified by two indexes, commonly called the row index and column index. So, you need two integer values to represent the center of the neighborhood. Then, if you limit yourself to square neighborhoods (as is common), the size of the neighborhood can be represented by a single (odd) integer. The Pattern As in the subarray pattern of Chapter 19, you need to add formal parameters to the signature of the method you are concerned with. Methods that are passed an array will have two additional parameters (the index and the size ), and methods that are passed an arra
← Previous Chapter Next Chapter →