Skip to content

2. Cairofunctions.py

Afras Ashraf edited this page Dec 30, 2021 · 2 revisions

The cairofunctions.py file contains all the objects and functions used for graphing. A table of objects is given below.

Objects

Object Description
Point These are used like vectors. Used to describe coordinates, locations, unit vectors, etc.
Transform These are 2 Dimensional linear transformations. When multiplied with a point, they output the transformed point.
Context This contains most of the code that draws to the cairo context.
Coordinate Grid This objects contains the Coordinate Grid. To plot, you need to define a coordinate grid.

Point

These are used like vectors. Used to describe coordinates, locations, unit vectors, etc.

To make a point object, use the following syntax.

import graphing.cairofunctions as cairofunctions

point = cairofunctions.Point(x, y) # x and y are floats, that represent the X-Coordinate and Y-Coordinate Respectively.

Points have 2 fields. Given in the table below.

Field Description
x Gives the x coordinate of the point object.
y Gives the y coordinate of the point object.

The behavior of point objects with operators is given in the table below.

Operator Return Type Function
Point + Point Point This adds the corresponding X and Y coordinates of the points.
Point * Scalar Point This scales the X and Y coordinates by a scalar.
Point - Point Point This subtracts the corresponding X and Y coordinates of the points.
Point / Scalar Point This scales the X and Y coordinates down by a scalar.

The methods that the point object has is given in the table below.

Method Return Type Function
mod float Returns the Euclidean Length of the Point.
norm Point Returns the unit vector in the direction of the Point.

Transform

When multiplied with a point, the Transform object returns the transformed Point.

To make a Transform object, use the following syntax.

import graphing.cairofunctions as cairofunctions

# The unit vectors of the transformed coordinate system
i = cairofunctions.Point(x1, y1)
j = cairofunctions.Point(x2, y2)

transform = cairofunctions.Transform(i, j)

The Transform object has the following fields.

Field Description
i X Axis Unit Vector
j Y Axis Unit Vector

The Transform object can be multiplied with a Point object. See the Example below.

import graphing.cairofunctions as cairofunctions

i = cairofunctions.Point(0,1)
j = cairofunctions.Point(-1,0)

rot90 = cairofunctions.Transform(i, j) # Linear Transformation corresponding to a pi/2 radians rotation.

point = cairofunctions.Point(2,3)

point_rotated = rot90 * point # Applies Linear Transformation to point.

print(f"Original Point = {point.x, point.y}\n"
    f"Transformed Point = {point_rotated.x, point_rotated.y}"
)
"""
Sample Output:

Original Point = (2, 3)
Transformed Point = (-3, 2)
"""

GRAPHING-CALC

  • Cairofunctions
  • Graphing.py

Clone this wiki locally