Contents

Shapes

Learn how to draw common geometric shapes

A number of shapes can be drawn, such as triangles, squares, rectangles, quadrilaterals, lines, and circles. All instance attributes on all shapes can be read and set individually on a new object, for example:

t = Triangle.new
t.x1 = 50
t.y3 = 25
t.color = 'red'

Triangles

Create a new triangle using:

Triangle.new

Here are all the optional instance attributes:

Triangle.new(
  x1: 50,  y1: 0,
  x2: 100, y2: 100,
  x3: 0,   y3: 100,
  color: 'red',
  z: 100
)

Squares

Create a square using:

Square.new

Here are all the optional instance attributes:

Square.new(
  x: 100, y: 200,
  size: 125,
  color: 'blue',
  z: 10
)

Rectangles

Create a rectangle using:

Rectangle.new

Here are all the optional instance attributes:

Rectangle.new(
  x: 125, y: 250,
  width: 200, height: 150,
  color: 'teal',
  z: 20
)

Quadrilaterals

Create a quadrilateral using:

Quad.new

Here are all the optional instance attributes:

Quad.new(
  x1: 275, y1: 175,
  x2: 375, y2: 225,
  x3: 300, y3: 350,
  x4: 250, y4: 250,
  color: 'aqua',
  z: 10
)

Lines

Create a line using:

Line.new

Here are all the optional instance attributes:

Line.new(
  x1: 125, y1: 100,
  x2: 350, y2: 400,
  width: 25,
  color: 'lime',
  z: 20
)

Circles

Create a circle using:

Circle.new

Here are all the optional instance attributes:

Circle.new(
  x: 200, y: 175,
  radius: 150,
  sectors: 32,
  color: 'fuchsia',
  z: 10
)

Continue to the next topic ▸


✏️ Suggest an edit to this page