1
0

Added simplified API for transformation operations

This commit is contained in:
neviyn 2020-11-08 22:08:15 +00:00
parent 1fc64a535d
commit a076b82bfc

View File

@ -1,17 +1,42 @@
import "./matrix" import "./matrix"
from "./tuple" import Tuple4
import math import math
proc translation*(x, y, z: float): Matrix = matrix(@[1.0, 0.0, 0.0, x, 0.0, 1.0, 0.0, y, 0.0, 0.0, 1.0, z, 0.0, 0.0, 0.0, 1.0], 4, 4) proc translation(x, y, z: float): Matrix = matrix(@[1.0, 0.0, 0.0, x, 0.0, 1.0, 0.0, y, 0.0, 0.0, 1.0, z, 0.0, 0.0, 0.0, 1.0], 4, 4)
proc scaling*(x, y, z: float): Matrix = matrix(@[x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0, z, 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4) proc translate*(mat: Matrix, x, y, z : float): Matrix = translation(x, y, z) * mat
proc rotationX*(radians: float): Matrix = matrix(@[1.0, 0.0, 0.0, 0.0, 0.0, cos(radians), -sin(radians), 0.0, 0.0, sin(radians), cos(radians), 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4) proc translate*(tup: Tuple4, x, y, z : float): Tuple4 = translation(x, y, z) * tup
proc rotationY*(radians: float): Matrix = matrix(@[cos(radians), 0.0, sin(radians), 0.0, 0.0, 1.0, 0.0, 0.0, -sin(radians), 0.0, cos(radians), 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4) proc scaling(x, y, z: float): Matrix = matrix(@[x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0, z, 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4)
proc rotationZ*(radians: float): Matrix = matrix(@[cos(radians), -sin(radians), 0.0, 0.0, sin(radians), cos(radians), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4) proc scale*(mat: Matrix, x, y, z: float): Matrix = scaling(x, y, z) * mat
proc shearing*(x_y, x_z, y_x, y_z, z_x, z_y: float): Matrix = matrix(@[1.0, x_y, x_z, 0.0, y_x, 1.0, y_z, 0.0, z_x, z_y, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4) proc scale*(tup: Tuple4, x, y, z: float): Tuple4 = scaling(x, y, z) * tup
proc rotationX(radians: float): Matrix = matrix(@[1.0, 0.0, 0.0, 0.0, 0.0, cos(radians), -sin(radians), 0.0, 0.0, sin(radians), cos(radians), 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4)
proc rotateX*(mat: Matrix, radians: float): Matrix = rotationX(radians) * mat
proc rotateX*(tup: Tuple4, radians: float): Tuple4 = rotationX(radians) * tup
proc rotationY(radians: float): Matrix = matrix(@[cos(radians), 0.0, sin(radians), 0.0, 0.0, 1.0, 0.0, 0.0, -sin(radians), 0.0, cos(radians), 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4)
proc rotateY*(mat: Matrix, radians: float): Matrix = rotationY(radians) * mat
proc rotateY*(tup: Tuple4, radians: float): Tuple4 = rotationY(radians) * tup
proc rotationZ(radians: float): Matrix = matrix(@[cos(radians), -sin(radians), 0.0, 0.0, sin(radians), cos(radians), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4)
proc rotateZ*(mat: Matrix, radians: float): Matrix = rotationZ(radians) * mat
proc rotateZ*(tup: Tuple4, radians: float): Tuple4 = rotationZ(radians) * tup
proc shearing(x_y, x_z, y_x, y_z, z_x, z_y: float): Matrix = matrix(@[1.0, x_y, x_z, 0.0, y_x, 1.0, y_z, 0.0, z_x, z_y, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0], 4, 4)
proc shear*(mat: Matrix, x_y, x_z, y_x, y_z, z_x, z_y: float): Matrix = shearing(x_y, x_z, y_x, y_z, z_x, z_y) * mat
proc shear*(tup: Tuple4, x_y, x_z, y_x, y_z, z_x, z_y: float): Tuple4 = shearing(x_y, x_z, y_x, y_z, z_x, z_y) * tup
when isMainModule: when isMainModule:
import unittest import unittest