Live Code, Satellite Example Part B Now We Will Do Something Even More Interesting Have you ever wondered how to determine the Euclidean distance between every point andthe circle's center? In this post, we'll show you how to draw a circle with a radius equal to50% of the total row index and figure out the Euclidean distance between each of the circle'spoints. We'll describe how to build a filter that makes all the points outside of that radius lineand circle true and all the points inside of that circle false. To start, we create two mesh grids, x and y, which will be used to calculate the center pointof the circle. We will also determine the shape of our photo data, an ndarray which is athree-layer image. The number of rows will be assigned to total rows, the number of columnsto total columns, and the three layers RGB to total layers. If we print the shape, we will seethat it still has the original shape of the image. Using the ogrid function, we will vectorize the distance from the center, which will be afunction of the two variables, x and y. The ogrid function returns an ndarray that has therange of total rows and total columns as its arguments. The function then gives us twovectors, x and y, which will have the total number of rows and the total number of columns inthe original image, respectively. Ogrid is a compact method of creating multidimensionalndarray operations in single lines. After that, we get the center point, x and y, by dividing the total number of rows and columnsby 2. To determine the locations that are farther than the radius of the circle we areattempting to generate, we use the two vectors, x and y. Using the x vector and our centerrow, we generate a distance from the center matrix. With our circular mask, we have set truefor each of the circular values that are outside of the circle. That is what we give the circularmask. Only the borders of the circular mask will be visible if we print it. Then, we can filter out allthe points outside of the circle using this mask. All values that are less than or equal to theradius will be set to false, while all values greater than the radius will be set to true. We willnow determine the Euclidean distance between each point and the center. The broadcasting feature of NumPy can be used to determine the Euclidean distance.NumPy can conduct operations on arrays of various shapes thanks to broadcasting. In thisinstance, the center column and row will be subtracted from the x vector and y vector,respectively. The x and y vectors will then be squared and added. The outcome will be adistance matrix that we can use to determine how far each point is from the center using theEuclidean distance formula. We can calculate the Euclidean distance using the following formula: def euclidean_distance (x, y): return np.sqrt(x ** 2 + y ** 2 )
To determine the Euclidean distance between each point inside the circle, we can now applythe circular mask to the distance matrix. To use the circular mask, we can create anotherfunction: def apply_circular_mask(mask, data ): return np.ma.masked_array( data , mask=mask) This function will mask all the points outside of the circle in our distance matrix. We can thenuse the masked distance matrix to get the Euclidean distance of all the points within thecircle.