How to make 4 different colour on four different quadrant?

Hi, i have a question regarding the line colour. It was not equally or orderly distributed. It supposed to be a blue color on -45 degree to 45 degree, a red color starting from 45 degree to 135 degree, a black color from 135 degree to 225 degree. Last, the green color from 225 degree to 315 degree. Below are my coding.
for x = 1:9
for y = 1:9
Q=atan(Mnew(x,y));
if ((Q>=(-pi/4))&&(Q<(pi/4)))
plot([X(x,y) Xnew(x,y)],[Y(x,y) Ynew(x,y)],'b','LineWidth',3);
elseif ((Q>=(pi/4))&&(Q<(3*pi/4)))
plot([X(x,y) Xnew(x,y)],[Y(x,y) Ynew(x,y)],'r','LineWidth',3);
elseif ((Q>=(3*pi/4))&&(Q<(5*pi/4)))
plot([X(x,y) Xnew(x,y)],[Y(x,y) Ynew(x,y)],'k','LineWidth',3);
else
plot([X(x,y) Xnew(x,y)],[Y(x,y) Ynew(x,y)],'g','LineWidth',3);
end
hold on;
end
end
Mind to tell you that this is only half of my coding, and i hope anyone can check whether my coding for color distribution are correct or not.

Réponses (2)

Geoff Hayes
Geoff Hayes le 9 Nov 2015
Nik - the documentation for atan indicates that calling this function returns values in the interval [-pi/2,pi/2]. You should probably use atan2 instead as it will return values in the interval of [-pi,pi]. With that in mind, you will need to adjust your checks for whether Q is less than 225 degrees and whether Q is less than 315 degrees (you will need to use the negative equivalents of -135 and -45 respectively).

2 commentaires

Thank you for your help Geoff. However i still cannot run my matlab project. It said that there are not enough input arguments. Here below are my coding, perhaps you can help me to figure out something.
function FocusPoint()
[x,y] = meshgrid(1:9,1:9);
m = zeros(9,9);
plotbargraph(m,x,y);
for k = 1:1
[x1,y1,button] = ginput(1);
while button ~=3
m1 = (y-y1)./(x-x1);
plotbargraph(m1,x,y);
[x1,y1,button] = ginput(1);
end
end
end
function plotbargraph(M,X,Y)
C = Y-M.*X;
r = 0.8;
a = 1+M.^2;
b = 2*M.*C-2*X-2*M.*Y;
c = X.^2+C.^2-2*C.*Y+Y.^2-r^2;
Xnew = (-b+sqrt(b.^2-4*a.*c))./(2*a);
Ynew = M.*Xnew+C;
Mnew = (Y-Ynew)./(X-Xnew);
for x = 1:9
for y = 1:9
Q=atan(Mnew(x,y));
if ((Q>=(-pi/4))&&(Q<(pi/4)))
plot([X(x,y) Xnew(x,y)],[Y(x,y) Ynew(x,y)],'b','LineWidth',3);
elseif ((Q>=(pi/4))&&(Q<(3*pi/4)))
plot([X(x,y) Xnew(x,y)],[Y(x,y) Ynew(x,y)],'r','LineWidth',3);
elseif ((Q>=(3*pi/4))&&(Q<(5*pi/4)))
plot([X(x,y) Xnew(x,y)],[Y(x,y) Ynew(x,y)],'k','LineWidth',3);
else
plot([X(x,y) Xnew(x,y)],[Y(x,y) Ynew(x,y)],'g','LineWidth',3);
end
hold on;
end
end
hold off;
end
Nik - I don't observe any errors when I run the above code, so perhaps you have copied and pasted something different? As well, you need to use atan2 which requires two inputs (perhaps that is where the error is being generated?).

Connectez-vous pour commenter.

Thorsten
Thorsten le 11 Nov 2015
Use atan2 to get the four-quadrant inverse tangent.

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by