Page 80 - Computational Colour Science Using MATLAB
P. 80
IMPLEMENTATIONS AND EXAMPLES 67
elseif ((ab(1) < 0) & (ab(2) > 0))
h = 180 - h; % second quadrant
elseif ((ab(1) < 0) & (ab(2) < 0))
h = 180 + h; % third quadrant
else
h = 360 - h; % fourth quadrant
end
end
The syntax for the function call is
[c,h] = car2pol(ab)
where ab is a 261or162 matrix and c and h are both 161 matrices whose
entries are the polar coordinates. Note that the code in car2pol could be
shortened considerably by the use of the atan2 command. Whereas the atan
MATLAB function returns the arctangent of the input element and requires that
the quadrant be determined from the polarities of the cartesian coordinates, the
atan2 command returns the four-quadrant arctangent directly.
The function pol2car is provided to return polar coordinates to cartesian
coordinates. The format for the function call is
[a,b] = pol2car(ch)
where ch is a 261or162 matrix containing the distance and angular terms and
a and b are both 161 matrices whose entries are the horizontal and vertical
components of the cartesian space.
Box 9: pol2car.m
function [a,b] = pol2car(ch)
% function [a,b] = pol2car(ch)
% converts the polar coordinates
% of Chroma C and Hue H
% ch must be a row or column matrix 2 by 1 or 1 by 2
% see also car2pol
dim = size(ch);
if (dim(1) == 1) | (dim(2) == 1)
ch = ch(:)’; % force to be a row matrix