{"id":2597,"date":"2017-06-06T11:00:19","date_gmt":"2017-06-06T15:00:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=2597"},"modified":"2019-11-01T17:11:16","modified_gmt":"2019-11-01T21:11:16","slug":"chessboards-implicit-expansion-repelem-and-unicode-chess-queens","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2017\/06\/06\/chessboards-implicit-expansion-repelem-and-unicode-chess-queens\/","title":{"rendered":"Chessboards, Implicit Expansion, REPELEM, and Unicode Chess Queens"},"content":{"rendered":"<div class=\"content\"><p><a href=\"https:\/\/blogs.mathworks.com\/steve\/2017\/04\/20\/the-eight-queens-problem\/\">A few weeks ago<\/a>, I wrote about a solver algorithm for the Eight Queens Problem. The post included diagrams like this one.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/sample-solution-8.png\" alt=\"\"> <\/p><p>Today I want to show you how I made that diagram in MATLAB. First, let's talk about the board. Back in 2011, I wrote about a variety of ways to make a checkerboard (or chessboard) pattern. In that post, I played games with <tt>repmat<\/tt>, powers of -1, <tt>floor<\/tt>, and <tt>round<\/tt>. It got a little crazy.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_06.png\" alt=\"\"> <\/p><p>I'm still fond of using integer powers of -1, like this.<\/p><p>$(-1)^{(n_1 + n_2)}$<\/p><p>In 2011, I used <tt>ndgrid<\/tt> to make a matrix of integer powers, $n_1 + n_2$.<\/p><pre class=\"codeinput\">[n1,n2] = ndgrid(0:4);\r\n(-1).^(n1 + n2)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n     1    -1     1    -1     1\r\n    -1     1    -1     1    -1\r\n     1    -1     1    -1     1\r\n    -1     1    -1     1    -1\r\n     1    -1     1    -1     1\r\n\r\n<\/pre><p>Since the introduction of implicit expansion in R2016b, though, I no longer need to use <tt>ndgrid<\/tt> to explicitly form the matrix of powers.<\/p><pre class=\"codeinput\">n = 0:4;\r\n(-1).^(n + n')\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n     1    -1     1    -1     1\r\n    -1     1    -1     1    -1\r\n     1    -1     1    -1     1\r\n    -1     1    -1     1    -1\r\n     1    -1     1    -1     1\r\n\r\n<\/pre><p>In my eight queens code, here's how I got the shades of gray for a chessboard. (There are lots of ways to do this.)<\/p><pre class=\"codeinput\">N = 8;\r\n\r\ndark_square_color = .7;\r\nlight_square_color = .9;\r\n\r\n\r\ncolor_range = light_square_color - dark_square_color;\r\nf = (-1).^((1:N)' + (1:N));\r\nf = (f + 1) * color_range \/ 2;\r\nf = f + dark_square_color;\r\nf(1:4,1:4)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n    0.9000    0.7000    0.9000    0.7000\r\n    0.7000    0.9000    0.7000    0.9000\r\n    0.9000    0.7000    0.9000    0.7000\r\n    0.7000    0.9000    0.7000    0.9000\r\n\r\n<\/pre><p>Next, I needed to replicate each element of <tt>f<\/tt> to make an image with larger squares. Do you know how to replicate elements of a matrix? Some experienced MATLAB users would do it using the <tt>kron<\/tt> function. Now, however, you can just use the new <tt>repelem<\/tt> function. The <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/repelem.html\">reference page<\/a> tells you when this function was introduced.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/repelem-introduced-in.png\" alt=\"\"> <\/p><p>I'm going to make my squares 60 pixels wide, and then I'll turn it into a truecolor image so that the pixel colors are independent of the figure's colormap.<\/p><pre class=\"codeinput\">pixel_size = 60;\r\nf = repelem(f,pixel_size,pixel_size);\r\nf = repmat(f,1,1,3);\r\nh = imshow(f,<span class=\"string\">'InitialMagnification'<\/span>,100);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_01.png\" alt=\"\"> <p>Now I want to show you a coordinate system trick. If you turn on the axes display, you can see the image pixel coordinates. (In Image Processing Toolbox terminology, these are called <i>intrinsic coordinates<\/i>.)<\/p><pre class=\"codeinput\">axis <span class=\"string\">on<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_02.png\" alt=\"\"> <p>When I get to the part about displaying the queens, however, I would like to be able to place them based on the coordinates of each chessboard square, independent of the number of pixels per square. I can do that by manipulating the <tt>XData<\/tt> and <tt>YData<\/tt> properties of the image. These properties assign spatial coordinates to the left\/right and top\/bottom edges of the image.<\/p><pre class=\"codeinput\">h.XData = [0.5 N+0.5];\r\nh.YData = [0.5 N+0.5];\r\naxis([0.5 N+0.5 0.5 N+0.5])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_03.png\" alt=\"\"> <p>Now let's put some queens on the board. This turns out to be pretty easy because you can just do it with a <tt>text<\/tt> object. Since R2014b, you've been able to draw text in MATLAB graphics using Unicode characters. And the Unicode character set includes chess symbols! The black queen symbol is at code point 9819 (decimal).<\/p><pre class=\"codeinput\">queen = char(9819)\r\n<\/pre><pre class=\"codeoutput\">\r\nqueen =\r\n\r\n    '&#9819;'\r\n\r\n<\/pre><p>Let's put a queen on the square in the second row, third column. (That's $x=3$, $y=2$.)<\/p><pre class=\"codeinput\">hq = text(3,2,queen);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_04.png\" alt=\"\"> <p>Oops, our queen is pretty small, and she also appears to be off-center. Let's fix that.<\/p><pre class=\"codeinput\">hq.FontSize = 40;\r\nhq.HorizontalAlignment = <span class=\"string\">'center'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_05.png\" alt=\"\"> <p>That's better.<\/p><p>I have one more little coding trick to show you. When I wrote my eight queens animation code, I wanted to have an easy to show and remove a queen at any square on the board. So, I made an matrix of text objects, one at each square. Then I could just index into the matrix of text objects and turn the <tt>Visible<\/tt> property on and off. Here's how. (First, let me delete the text object I just made.)<\/p><pre class=\"codeinput\">delete(hq)\r\n<span class=\"keyword\">for<\/span> r = 1:N\r\n    <span class=\"keyword\">for<\/span> c = 1:N\r\n        queens(r,c) = text(c,r,char(9819),<span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'HorizontalAlignment'<\/span>,<span class=\"string\">'center'<\/span>,<span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'Visible'<\/span>,<span class=\"string\">'off'<\/span>,<span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'FontSize'<\/span>,40);\r\n    <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_06.png\" alt=\"\"> <p>OK, let's turn on the queen displays on a couple of squares.<\/p><pre class=\"codeinput\">queens(5,3).Visible = <span class=\"string\">'on'<\/span>;\r\nqueens(2,4).Visible = <span class=\"string\">'on'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_07.png\" alt=\"\"> <p>For a final bit of fun, the function <tt>set<\/tt> lets you modify properties of a whole array of graphics objects at once.<\/p><p>Turn them all on!<\/p><pre class=\"codeinput\">set(queens,<span class=\"string\">'Visible'<\/span>,<span class=\"string\">'on'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_08.png\" alt=\"\"> <p>(The board above, by the way, is NOT a solution to the Eight Queens Problem. In case you were wondering.)<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_c6dd1186e5b945f198e1906ee07031fd() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='c6dd1186e5b945f198e1906ee07031fd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' c6dd1186e5b945f198e1906ee07031fd';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2017 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_c6dd1186e5b945f198e1906ee07031fd()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><\/div><!--\r\nc6dd1186e5b945f198e1906ee07031fd ##### SOURCE BEGIN #####\r\n%% \r\n% <https:\/\/blogs.mathworks.com\/steve\/2017\/04\/20\/the-eight-queens-problem\/\r\n% A few weeks ago>, I wrote about a solver algorithm for the Eight\r\n% Queens Problem. The post included diagrams like this one.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/sample-solution-8.png>>\r\n%\r\n% Today I want to show you how I made that diagram in MATLAB. First,\r\n% let's talk about the board. Back in 2011, I wrote about a variety\r\n% of ways to make a checkerboard (or chessboard) pattern. In that\r\n% post, I played games with |repmat|, powers of -1, |floor|, and\r\n% |round|. It got a little crazy.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_06.png>>\r\n%\r\n% I'm still fond of using integer powers of -1, like this.\r\n%\r\n% $(-1)^{(n_1 + n_2)}$\r\n%\r\n% In 2011, I used |ndgrid| to make a matrix of integer powers, $n_1\r\n% + n_2$.\r\n\r\n[n1,n2] = ndgrid(0:4);\r\n(-1).^(n1 + n2)\r\n\r\n%%\r\n% Since the introduction of implicit expansion in R2016b, though, I\r\n% no longer need to use |ndgrid| to explicitly form the matrix of\r\n% powers.\r\n\r\nn = 0:4;\r\n(-1).^(n + n')\r\n\r\n%%\r\n% In my eight queens code, here's how I got the shades of gray for\r\n% a chessboard. (There are lots of ways to do this.)\r\n\r\nN = 8;\r\n\r\ndark_square_color = .7;\r\nlight_square_color = .9;\r\n\r\n\r\ncolor_range = light_square_color - dark_square_color;\r\nf = (-1).^((1:N)' + (1:N));\r\nf = (f + 1) * color_range \/ 2;\r\nf = f + dark_square_color;\r\nf(1:4,1:4)\r\n\r\n%%\r\n% Next, I needed to replicate each element of |f| to make an image\r\n% with larger squares. Do you know how to replicate elements of a\r\n% matrix? Some experienced MATLAB users would do it using the |kron|\r\n% function. Now, however, you can just use the new |repelem|\r\n% function. The\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/repelem.html reference\r\n% page> tells you when this function was introduced.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/repelem-introduced-in.png>>\r\n%\r\n% I'm going to make my squares 60 pixels wide, and then I'll turn it\r\n% into a truecolor image so that the pixel colors are independent of\r\n% the figure's colormap.\r\n\r\npixel_size = 60;\r\nf = repelem(f,pixel_size,pixel_size);\r\nf = repmat(f,1,1,3);\r\nh = imshow(f,'InitialMagnification',100);\r\n\r\n%%\r\n% Now I want to show you a coordinate system trick. If you turn on\r\n% the axes display, you can see the image pixel coordinates. (In\r\n% Image Processing Toolbox terminology, these are called _intrinsic\r\n% coordinates_.)\r\n\r\naxis on\r\n\r\n%%\r\n% When I get to the part about displaying the queens, however, I\r\n% would like to be able to place them based on the coordinates of\r\n% each chessboard square, independent of the number of pixels per\r\n% square. I can do that by manipulating the |XData| and |YData|\r\n% properties of the image. These properties assign spatial\r\n% coordinates to the left\/right and top\/bottom edges of the image.\r\n\r\nh.XData = [0.5 N+0.5];\r\nh.YData = [0.5 N+0.5];\r\naxis([0.5 N+0.5 0.5 N+0.5])\r\n\r\n%%\r\n% Now let's put some queens on the board. This turns out to be\r\n% pretty easy because you can just do it with a |text| object. Since\r\n% R2014b, you've been able to draw text in MATLAB graphics using\r\n% Unicode characters. And the Unicode character set includes chess\r\n% symbols! The black queen symbol is at code point 9819 (decimal).\r\n\r\nqueen = char(9819)\r\n\r\n%%\r\n% Let's put a queen on the square in the second row, third column.\r\n% (That's $x=3$, $y=2$.)\r\n\r\nhq = text(3,2,queen);\r\n\r\n%%\r\n% Oops, our queen is pretty small, and she also appears to be\r\n% off-center. Let's fix that.\r\n\r\nhq.FontSize = 40;\r\nhq.HorizontalAlignment = 'center';\r\n\r\n%%\r\n% That's better.\r\n%\r\n% I have one more little coding trick to show you. When I wrote my\r\n% eight queens animation code, I wanted to have an easy to show and\r\n% remove a queen at any square on the board. So, I made an matrix of\r\n% text objects, one at each square. Then I could just index into the\r\n% matrix of text objects and turn the |Visible| property on and off.\r\n% Here's how. (First, let me delete the text object I just made.)\r\n\r\ndelete(hq)\r\nfor r = 1:N\r\n    for c = 1:N\r\n        queens(r,c) = text(c,r,char(9819),...\r\n            'HorizontalAlignment','center',...\r\n            'Visible','off',...\r\n            'FontSize',40);\r\n    end\r\nend\r\n\r\n%%\r\n% OK, let's turn on the queen displays on a couple of squares.\r\n\r\nqueens(5,3).Visible = 'on';\r\nqueens(2,4).Visible = 'on';\r\n\r\n%%\r\n% For a final bit of fun, the function |set| lets you modify\r\n% properties of a whole array of graphics objects at once.\r\n%\r\n% Turn them all on!\r\n\r\nset(queens,'Visible','on')\r\n\r\n%%\r\n% (The board above, by the way, is NOT a solution to the Eight Queens\r\n% Problem.)\r\n##### SOURCE END ##### c6dd1186e5b945f198e1906ee07031fd\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/drawing_queens_chessboards_08.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>A few weeks ago, I wrote about a solver algorithm for the Eight Queens Problem. The post included diagrams like this one. Today I want to show you how I made that diagram in MATLAB. First, let's talk... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2017\/06\/06\/chessboards-implicit-expansion-repelem-and-unicode-chess-queens\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":2606,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[50,1193,733,252,36,915,206,1107,116,188,78],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2597"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=2597"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2597\/revisions"}],"predecessor-version":[{"id":2611,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2597\/revisions\/2611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/2606"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=2597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=2597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=2597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}