1
$\begingroup$

I want to color one of the diagonal elements of a matrix that consists of 45° rotated and zigzag arranged squares. The addressing condition is following j=2*i and j=2*i-1, where j and i are the vertical and horizontal order numbers.

sideLength = 1;
rotatedSqare[x0_, y0_] := Polygon[{{x0, y0}, {x0 + sideLength*0.7, 
y0 - sideLength*0.7}, {x0 + sideLength*1.4, 
y0}, {x0 + sideLength*0.7, y0 + sideLength*0.7}}]

Applying the conditions individually If [j == 2*i , Blue, Green] and If [j == 2*i - 1 , Blue, Green] for coloring works properly, as shown below:

{Graphics[{EdgeForm[Thickness[0.0013]], LightBlue, Table[{If [j == 2*i -    1 , Blue, Green], rotatedSqare[
  x0 + (period)*(i - 1) + Boole[OddQ[j + 1]]*(period)/2, 
  y0 + (period/2) j]}, {i, 1, 11}, {j, 1, 22}]}, PlotRange -> {{0, 20}, {0, 15}}, ImageSize -> 300], Graphics[{EdgeForm[Thickness[0.0013]], LightBlue, Table[{If [j == 2*i , Blue, Green], rotatedSqare[
  x0 + (period)*(i - 1) + Boole[OddQ[j + 1]]*(period)/2, 
  y0 + (period/2) j]}, {i, 1, 11}, {j, 1, 22}]}, PlotRange -> {{0, 20}, {0, 15}}, ImageSize -> 300]}

enter image description here enter image description here

When I combine two conditions together “ If [(j == 2*i And j == 2*i-1), Blue, Green]”, then I get this message:

“If is not a Graphics primitive or directive.”

Can someone help me to get around this? Thanks!!!

$\endgroup$
8
  • 1
    $\begingroup$ If you entered the same code as you gave in text, then you have probably a syntax error. You can replace that And with && or use And with infix notation as If[(j == 2*i)~And~(j == 2*i - 1), Blue, Green]. $\endgroup$
    – Pinti
    May 14, 2017 at 18:15
  • $\begingroup$ You have also forgot to include the definitions for symbols x0, y0 and period in your code. Please edit and update your question accordingly. $\endgroup$
    – Pinti
    May 14, 2017 at 18:17
  • $\begingroup$ Thanks a lot Pint for repspondingi!!! $\endgroup$
    – surfAliq
    May 15, 2017 at 5:04
  • $\begingroup$ @Pinti. Thanks a lot Pint for repspondingi!!! Yes I forgot the parameters, here they are x0 = 0; y0 = 0; sideLength = 1; period = 1.5*sideLength. $\endgroup$
    – surfAliq
    May 15, 2017 at 5:11
  • $\begingroup$ @Pinti. I have tried both If [(j == 2*i - 1) && (j == 2*i), Blue, Green] and If [(j == 2*i - 1)~And~ (j == 2*i), Blue, Green]. It does not give any error message, but does not do apply the conditions. $\endgroup$
    – surfAliq
    May 15, 2017 at 5:30

1 Answer 1

1
$\begingroup$

here is the result:

x0 = 0; y0 = 0;
sideLength = 1;
period = 1.5*sideLength;

rotatedSqare[x0_, y0_] := Polygon[{{x0, y0}, {x0 + sideLength*0.7, y0 - sideLength*0.7}, {x0 + sideLength*1.4, y0}, {x0 + sideLength*0.7, y0 + sideLength*0.7}}]

Graphics[{EdgeForm[Thickness[0.0013]], LightBlue, Table[{If [(j == 2*i - 1) || (j == 2*i), Blue, Green], rotatedSqare[
 x0 + (period)*(i - 1) + Boole[OddQ[j + 1]]*(period)/2, 
 y0 + (period/2) j]}, {i, 1, 20}, {j, 1, 22}]}, PlotRange -> {{0, 20}, {0, 15}}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.