tutorial-Pictures.xhtml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Composable pictures</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="tutorial-Variables.xhtml" title="Variables"/>
  10. <link rel="next" href="tutorial-Sequences.xhtml" title="Lists and sequences"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Composable pictures" epub:type="subchapter" id="Tutorial---Pictures">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Composable pictures</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>The <code class="literal">pictures</code> library lets you create geometric shapes
  23. and images, and combine them in interesting ways.
  24. You first need to import the library:
  25. </p>
  26. <pre class="screen">(import (kawa pictures))
  27. </pre>
  28. <p>The easiest way to use and learn the library
  29. is with a suitable REPL, where you can type
  30. expressions that evaluate to pictures values,
  31. and view the resulting pictures directly on the console.
  32. The easiest way is to start the <code class="literal">kawa</code> command with the <code class="literal">-w</code>
  33. flag. Alternatively, you can use
  34. a <a class="link" href="REPL-Console.xhtml#Using-DomTerm">DomTerm</a>-based terminal emulator
  35. such as <code class="literal">qtdomterm</code> (which is shown in the image below),
  36. and then the <code class="literal">kawa</code> command.
  37. </p>
  38. <div class="informalfigure">
  39. <div class="mediaobject">
  40. <img src="images/domterm-pictures-1.png"/>
  41. </div>
  42. </div>
  43. <p>The above image shows two simple examples: a filled
  44. circle (radius 30 pixels, color magenta), and a non-filled rotated rectangle
  45. (color maroon 3-pixel wide strokes).
  46. </p>
  47. <p>See <a class="link" href="Composable-pictures.xhtml" title="Composable pictures">Composable pictures</a> for details and more examples.
  48. </p>
  49. <h3 id="idm139667880509200">Shapes and coordinates</h3>
  50. <p>A <em class="firstterm">shape</em> is a geometrical figure consisting of
  51. one or more curves and lines. One kind of shape is a circle;
  52. you can create one with the <code class="literal">circle</code> procedure,
  53. specifying the radius in “pixels”.
  54. </p>
  55. <pre class="screen"><span class="prompt">#|kawa:1|# </span><strong class="userinput"><code>(import (kawa pictures))</code></strong>
  56. <span class="prompt">#|kawa:2|# </span><strong class="userinput"><code>(circle 30)</code></strong>
  57. <img src="images/fill-circ-1.png"/>
  58. </pre>
  59. <p>It you print a shape, it will show it as a thin black curve.
  60. </p>
  61. <p>A <em class="firstterm">point</em> has two real-numbered parts: the point’s x-coordinate,
  62. and its y-coordinate.
  63. The x-coordinate increases as you move right along the page/screen,
  64. while the y-coordinate increases as you move <span class="emphasis"><em>down</em></span>.
  65. (Unlike traditional mathematics, where
  66. the y-coordinate increases as you go up.)
  67. The unit distance is one “pixel”, which is defined as CSS or HTML.
  68. You can create a point with <code class="literal">&amp;P</code> operator.
  69. For example:
  70. </p>
  71. <pre class="screen">&amp;P[30 20]
  72. </pre>
  73. <p>is a point 30 pixels right and 20 pixels down from the origin point.
  74. To create a circle centered on that point do <code class="literal">(center 30 &amp;P[30 20])</code>.
  75. </p>
  76. <p>The expression <code class="literal">(rectangle &amp;P[10 20] &amp;P[50 40])</code> creates
  77. a rectangle whose upper left corner is (10,20) and whose
  78. lower right corner is (50,40).
  79. </p>
  80. <p>A <em class="firstterm">dimension</em> is a pair, a width and height,
  81. and is written:
  82. </p>
  83. <pre class="screen">&amp;D[<em class="replaceable"><code>width</code></em> <em class="replaceable"><code>height</code></em>]
  84. </pre>
  85. <p>In addition to being used for sizes,
  86. a dimension is also used for relative offsets.
  87. For example, the previous rectangle could also
  88. be written <code class="literal">(rectangle &amp;P[10 20] &amp;D[40 20])</code>.
  89. </p>
  90. <p>You can use <code class="literal">line</code> to create a line.
  91. More generally, if you specify <em class="replaceable"><code>n</code></em> points you get a
  92. <em class="firstterm">polyline</em> of <em class="replaceable"><code>n-1</code></em> line segments:
  93. </p>
  94. <pre class="screen"><span class="prompt">#|kawa:3|# </span><strong class="userinput"><code>(line &amp;P[10 20] &amp;P[50 60] &amp;P[90 0])</code></strong>
  95. <img src="images/polyline-1.png"/>
  96. </pre>
  97. <p>The same line using dimensions for relative offsets:
  98. </p>
  99. <pre class="screen"><span class="prompt">#|kawa:4|# </span><strong class="userinput"><code>(line &amp;P[10 20] &amp;D[40 20] &amp;D[40 -60])</code></strong>
  100. </pre>
  101. <p>A <em class="firstterm">closed shape</em> is one whose end point is the same as its start point.
  102. The <code class="literal">polygon</code> function creates one using straight line segments
  103. </p>
  104. <pre class="screen"><span class="prompt">#|kawa:5|# </span><strong class="userinput"><code>(polygon &amp;P[10 20] &amp;P[50 60] &amp;P[90 0])</code></strong>
  105. <img src="images/polygon-1.png"/>
  106. </pre>
  107. <h3 id="idm139667880488080">Colors and filling</h3>
  108. <p>You can override the default color (black) using the
  109. <code class="literal">with-paint</code> procedure, which takes a color and a picture
  110. to produce a new picture:
  111. </p>
  112. <pre class="screen"><span class="prompt">#|kawa:6|# </span><strong class="userinput"><code>(with-paint 'red (circle 32))</code></strong>
  113. </pre>
  114. <p>The first argument can be either one of the standard CSS/HTML5 color
  115. names (such as <code class="literal">'red</code> or <code class="literal">'medium-slate-blue</code>),
  116. or an integer representing an sRGB color, usually written
  117. as a hex literal in the form <code class="literal">#xRRGGBB</code>:
  118. </p>
  119. <pre class="screen"><span class="prompt">#|kawa:7|# </span><strong class="userinput"><code>(with-paint #x0808FF (circle 32))</code></strong>
  120. </pre>
  121. <p>The name <code class="literal">with-paint</code> is because the first argument
  122. can be not just a color, but a general “paint”, such as
  123. a gradient or a background image. However, we won’t go into that.
  124. </p>
  125. <p>If the shape is closed, you can “fill” its inside:
  126. </p>
  127. <pre class="screen">(fill (circle 32))
  128. </pre>
  129. <p>You can change the color using <code class="literal">with-paint</code>:
  130. </p>
  131. <pre class="screen">(with-paint 'goldenrod (fill (circle 32)))
  132. </pre>
  133. <p>or as an extra argument to <code class="literal">fill</code>:
  134. </p>
  135. <pre class="screen">(fill 'goldenrod (circle 32))
  136. </pre>
  137. <p>draw TODO
  138. </p>
  139. <h3 id="idm139667880478336">Images</h3>
  140. <p>An image is a picture represented as a rectangular grid of color values.
  141. It may be a photograph from a camera, or be created by a painting
  142. program like Photoshop or gimp.
  143. You can use <code class="literal">image-read</code> to read an image from a file,
  144. typically a <code class="literal">.png</code> or <code class="literal">.jpg</code> file.
  145. </p>
  146. <pre class="screen"><span class="prompt">#|kawa:10|# </span><strong class="userinput"><code>(define img1 (image-read "http://pics.bothner.com/2013/Cats/06t.jpg"))</code></strong>
  147. <span class="prompt">#|kawa:11|# </span><strong class="userinput"><code>img1</code></strong>
  148. <img src="images/image-cat-1a.png"/>
  149. </pre>
  150. <h3 id="idm139667880472832">Transforms TODO</h3>
  151. <pre class="screen"><span class="prompt">#|kawa:12|# </span><strong class="userinput"><code>(scale 0.6 (rotate 30 img1))</code></strong>
  152. <img src="images/image-cat-1b.png"/>
  153. </pre>
  154. <h3 id="idm139667880470144">Combining and adjusting pictures TODO</h3>
  155. <h3 id="idm139667880469440">Using and combining pictures TODO</h3>
  156. </section>
  157. <footer>
  158. <div class="navfooter">
  159. <p>
  160. Up: <a accesskey="u" href="tutorial-index.xhtml">Kawa Scheme Tutorial</a></p>
  161. <p>
  162. Previous: <a accesskey="p" href="tutorial-Variables.xhtml">Variables</a></p>
  163. <p>
  164. Next: <a accesskey="n" href="tutorial-Sequences.xhtml">Lists and sequences</a></p>
  165. </div>
  166. </footer>
  167. </body>
  168. </html>