MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Drawing Rectangular Grid

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20211] Re: Drawing Rectangular Grid
  • From: Tom Burton <tburton at brahea.com>
  • Date: Wed, 6 Oct 1999 21:06:29 -0400
  • Organization: Brahea, Inc.
  • References: <7tcbdt$m72@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 5 Oct 1999 04:04:13 -0400, in comp.soft-sys.math.mathematica you
wrote:

>I would like to have a function that I can give the following
>information to:
>
>Grid[size_of_grid, matrix_of_cells_to_fill]
>
>This function will draw a n x m rectangular grid = size (for example
>size = {2,2} for a 2 x 2 grid).
>
>I then will pass an array of the cells to be filled...

These functions are indeed easy to implement. The following notebook,
with Output removed for brevity, shows one way to make them. Paste the
notebook into Mathematica 3 or Mathematica 4. (Differences between these
two version are relevant to this task.)

Tom Burton

(***********************************************************************

                    Mathematica-Compatible Notebook

This notebook can be used on any computer system with Mathematica 4.0,
MathReader 4.0, or any compatible application. The data for the notebook

starts with the line containing stars above.

To get the notebook into a Mathematica-compatible application, do one of

the following:

* Save the data starting with the line of stars above into a file
  with a name ending in .nb, then open the file inside the application;

* Copy the data starting with the line of stars above to the
  clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode.  Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing the 
word CacheID, otherwise Mathematica-compatible applications may try to 
use invalid cache data.

For more information on notebooks and Mathematica-compatible 
applications, contact Wolfram Research:
  web: http://www.wolfram.com
  email: info at wolfram.com
  phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from 
Wolfram Research.
***********************************************************************)

(*CacheID: 232*)


(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[      9530,        287]*)
(*NotebookOutlinePosition[     10183,        310]*)
(*  CellTagsIndexPosition[     10139,        306]*)
(*WindowFrame->Normal*)



Notebook[{
Cell[TextData[{
  "The function ",
  StyleBox["board", "Input"],
  " prepares a game board of ",
  Cell[BoxData[
      FormBox[
        RowBox[{"m", "\[Times]", "n"}], TraditionalForm]]],
  " units \"squares\". It also sets the aspect ratio of the plot so that
the \
\"squares\" are square."
}], "Text"],

Cell[BoxData[
    RowBox[{
      RowBox[{"board", "[", 
        RowBox[{
          RowBox[{"n_Integer", "?", "Positive"}], ",", 
          RowBox[{"m_Integer", "?", "Positive"}]}], "]"}], ":=", 
      RowBox[{"Graphics", "[", 
        RowBox[{
          RowBox[{"{", "\[IndentingNewLine]", 
            RowBox[{
              RowBox[{
                RowBox[{
                  RowBox[{"Line", "[", 
                    RowBox[{"{", 
                      RowBox[{
                        RowBox[{"{", 
                          RowBox[{"0", ",", "#"}], "}"}], ",", 
                        RowBox[{"{", 
                          RowBox[{"m", ",", "#"}], "}"}]}], "}"}],
"]"}], 
                  "&"}], "/@", 
                RowBox[{"Range", "[", 
                  RowBox[{"0", ",", "n"}], "]"}]}], ",", 
              "\[IndentingNewLine]", 
              RowBox[{
                RowBox[{
                  RowBox[{"Line", "[", 
                    RowBox[{"{", 
                      RowBox[{
                        RowBox[{"{", 
                          RowBox[{"#", ",", "0"}], "}"}], ",", 
                        RowBox[{"{", 
                          RowBox[{"#", ",", "n"}], "}"}]}], "}"}],
"]"}], 
                  "&"}], "/@", 
                RowBox[{"Range", "[", 
                  RowBox[{"0", ",", "m"}], "]"}]}]}],
"\[IndentingNewLine]", 
            "}"}], ",", 
          RowBox[{"AspectRatio", "\[Rule]", 
            FractionBox["n", "m"]}]}], "]"}]}]], "Input"],

Cell[TextData[{
  "The basic idea is to ",
  StyleBox["Map", "Input"],
  " (",
  StyleBox["/@", "Input"],
  ") the function ",
  StyleBox["Line", "Input"],
  " onto all needed positions, first horizontally, then vertically.
There are \
probably more compact and elegant functions, but this one is easy to get
\
right the first time, and isn't that an important aspect of using ",
  StyleBox["Mathematica",
    FontSlant->"Italic"],
  "?"
}], "Text"],

Cell[BoxData[
    RowBox[{
      RowBox[{"Show", "[", 
        RowBox[{"board", "[", 
          RowBox[{"2", ",", "1"}], "]"}], "]"}], ";"}]], "Input"],

Cell[BoxData[
    RowBox[{
      RowBox[{"Show", "[", 
        RowBox[{"board", "[", 
          RowBox[{"5", ",", "10"}], "]"}], "]"}], ";"}]], "Input"],

Cell[TextData[{
  "The function ",
  StyleBox["board", "Input"],
  " will not accept 0 as a board dimension:"
}], "Text"],

Cell[BoxData[
    RowBox[{
      RowBox[{"Show", "[", 
        RowBox[{"board", "[", 
          RowBox[{"5", ",", "0"}], "]"}], "]"}], ";"}]], "Input"],

Cell[TextData[{
  "The function ",
  StyleBox["pieces", "Input"],
  " puts game pieces (filled circles) at the specified locations. Color
is \
optional; black is the default."
}], "Text"],

Cell[BoxData[
    RowBox[{
      RowBox[{"pieces", "[", 
        RowBox[{
          RowBox[{"p", ":", 
            RowBox[{"{", 
              RowBox[{
                RowBox[{"{", 
                  RowBox[{
                    RowBox[{"_Integer", "?", "Positive"}], ",", 
                    RowBox[{"_Integer", "?", "Positive"}]}], "}"}],
".."}], 
              "}"}]}], ",", 
          RowBox[{"color_:", 
            RowBox[{"GrayLevel", "[", "0", "]"}]}]}], "]"}], ":=", 
      RowBox[{"Graphics", "[", 
        RowBox[{"{", 
          RowBox[{"color", ",", 
            RowBox[{
              RowBox[{
                RowBox[{"Disk", "[", 
                  RowBox[{
                    RowBox[{"#", "-", 
                      RowBox[{"{", 
                        RowBox[{
                          FractionBox["1", "2"], ",", 
                          FractionBox["1", "2"]}], "}"}]}], ",", ".3"}],

                  "]"}], "&"}], "/@", "p"}]}], "}"}], "]"}]}]],
"Input"],

Cell["\<\
Because this function does not know about the entire grid, it cannot set
the \
proper aspect ratio.\
\>", "Text"],

Cell[BoxData[
    RowBox[{
      RowBox[{"Show", "[", 
        RowBox[{"pieces", "[", 
          RowBox[{
            RowBox[{"{", 
              RowBox[{
                RowBox[{"{", 
                  RowBox[{"1", ",", "1"}], "}"}], ",", 
                RowBox[{"{", 
                  RowBox[{"2", ",", "2"}], "}"}]}], "}"}], ",", 
            RowBox[{"GrayLevel", "[", ".7", "]"}]}], "]"}], "]"}], 
      ";"}]], "Input"],

Cell[TextData[{
  "When you show the game board ",
  StyleBox["first",
    FontSlant->"Italic"],
  ", the proper aspect ratio is established:"
}], "Text"],

Cell[BoxData[
    RowBox[{
      RowBox[{"Show", "[", 
        RowBox[{
          RowBox[{"board", "[", 
            RowBox[{"5", ",", "10"}], "]"}], ",", 
          RowBox[{"pieces", "[", 
            RowBox[{"{", 
              RowBox[{
                RowBox[{"{", 
                  RowBox[{"1", ",", "1"}], "}"}], ",", 
                RowBox[{"{", 
                  RowBox[{"2", ",", "2"}], "}"}], ",", 
                RowBox[{"{", 
                  RowBox[{"3", ",", "2"}], "}"}], ",", 
                RowBox[{"{", 
                  RowBox[{"4", ",", "3"}], "}"}], ",", 
                RowBox[{"{", 
                  RowBox[{"5", ",", "4"}], "}"}], ",", 
                RowBox[{"{", 
                  RowBox[{"6", ",", "4"}], "}"}]}], "}"}], "]"}]}],
"]"}], 
      ";"}]], "Input"],

Cell[TextData[{
  StyleBox["pieces", "Input"],
  " does not know the board size, so you can go outside by mistake. The
\
aspect ratio is still maintained, and it should be easy to \"spot\" and
\
correct this mistake."
}], "Text"],

Cell[BoxData[
    RowBox[{
      RowBox[{"Show", "[", 
        RowBox[{
          RowBox[{"board", "[", 
            RowBox[{"5", ",", "10"}], "]"}], ",", 
          RowBox[{"pieces", "[", 
            RowBox[{
              RowBox[{"Table", "[", 
                RowBox[{
                  RowBox[{"{", 
                    RowBox[{"i", ",", "i"}], "}"}], ",", 
                  RowBox[{"{", 
                    RowBox[{"i", ",", "6"}], "}"}]}], "]"}], ",", 
              RowBox[{"GrayLevel", "[", ".7", "]"}]}], "]"}]}], "]"}], 
      ";"}]], "Input"],

Cell["\<\
Here are two sets of circles of different colors. You may not need \
multicolored plots right away, but it's nice to know how easy they are
to do.\
\
\>", "Text"],

Cell[BoxData[
    RowBox[{
      RowBox[{"Show", "[", 
        RowBox[{
          RowBox[{"board", "[", 
            RowBox[{"5", ",", "10"}], "]"}], ",", "\[IndentingNewLine]",

          RowBox[{"pieces", "[", 
            RowBox[{
              RowBox[{"{", 
                RowBox[{
                  RowBox[{"{", 
                    RowBox[{"1", ",", "1"}], "}"}], ",", 
                  RowBox[{"{", 
                    RowBox[{"3", ",", "2"}], "}"}], ",", 
                  RowBox[{"{", 
                    RowBox[{"4", ",", "3"}], "}"}], ",", 
                  RowBox[{"{", 
                    RowBox[{"6", ",", "4"}], "}"}]}], "}"}], ",", 
              RowBox[{"Hue", "[", "1", "]"}]}], "]"}], ",", 
          "\[IndentingNewLine]", 
          RowBox[{"pieces", "[", 
            RowBox[{
              RowBox[{"{", 
                RowBox[{
                  RowBox[{"{", 
                    RowBox[{"2", ",", "1"}], "}"}], ",", 
                  RowBox[{"{", 
                    RowBox[{"4", ",", "2"}], "}"}], ",", 
                  RowBox[{"{", 
                    RowBox[{"8", ",", "3"}], "}"}], ",", 
                  RowBox[{"{", 
                    RowBox[{"6", ",", "5"}], "}"}]}], "}"}], ",", 
              RowBox[{"Hue", "[", ".6", "]"}]}], "]"}]}], "]"}], 
      ";"}]], "Input"]
},
FrontEndVersion->"4.0 for Microsoft Windows",
ScreenRectangle->{{0, 1152}, {0, 814}},
WindowSize->{596, 656},
WindowMargins->{{0, Automatic}, {Automatic, 0}}
]


(***********************************************************************
Cached data follows.  If you edit this Notebook file directly, not using
Mathematica, you must remove the line containing CacheID at the top of 
the file.  The cache data will then be recreated when you save this file

from within Mathematica.
***********************************************************************)

(*CellTagsOutline
CellTagsIndex->{}
*)

(*CellTagsIndex
CellTagsIndex->{}
*)

(*NotebookFileOutline
Notebook[{
Cell[1717, 49, 303, 9, 60, "Text"],
Cell[2023, 60, 1491, 37, 156, "Input"],
Cell[3517, 99, 449, 13, 81, "Text"],
Cell[3969, 114, 151, 4, 35, "Input"],
Cell[4123, 120, 152, 4, 35, "Input"],
Cell[4278, 126, 121, 4, 39, "Text"],
Cell[4402, 132, 151, 4, 35, "Input"],
Cell[4556, 138, 187, 5, 60, "Text"],
Cell[4746, 145, 983, 26, 92, "Input"],
Cell[5732, 173, 123, 3, 59, "Text"],
Cell[5858, 178, 426, 12, 35, "Input"],
Cell[6287, 192, 154, 5, 38, "Text"],
Cell[6444, 199, 797, 21, 77, "Input"],
Cell[7244, 222, 229, 5, 60, "Text"],
Cell[7476, 229, 556, 15, 56, "Input"],
Cell[8035, 246, 172, 4, 59, "Text"],
Cell[8210, 252, 1316, 33, 77, "Input"]
}
]
*)




(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)




Tom Burton


  • Prev by Date: Re: Rebuilding polygon from CoefficientList?
  • Next by Date: MathLink performance
  • Previous by thread: RE: Drawing Rectangular Grid
  • Next by thread: Re: Drawing Rectangular Grid