scheinerman / SimpleDrawingObjects.jl

Collection of shapes that can be drawn with SimpleDrawing.
MIT License
0 stars 0 forks source link

SimpleDrawingObjects

This is a companion module to SimpleDrawing. Its purpose is to simplify the drawing of basic shapes.

Introduction

This module defines some basic shapes (such as Circle) that can be endowed with attributes (such as line thickness and color). The underlying visualization of these objects is provided by Plots.

The general workflow is to (1) create a shape, (2) specify appearance attributes for that shape, and (3) visualize it using the draw function.

Example

using SimpleDrawingObjects, SimpleDrawing
C = Circle(2-im, 3)         # circle centered at (2,-1) with radius 3
set_linecolor!(C, :red)     
set_linewidth!(C, 2)
set_linestyle!(C, :dash)
newdraw()                   # erases the drawing window (from SimpleDrawing)
draw(C)                     # draws the circle

Here is the result:

Note that the center of the circle is specified as a complex number. Alternatively, we could have used Circle(2,-1,3). Note that Circle(2,3) is understood as Circle(2+0im,3) and would create a circle centered at (2,0).

Comparison

The same image (only using SimpleDrawing) could be accomplished like this:

draw_circle(2-im, 3, color=:red, linestyle=:dash, width=2)

The advantage of using a SimpleDrawingObject is that its appearance attributes (color, line width, etc.) can be modified.

Supported Objects

More information on each of these is provided below.

Drawing objects

The draw function causes the object to be drawn on the screen.

The draw function may be applied to a list (vector) of objects, in which case the objects in the list are drawn in the order presented.

Attributes

Each SimpleDrawingObject has attributes that affect how it is drawn. The following methods are provided for setting object attributes:

These functions pertain only to Points:

These functions pertain only to filled objects:

More generally, use set_attribute!(o, attr, val) to set o's attribute attr to the value val. Use with caution.

Use reset_attributes!(o) to return o to its "factory default" attributes.

The function get_attributes(o) returns a view into o's attribute dictionary which can then be directly manipulated (if you dare).

More information on object attributes can be found in the Plots documentation.

Line Segments

Create a new line segment using one of these:

Polygons

Polygons are created from a list of complex numbers. The following are equivalent

The convenience function Rectangle creates an axis-parallel rectangle.

The interior of a Polygon is blank. To create a filled-in polygon, use FilledPolygon (as well as FilledRectangle).

A polygon with n sides is defined using n points. The last point in the list is joined to the first to create a closed figure. We also provide this function:

Circular Shapes

Circles

Circles are created from a center and a radius:

A FilledCircle is a circle whose interior has a color. Like circles, create with one of these:

The symbols Disc and Disk are synonyms for FilledCircle.

Arcs

Arcs of circles are created with Arc(ctr, rad, t1, t2, t3) where:

We require the intermediante angle t2 because simply specifying the end points of the arc does not determine which piece of the circle we are considering.

The center can also be specified as two real numbers: Arc(x, y, rad, t1, t2, t3).

Ellipses

Create an ellipse using Ellipse(z, rx, ry) where z is the center (complex) and rx and ry are the horizontal and vertical radii. Note that only axis-parallel ellipses can be created. Alternatively, use Ellipse(x, y, rx, ry) for an ellipse centered at (x, y).

For an ellipse with a filled-in interior, use FilledEllipse.

Spline Curves

The functions ClosedCurve and OpenCurve create curves from a list of points (just like Polygon). The curves are cubic splines through those points. The following are all equivalent:

We also have FilledClosedCurve for a closed curve whose interior is colored.

Points

Points in the plane are created with Point(z) or Point(x,y). A Point is rendered as a small dot. These two functions determine the appearance of points:

Finer control over point appearance can be achieved using set_fillcolor! and set_linecolor! for points. The function set_pointcolor! sets the fill and the line of the Point to the same color.