Geometry

We begin by looking at the geometric objects available in Yas. To use these object types, you must first activate the Geometry module:

use Geometry

Points

A point is a the most primitive geometric construct. It represents a location in space, specified by its coordinates, x and y.

Points are represented in Yas by the builtin object type point:

console

Notice that the print command output for the point object includes a third coordinate (which is equal to 0). This is because points (and vectors, see below) in Yas are 3-dimensional, with the third coordinate, z, being equal to 0 by default. For now, we will stick to using 2-dimensional objects that we will paint on our simple 2-dimensional canvas.

Origin and Coordinate Axes

The origin is defined as the point with all coordinates set to 0:

use Geometry

let O = point(0, 0)     # it is customary to refer to the origin by the letter O

By default convention in 2D screen graphics, the origin is mapped to the top-left corner of the canvas, with the positive direction of the X-axis pointing to the right and the positive direction of the Y-axis pointing down.

Vectors

In geometry, vectors represent displacement or motion from one point to another. Like points, vectors have coordinates (in 3D space) which stand for displacement along the x and y axes (also z in 3D space). Use the vector builtin object type to represent a vector:

use Geometry

let V = vector(10, 10)       # 2D vector
print V.x, V.y

Vector Operations

Some common vector operations are supported:

  • addition and subtraction of vectors
  • multiplication of a vector by a scalar
  • inner product of two vectors
  • testing vectors for equality
  • magnitude
  • slope

console

Points Are Just Vectors

Each point P in space can be associated with a unique vector pointing from the origin to P. For this reason, the point objects in Yas are just a special kind of vector, and in fact, the vector operations above can also be applied to points. Using vector arithmetic with points, it is easy to find answers to questions like:

  • Where does a point P end up after being displaced by a vector V? (Vector addition: P + V)
  • What is the vector pointing from point A to point B? (Vector subtraction: B - A)
  • How far are two points from each other? (Magnitude of the difference vector, i.e.: (B - A).abs())

Lines and line segments

Lines are represented by the builtin object type line. A line can be constructed given two points:

console

A line segment can be similarly constructed:

console

The difference between lines and segments is that, when drawn on the screen, lines extend to the edges of the canvas, whereas a segment is bound between its endpoints.

Paths and Polygons (TO BE IMPLEMENTED)

Paths and polygons are defined by giving a sequence of vertex points that are consecutively connected by edges:

let p1 = point(0, 0)
let p2 = point(0, 10)
let p3 = point(10, 0)

let path1 = path(p1, p2, p3)        # open path
let poly1 = polygon(p1, p2, p3)     # polygon = closed path (triangle)

The difference between paths and polygons is that a polygon is a closed path, meaning that we assume that there is an additional edge connecting the last vertex of a polygon point sequence to the first.

Other Shapes

Rectangles

A rectangle is given by a point representing its top-left corner, a width and a height:

console

Circles

A circle is defined by giving its center point and radius:

console

Back (Table of Contents)
Forward (020-drawing-on-canvas)