I need help with a checker board pattern

조회 수: 8 (최근 30일)
Rafael Perales
Rafael Perales 2016년 11월 21일
댓글: Rafael Perales 2016년 11월 21일
I have a code that lets the user input the size of the squares, the dimension of the pattern, and the two colors. The problem I am having is that I can not figure out how to properly have each square alternate between the colors and how to properly have all the squares in the dimension.
This is the code for the plot of the checkerboard pattern that I have so far:
N=4;
S=0;
theta=zeros(1,N);
for k=1:N
theta(k)=2*pi*(k-1)/N;
end
for m=1:D
x1=(R*cos(theta+pi/4)/sqrt(2))+S;
y1=(R*sin(theta+pi/4)/sqrt(2))+S;
plot(x1,y1);
fill(x1,y1,C1);
hold;
x2=(R*cos(theta+pi/4)/sqrt(2))+S;
y2=(R*sin(theta+pi/4)/sqrt(2));
plot(x2,y2);
fill(x2,y2,C2);
hold;
x3=(R*cos(theta+pi/4)/sqrt(2));
y3=(R*sin(theta+pi/4)/sqrt(2))+S;
plot(x3,y3);
fill(x3,y3,C2);
hold;
S=S+R;
end
R is the Size of the square
D is the Dimension Size
C1 and C2 are the colors
How would I go about fixing this code to make sure that I can satisfy all requirements?
  댓글 수: 2
John D'Errico
John D'Errico 2016년 11월 21일
Why are you working in polar coordinates to create a checkerboard?
Rafael Perales
Rafael Perales 2016년 11월 21일
Because one of the requirements is to inscribe the square into a circle than to rotate by a phase to make a pattern out of those squares

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2016년 11월 21일
Why not use the checkerboard() function?
  댓글 수: 5
Image Analyst
Image Analyst 2016년 11월 21일
checkerboard() creates an image. In my mind, and I think most people's minds, when we think of a checkerboard we think of an image, like if you were looking at a chessboard or checkerboard. Not sure what you mean by a plot. Can you show us what a plot might look like?
Rafael Perales
Rafael Perales 2016년 11월 21일
Ok here is basically what I'm trying to do. Basically this is 2x2 version of what I'm trying to do.
The code I used for this is:
N=4;
R=2;
theta=zeros(1,N);
for k=1:N
theta(k)=2*pi*(k-1)/N;
end
x1=(R*cos(theta+pi/4)/sqrt(2))+R;
y1=(R*sin(theta+pi/4)/sqrt(2))+R;
plot(x1,y1);
fill(x1,y1,'y');
x2=(R*cos(theta+pi/4)/sqrt(2))+R;
y2=(R*sin(theta+pi/4)/sqrt(2));
plot(x2,y2);
fill(x2,y2,'b');
x3=(R*cos(theta+pi/4)/sqrt(2));
y3=(R*sin(theta+pi/4)/sqrt(2))+R;
plot(x3,y3);
fill(x3,y3,'b');
x4=(R*cos(theta+pi/4)/sqrt(2));
y4=(R*sin(theta+pi/4)/sqrt(2));
plot(x4,y4);
fill(x4,y4,'y');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by