Basic "Shapes" Library complex
Library name: shapes
Functions to create and label basic shapes by using preexisting ASCIIsvg macro. Version January 2022
By default, an image description is generated for each shape.
To import the library, include loadlibrary("shapes") in the question's common control.
The functions in this library can be used to draw and label basic shapes:
List of functions:
Function output:
These functions output images that are defined in code by $gr = function("arguments") and then placed into a problem's text with $gr.
Functions:
draw_angle
Draws and labels an angle.
Syntax: draw_angle("measurement,[label],[noarc]",["rotate,[ang]"],["axes"])
The first option must include the angle measurement.
Each option is a list inside quotes. Options include:
- "measurement,[label],[noarc]" -- The label will include the degree symbol by default. To not use degree symbol, use "rad", e.g. "57,1rad". Leave label blank to not include a label. Angle arc will be drawn to show direction of angle. To not show the arc, put noarc as the last entry.
- "rotate,[deg]" -- Rotates the image by deg degrees. If "deg" is omitted, rotates by a random angle.
- "axes" -- Draws the xy axes.
- "size,length" -- Changes the size of the image to be lengthxlength. Default is 300x300.
- "alttext,text" overwrites the auto-generated alttext for the image.
- Note: Angles must be strictly between -2000 and 2000 degrees.
- Note: Greek letters (including pi, alpha, beta, gamma, theta, phi, tau) will be displayed as their html symbols. E.g. you can type a label as "3 pi/4" or "alpha". Using Greek letters will suppress the degree symbol.
Examples:
$gr = draw_angle("340,theta","axes") -- Draws a 340 degree angle, labeled θ, in standard position on coordinate axes, and an angle arc with arrow is shown.
$gr = draw_angle("120,2pi/3,noarc") -- This draws a 120 degree angle in standard position, labeled 2π/3. Angle arc is not drawn.
$gr = draw_angle("-150,-150","axes","rotate") -- This draws a 150 degree angle clockwise, labeled -150°. Coordinate axes are drawn, and starting ray of the angle is rotated by a random angle. Angle arc is shown with arrow.
draw_circle
Draws and labels a circle.
Syntax: draw_circle(["option 1", "option 2", ...])
draw_circle() -- this draws a circle with no labels.
Each option is a list inside quotes. Options include:
- "center,[label]" -- Draws the center point with an optional label.
- "radius,[label]" -- Draws a horizontal radius with an optional label.
- "diameter,[label]" -- Draws a diameter with an optional label.
- "angle,measurement,[label]" -- Draws an angle measured counterclockwise from positive x-direction, with optional angle label.
- Note: Angle must be given in degrees, and degree symbol will be displayed by default. To not display degree symbol, use "rad" in the label. E.g. "angle,57,1 rad".
- "point,angle,[label]" -- Draws a point on the circle where angle is measured counterclockwise from positive x-direction. Optional label on point.
- Note: Greek letters (including pi, alpha, beta, gamma, theta, phi, tau) will be displayed as their html symbols. E.g. you can type a label as "3 pi/4" or "alpha". Using Greek letters will suppress the degree symbol.
- Note: If a label is a point, type it as (x;y).
- "axes" -- Draws the x and y axes.
- "size,length" Sets the image size to be length x length. [Default is 350x350]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_circle("diameter,7 feet") -- This is a circle with a diameter labeled with "7 feet".
$gr = draw_circle("center,P", "angle,45,pi/4") -- This circle has a center labeled P and a 45 degree angle drawn with the label π/4.
$gr = draw_circle("radius", "point,0,M", "point,90") -- This is a circle with a horizontal radius (unlabeled) drawn from the center to labeled point M. Also, point at 90 degrees is drawn but not labeled.
draw_circlesector
Draws and labels a scaled sector of a circle.
Syntax: draw_circlesector("angle,measurement,[label]", ["option 1", "option 2", ...])
draw_circlesector("angle,measurement,[label]") -- this draws a circle sector with angle measurement (given in degrees), and optional label.
Note: Up to two "angle" arguments may be used. Test to make sure labels do not overlap (for close angles).
Using "angle,measurement" is required for draw_circlesector.
Each option is a list inside quotes. Options include:
- "center,[label]" Plots the center of the circle with optional label. If label is a point (x,y), type it as (x;y).
- "radius,[label]" Draws a horizontal radius from the center, with optional label.
- "point,angle,[label]" Plots a point on the circle at angle measured counterclockwise. If optional label is a point (x,y), type is as (x;y).
- "axes" -- Draws the x and y axes.
- "size,length" Sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
- Note: Angle must be given in degrees, and degree symbol will be displayed by default. To not display degree symbol, use "rad" in the label. E.g. "angle,57.3,1 rad".
- Note: Greek letters (including pi, alpha, beta, gamma, theta, phi, tau) will be displayed as their html symbols. E.g. you can type a label as "3 pi/4" or "alpha". Using Greek letters will suppress the degree symbol.
Examples:
$gr = draw_circlesector("angle,45,pi/4") -- This is a 45 degree sector with angle labeled π/4.
$gr = draw_circlesector("angle,45", "point,45,(3;3)", "axes") -- This is a 45 degree sector with point on circle at 45 degrees labeled (3,3). The x and y axes are shown.
draw_cone
Draws and labels a right, circular cone.
Syntax: draw_cone([diameter,height],[option 1],[option 2],[option 3]
draw_cone() -- draws a random cone with no labels. The point of the cone is at the top.
draw_cone(a,b) -- makes a scaled drawing of a cone with diameter 'a' and height 'b'.
Each option is a list inside quotes. Options include:
- "radius,[label]" Draws a radius on the circular base with optional label.
- "diameter,[label]" Draws a diameter on the circular base with optional label.
- "height,[label]" Draws the height of the cone with optional label.
- "slant,[label]" Labels the slant height of the cone.
- "fill,[percent]" Fills the cone up to specified percent of its height. If 'percent' omitted, fills up to a random percent of the height.
- "size,[length]" Sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_cone(1,1,"radius,r=12") -- A cone with equal diameter and height, with radius drawn and labeled 'r=12'.
$gr = draw_cone(height,x","fill,30","invert") -- A random cone, inverted, with its height labeled 'x' and filled up to 30 percent of its height.
draw_cylinder
Draws and labels a circular cylinder.
Syntax: draw_cylinder([diameter,height],[option 1],[option 2],[option 3]
draw_cylinder() -- draws a random cylinder with no labels.
draw_cylinder(a,b) -- makes a scaled drawing of a cylinder with diameter 'a' and height 'b'.
Each option is a list inside quotes. Options include:
- "radius,[label]" Draws a radius on the top of the cylinder with optional label.
- "diameter,[label]" Draws a diameter on the top of the cylinder with optional label.
- "height,[label]" Labels the height of the cylinder.
- "fill,[percent]" Fills a specified percent of the cylinder. If 'percent' omitted, fills a random percent.
- "size,[length]" Sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_cylinder(1,1,"radius,r=12") -- A cylinder with equal diameter and height, with radius drawn and labeled 'r=12'.
$gr = draw_cylinder("height,x","fill,30") -- A random cylinder with height labeled 'x' and filled 30 percent.
draw_rectangle
Draws a scaled rectangle with labels.
Syntax: draw_rectangle(["option 1", "option 2", ...])
draw_rectangle() -- this draws a random rectangle with no labels
Each option is a list inside quotes. Options include:
- "base,b,[label]" Must be used with "height". Draws scaled rectangle with base "b" and height "h" with optional label on bottom horizontal side.
- "height,h,[label]" Must be used with "base". Draws scaled rectangle with base "b" and height "h" with optional label on right-hand vertical side.
- "points,lab1,lab2,lab3,lab4" Plots the vertices with optional labels (counterclockwise from bottom left). If label is a point (a,b), type it as (a;b).
- "size,length" Sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Notes:
- "base" and "height" must be used together as they define the size of the rectangle.
Examples:
$gr = draw_rectangle("base,1,x","height,2,y","points,(0;0),(5;0)") -- A rectangle with 1:2 ratio of base-to-height, bottom horizontal side labeled "x", right-hand vertical side labeled "y", and bottom two vertices labeled (0,0) and (5,0).
draw_rectprism
Draws and labels a rectangular prism.
Syntax: draw_rectprism([length,depth,height],[option 1],[option 2],[option 3]
draw_rectprism() -- draws a random rectangular prism with no labels.
draw_rectprism(length,depth,height) -- draws a scaled rectangular prism with no labels.
Each option is a list inside quotes. Options include:
- "labels,lengthlab,depthlab,heightlab" labels the length, depth and height of the prism. You can omit one or more labels, such as "labels,,,heightlab".
- "fill,[percent]" fills the prism up to specified percent of its height. If 'percent' omitted, fills up to a random percent of the height.
- "size,length" sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_rectprism(4,5,6,"labels,a,,c","fill,30") -- A rectangular prism that is 4 units long, 5 units deep and 6 units tall, with front length edge labeled 'a' and height edge labeled 'c'. Prism is filled up to 40 percent of its height.
draw_sphere
Draws and labels a sphere.
Syntax: draw_sphere([option 1],[option 2],[option 3]
draw_sphere() -- draws a sphere with no labels.
Each option is a list inside quotes. Options include:
- "radius,[label]" Draws a radius with optional label.
- "diameter,[label]" Draws a diameter with optional label.
- "fill,[percent]" Fills the sphere up to specified percent of its height. If 'percent' omitted, fills up to a random percent of the height.
- "size,[length]" Sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_sphere("radius,r=12","fill,50") -- A sphere with radius drawn and labeled 'r=12', filled half way up from the bottom.
draw_square
Draws and labels a square.
Syntax: draw_square(["option 1", "option 2", ...])
draw_square() -- this draws an unlabeled square
Each option is a list inside quotes. Options include:
- "base,[label]" Labels the bottom horizontal side.
- "height,[label]" Labels the right-hand vertical side.
- "points,lab1,lab2,lab3,lab4" Plots the vertices with optional labels (counterclockwise from bottom left). If label is a point (a,b), type it as (a;b).
- "size,length" Sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_square("base,x","points,(0;0),(5;0)") -- A square with bottom horizontal side labeled "x", and bottom two vertices labeled (0,0) and (5,0).
draw_triangle
Draws a scaled triangle, where size can be defined by its "angles", "sides" or "random".
Syntax: draw_triangle(["option 1", "option 2", ...])
draw_triangle() -- this draws a random triangle with no labels.
Each option is a list inside quotes. Options include:
- "angles,A,B,C,[lab1,lab2,lab3,arc1,arc2,arc3]" Here, A,B,C are angles (in degrees) that define the triangle; lab1,lab2,lab3 are the angle labels; and arc1,arc2,arc3 can be (,(( or ((( to make arc symbols.
- "sides,a,b,c,[lab1,lab2,lab3,tick1,tick2,tick3]" Here, a,b,c are sides that define the triangle; lab1,lab2,lab3 are the side labels; and tick1,tick2,tick3 can be |,|| or ||| to make side tickmarks.
- "points,[P,Q,R]" Draws points on the vertices, and P,Q,R are optional labels on those points. If a label is a point (a,b), type it as (a;b).
- "bisector,[_,_,_,lab1,lab2,lab3,ptlab1,ptlab2,ptlab3]" The three entries following "bisector" correspond to the order of the angles. Put a "1" to draw the angle bisector for that angle; leave blank (or put a zero) to not draw the bisector. lab1,lab2,lab3 are labels for the bisector, and ptlab1,ptlab2,ptlab3 are labels for where the bisectors intersect their opposite sides.
- "median,[_,_,_,lab1,lab2,lab3,ptlab1,ptlab2,ptlab3]" Similar to bisector. Putting a "1" will draw the median for that angle. lab1,lab2,lab3 are labels for the medians, and ptlab1,ptlab2,ptlab3 are labels for where the medians intersect their opposite sides
- "altitude,[_,_,_,lab1,lab2,lab3,ptlab1,ptlab2,ptlab3]" Similar to bisector. Putting a "1" will draw the altitude for that angle. lab1,lab2,lab3 are labels for the altitudes, and ptlab1,ptlab2,ptlab3 are labels for where the altitudes intersect their opposite (extended) sides. Dashed lines are drawn to extend opposite side if necessary.
- "random" Creates random angles for the triangle. Using this means "angles" should omit A,B,C and "sides" should omit a,b,c, but both can have labels. [i.e. Don't leave blanks, but completely omit those entries, such as "angles,lab1,lab2,lab3".]
- "rotate,[ang]" Rotates the triangle counterclockwise by ang degrees. If "ang" is omitted, rotates by a random angle.
- "size,length" Sets the image size to be length x length. [Default is 350x350]
- "alttext,text" overwrites the auto-generated alttext for the image.
Notes
- If "angles" is given after "sides", then A,B,C are omitted (since the "sides,a,b,c" will define the triangle).
- If "sides" is used after "angles", then a,b,c are omitted (since the "angles,A,B,C" will define the triangle).
- If "angles" is used to define triangle, then angles should be given in degrees, and the degree symbol will displayed by default. To not display the degree symbol, use "rad" in the label. E.g. "angles,50,60,70,A rad,B rad,C rad"
- If neither "angles" nor "sides" are used, then defaults to "random".
- The right angle box symbol is always displayed, unless an angle arc is also used on the right angle.
- Common Greek letters (pi, alpha, beta, gamma, theta, phi, tau) will be displayed as their html symbols. E.g. you can type a label as "3 pi/4" or "alpha". Using a Greek letter will suppress the degree symbol.
- If a label is a point, type it as (x;y).
- For "sides,a,b,c" and "angles,A,B,C", the number "a" is the side opposite angle "A", etc.
- By default, triangles are drawn with vertex 0 at bottom left, vertex 1 at bottom right, and vertex 2 at top. Use "rotate" to rotate the triangle.
Examples:
$gr = draw_triangle("angles,30,60,90,pi/6,pi/3") -- A 30-60-90 right triangle with angle labels π/6 and π/3. The right angle box is displayed by default.
$gr = draw_triangle("sides,5,10,12,a,b,c", "angles,A,B,C") -- A triangle with side lengths 5, 10 and 12, labeled with letters a,b,c. Angles are labeled with A,B,C.
$gr = draw_triangle("rotate","points") -- A random triangle with points on the vertices (unlabeled), drawn with random rotation.
$gr = draw_triangle("angles,30,90,60","rotate,90") -- A 30-60-90 triangle rotated so the right edge is drawn vertically and the right angle is at top right.
$gr = draw_triangle("random","points,P,Q,R","angles,a,b,c,(,((,(((") -- A triangle with random angles, points are labeled P,Q,R, angles are labeled a,b,c and they have arcs (, (( and (((.
$gr = draw_triangle("points,A,B,C","angles,30,110,40","altitude,1,0,0,P") -- A 30-110-40 triangle (unlabeled angles) with vertices labeled A,B,C, and an altitude drawn down from point A, labeled with the letter "P".
draw_polygon
Draws and labels a polygon with n sides.
Points drawn counterclockwise. By default, first side is horizontal on the bottom.
Syntax: draw_polygon([n],["option 1", "option 2", ...])
draw_polygon() -- this draws a random polygon with between 3 and 9 sides.
draw_polygon(n) -- this draws an n-sided polygon. Maximum number of sides is 50.
Each option is a list inside quotes. Options include:
- "points,[lab1,lab2,...,labn]" Labels the vertices counterclockwise from bottom-left. If label is a point (a,b), type it as (a;b).
- "sides,lab1,lab2,...,labn" Labels the sides counterclockwise from bottom side.
- "regular" Makes it a regular polygon
- "rotate" Rotates polygon by random angle.
- "axes" Shows x and y axes.
- "size,length" Sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_polygon(5,"axes","norotate","regular","points,P") -- A regular pentagon with points on vertices, and first vertex on positive x-axis labeled "P".
draw_prismcubes
Draws a rectangular prism sliced into cubes.
Syntax: draw_prismcubes([option 1],[option 2],[option 3]
draw_prismcubes() -- this draws a cube with no labels.
Each option is a list inside quotes. Options include:
- "cubes,length,height,depth" Draws a rectangular prism made of cubes, that is length x height x depth. Maximum of each dimension is 20.
- "labels,lab1,lab2,lab3" This labels the length, height and depth of the prism.
- "size,length" Sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_prismcubes("cubes,2,5,3","labels,a,b,c") -- A 2x5x3 rectangular prism of cubes, dimensions labeled a, b and c.
$gr = draw_prismcubes("labels,L,H,W") -- A 1x1x1 cube, dimensions labeled L, H and W.
draw_pyramid
Draws and labels a pyramid with rectangular base.
Syntax: draw_pyramid([length,width,height],[option 1],[option 2],[option 3]
draw_pyramid() -- draws a random pyramid with no labels.
Each option is a list inside quotes. Options include:
- "length,label" labels the front edge of the rectangular base.
- "width,label" labels the front edge of the rectangular base.
- "height,[label]" draws the height of the pyramid with optional label.
- "slant1,[label]" draws the slant height on the right face of the pyramid with optional label.
- "slant2,[label]" draws the slant height on the front face of the pyramid with optional label.
- "fill,[percent]" fills the pyramid up to specified percent of its height. If 'percent' omitted, fills up to a random percent of the height.
- "invert" inverts the pyramid.
- "size,length" sets the image size to be length x length. [Default is 300x300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_pyramid(4,5,6,"length,4","width,5","height,6","fill,30","invert") -- A pyramid with tip at bottom and rectangular base at top, filled with colored material up to 30 percent of its height, with the rear edge of the base labeled 4, the left edge of the base labeled 5 and its height drawn and labeled 6.
draw_polyomino
Draws and labels a random, connected polyomino.
Syntax: draw_polyomino([n],[option 1],[option 2],[option 3])
draw_polyomino() -- draws a random polyomino with between 3 and 9 squares.
draw_polyomino(n) -- draws a random polyomino with at least n squares.
Each option is a list inside quotes. Options include:
- "fill,[color]" shades in the polyomino with optional color. Omit [color] to get a random color.
- "grid" shows the background grid.
- "data" will make the function output an array containing [the graph,area,perimeter] for the polyomino.
- "size,length" sets the image size to be length pixels wide. [Default width is 300]
- "alttext,text" overwrites the auto-generated alttext for the image.
Examples:
$gr = draw_polyomino(5,"fill,transblue","grid") -- A random polyomino with 5 squares (i.e. a "pentomino"), shaded transparent blue, and shown on a grid.
$gr,$a,$p = draw_polyomino("fill","data") -- $gr is a random polyomino with between 3 and 9 squares, shaded with a random color; $a is the number of squares in the shape; $p is the perimeter of the shape
Back to top of page