June 2022
Intermediate to advanced
130 pages
2h 45m
English
Roughly stated, a polygon is a shape made up of the area between connected lines. For example, triangles are polygons with three sides. Let’s create a module for working with polygons.
We’ll start with a few constructors. Here are a couple of constructors for building squares and rectangles:
| | defmodule Polygon do |
| | def rectangle({x, y}, height, width) do |
| | [{x, y}, {x+width, y}, {x+width, y+height}, {x, y+height}] |
| | end |
| | |
| | def square(point, length) do |
| | rectangle(point, length, length) |
| | end |
| | end |
Both of these functions take the building blocks of the shape and return a polygon, a list of points. Now, let’s build some reducers. We’ve already done the bulk of the work in the Point module. The functions in ...
Read now
Unlock full access