An example showing how to use the Line command
// Under a shell type: box -l g snake.box
// ---
// This example shows the flexibility of the Window.Line instruction, which
// lets you draw lines with variable width: for each point of the line you
// can specify the enter-width and the exit-width. You can also use different
// join styles in the same line. This example shows in particular how to
// define a customized line terminator: predefined line terminators exist
// to draw arrows, but you can also use your own line terminator and draw
// a snake!
include "g"
// Here we draw our line-terminator: this is just a common Window object
// where some hot points have been defined.
eye = Window[][
Circle[color.yellow, (0, 0), 1, 0.5]
Circle[color.dark_green, (0, 0), 0.1, 0.5]
]
tongue = Window[][
Poly[color.dark_red, (0.5, 0), (0.5, 4), 0, 1, (0.5, 5), (1, 5)
(0, 5), (0, 4.5), (0, 5), (-1, 5), (-0.5, 5), (-0.5, 4)
0, 0, (-0.5, 0)]
]
// Now we can use the tongue and the eye figures to draw the face of a snake
face = Window[][
a = Point[(5, -5)], b = Point[(7, 0)]
c = Point[(4, 8)], d = Point[(0, 9)]
Poly[color.dark_green, 0.5, a, b, c, d,
(-c.x, c.y), (-b.x, b.y), (-a.x, a.y)]
Put[tongue, (0, 6)]
color.yellow
Put[eye, (2.6, 0), -70, Scale[1.5]]
Put[eye, (-2.6, 0), 70, Scale[1.5]]
color.dark_green
// These hot points are used by the Window.Line instruction to place
// the figure at the end of the line.
// Window.Line behaves as follows:
// - it moves the figure such that "head" goes to the final point
// of the line;
// - it rotates the figure such that the point "tail" is as near
// as possible to the second last point of the line;
// - it scales the figure such that the distance "tail"-"head" is equal
// to the width of the line.
// - after the figure has been placed, the line is drawed by connecting
// the second last point to the "join" point.
Hot["head", (0, 9); "tail", (0, 5); "join", (0, -4)]
]
// Now we can draw as many snakes as we want with just one Line instruction!
snake = Window[]
\ snake.Line[color.dark_green, line.smooth, (10, 10), 2, 8, (40, 100), 8, 10
(80, 10), (120, 90), (150, 25), (170, 55), face, (195, 70)]
snake.Save["snake.png", Window["rgb24", .Res[Dpi[100]]]]