Rotate a two-dimensional image counterclockwise around its center using nearest-neighbor sampling. The output must have the same height and width as the input image.
For an image with H rows and W columns, define the center row coordinate:
cy=2H−1Define the center column coordinate:
cx=2W−1Let a be the supplied angle in degrees. Convert it to radians:
θ=a180πFor each output position with row i and column j, subtract the image center to obtain dy = i - c_y and dx = j - c_x. Use inverse rotation to locate the corresponding source row:
sy=cy+dycos(θ)+dxsin(θ)Compute the corresponding source column:
sx=cx−dysin(θ)+dxcos(θ)Round s_y and s_x to the nearest integers using Python round. If both rounded coordinates are inside the input image, copy that source pixel. Otherwise, place 0 at the output position.
Here, i and j are output coordinates, s_y and s_x are source coordinates, and theta is the counterclockwise rotation angle in radians.
Return the rotated image as a two-dimensional list with the same dimensions as the input image.
Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], angle_degrees = 0
Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Explanation: A zero-degree inverse mapping selects every original pixel.
Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], angle_degrees = 180
Output: [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
Map each output coordinate backward through the inverse rotation.
Round the source coordinates and use zero when they fall outside the image.
Sign in to take notes on this problem
Accepts: array
Accepts: number
Rotate a two-dimensional image counterclockwise around its center using nearest-neighbor sampling. The output must have the same height and width as the input image.
For an image with H rows and W columns, define the center row coordinate:
cy=2H−1Define the center column coordinate:
cx=2W−1Let a be the supplied angle in degrees. Convert it to radians:
θ=a180πFor each output position with row i and column j, subtract the image center to obtain dy = i - c_y and dx = j - c_x. Use inverse rotation to locate the corresponding source row:
sy=cy+dycos(θ)+dxsin(θ)Compute the corresponding source column:
sx=cx−dysin(θ)+dxcos(θ)Round s_y and s_x to the nearest integers using Python round. If both rounded coordinates are inside the input image, copy that source pixel. Otherwise, place 0 at the output position.
Here, i and j are output coordinates, s_y and s_x are source coordinates, and theta is the counterclockwise rotation angle in radians.
Return the rotated image as a two-dimensional list with the same dimensions as the input image.
Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], angle_degrees = 0
Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Explanation: A zero-degree inverse mapping selects every original pixel.
Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], angle_degrees = 180
Output: [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
Map each output coordinate backward through the inverse rotation.
Round the source coordinates and use zero when they fall outside the image.
Sign in to take notes on this problem
Accepts: array
Accepts: number