Basic 2d support with vectors, points, matrices and some basic utilities. Vectors are implemented as direction vectors, ie. when transformed with a matrix the translation part of matrix is ignored. Operators + , - , * , / , += , -= , *= and /= are implemented for vectors and scalars.
Quick start example:
# Create a matrix which first rotates, then scales and at last translates var m:Matrix2d=rotate(DEG90) & scale(2.0) & move(100.0,200.0) # Create a 2d point at (100,0) and a vector (5,2) var pt:Point2d=point2d(100.0,0.0) var vec:Vector2d=vector2d(5.0,2.0) pt &= m # transforms pt in place var pt2:Point2d=pt & m #concatenates pt with m and returns a new point var vec2:Vector2d=vec & m #concatenates vec with m and returns a new vectorimplements binary operators
+
, -
, *
and /
for vectorsimplements binary operators +
, -
, *
and /
for vectorsimplements binary operators +
, -
, *
and /
for vectorsimplements binary operators +
, -
, *
and /
for vectorsimplements inplace binary operators +=
, -=
, /=
and *=
for vectorsimplements inplace binary operators +=
, -=
, /=
and *=
for vectorsimplements inplace binary operators +=
, -=
, /=
and *=
for vectorsimplements inplace binary operators +=
, -=
, /=
and *=
for vectors Matrix2d = object ax*, ay*, bx*, by*, tx*, ty*: float
Point2d = object x*, y*: float
Vector2d = object x*, y*: float
IDMATRIX: Matrix2d = matrix2d(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
ORIGO: Point2d = point2d(0.0, 0.0)
XAXIS: Vector2d = vector2d(1.0, 0.0)
YAXIS: Vector2d = vector2d(0.0, 1.0)
DEG360 = 6.283185307179586
DEG270 = 4.71238898038469
DEG180 = 3.141592653589793
DEG90 = 1.570796326794897
DEG60 = 1.047197551196598
DEG45 = 0.7853981633974483
DEG30 = 0.5235987755982988
DEG15 = 0.2617993877991494
proc setElements(t: var Matrix2d; ax, ay, bx, by, tx, ty: float) {.inline, raises: [], tags: [].}
proc matrix2d(ax, ay, bx, by, tx, ty: float): Matrix2d {.noInit, raises: [], tags: [].}
proc `&`(a, b: Matrix2d): Matrix2d {.noInit, raises: [], tags: [].}
proc scale(s: float): Matrix2d {.noInit, raises: [], tags: [].}
proc scale(s: float; org: Point2d): Matrix2d {.noInit, raises: [], tags: [].}
proc stretch(sx, sy: float): Matrix2d {.noInit, raises: [], tags: [].}
proc stretch(sx, sy: float; org: Point2d): Matrix2d {.noInit, raises: [], tags: [].}
proc move(dx, dy: float): Matrix2d {.noInit, raises: [], tags: [].}
proc move(v: Vector2d): Matrix2d {.noInit, raises: [], tags: [].}
proc rotate(rad: float): Matrix2d {.noInit, raises: [], tags: [].}
proc rotate(rad: float; org: Point2d): Matrix2d {.noInit, raises: [], tags: [].}
proc mirror(v: Vector2d): Matrix2d {.noInit, raises: [], tags: [].}
proc mirror(org: Point2d; v: Vector2d): Matrix2d {.noInit, raises: [], tags: [].}
proc skew(xskew, yskew: float): Matrix2d {.noInit, raises: [], tags: [].}
proc `$`(t: Matrix2d): string {.noInit, raises: [], tags: [].}
proc isUniform(t: Matrix2d; tol = 1e-006): bool {.raises: [], tags: [].}
proc determinant(t: Matrix2d): float {.raises: [], tags: [].}
proc isMirroring(m: Matrix2d): bool {.raises: [], tags: [].}
proc inverse(m: Matrix2d): Matrix2d {.noInit, raises: [DivByZeroError], tags: [].}
proc equals(m1: Matrix2d; m2: Matrix2d; tol = 1e-006): bool {.raises: [], tags: [].}
proc `=~`(m1, m2: Matrix2d): bool {.raises: [], tags: [].}
proc isIdentity(m: Matrix2d; tol = 1e-006): bool {.raises: [], tags: [].}
proc apply(m: Matrix2d; x, y: var float; translate = false) {.raises: [], tags: [].}
proc vector2d(x, y: float): Vector2d {.noInit, inline, raises: [], tags: [].}
proc polarVector2d(ang: float; len: float): Vector2d {.noInit, raises: [], tags: [].}
proc slopeVector2d(slope: float; len: float): Vector2d {.noInit, raises: [], tags: [].}
proc len(v: Vector2d): float {.inline, raises: [], tags: [].}
proc len=(v: var Vector2d; newlen: float) {.noInit, raises: [], tags: [].}
proc sqrLen(v: Vector2d): float {.inline, raises: [], tags: [].}
proc angle(v: Vector2d): float {.raises: [], tags: [].}
proc `$`(v: Vector2d): string {.raises: [], tags: [].}
proc `&`(v: Vector2d; m: Matrix2d): Vector2d {.noInit, raises: [], tags: [].}
proc `&=`(v: var Vector2d; m: Matrix2d) {.inline, raises: [], tags: [].}
proc tryNormalize(v: var Vector2d): bool {.raises: [], tags: [].}
proc normalize(v: var Vector2d) {.inline, raises: [DivByZeroError], tags: [].}
proc transformNorm(v: var Vector2d; t: Matrix2d) {.raises: [DivByZeroError], tags: [].}
proc transformInv(v: var Vector2d; t: Matrix2d) {.raises: [DivByZeroError], tags: [].}
proc transformNormInv(v: var Vector2d; t: Matrix2d) {.raises: [], tags: [].}
proc rotate90(v: var Vector2d) {.inline, raises: [], tags: [].}
proc rotate180(v: var Vector2d) {.inline, raises: [], tags: [].}
proc rotate270(v: var Vector2d) {.inline, raises: [], tags: [].}
proc rotate(v: var Vector2d; rad: float) {.raises: [], tags: [].}
proc scale(v: var Vector2d; fac: float) {.inline, raises: [], tags: [].}
proc stretch(v: var Vector2d; facx, facy: float) {.inline, raises: [], tags: [].}
proc mirror(v: var Vector2d; mirrvec: Vector2d) {.raises: [], tags: [].}
proc `-`(v: Vector2d): Vector2d {.raises: [], tags: [].}
proc `+`(a122589, b122591: Vector2d): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `+`(a122593: Vector2d; b122595: float): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `+`(a122597: float; b122599: Vector2d): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `-`(a122610, b122612: Vector2d): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `-`(a122614: Vector2d; b122616: float): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `-`(a122618: float; b122620: Vector2d): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `*`(a122631, b122633: Vector2d): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `*`(a122635: Vector2d; b122637: float): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `*`(a122639: float; b122641: Vector2d): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `/`(a122652, b122654: Vector2d): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `/`(a122656: Vector2d; b122658: float): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `/`(a122660: float; b122662: Vector2d): Vector2d {.inline, noInit, raises: [], tags: [].}
proc `+=`(a122673: var Vector2d; b122675: Vector2d) {.inline, raises: [], tags: [].}
proc `+=`(a122677: var Vector2d; b122679: float) {.inline, raises: [], tags: [].}
proc `-=`(a122727: var Vector2d; b122729: Vector2d) {.inline, raises: [], tags: [].}
proc `-=`(a122731: var Vector2d; b122733: float) {.inline, raises: [], tags: [].}
proc `*=`(a122781: var Vector2d; b122783: Vector2d) {.inline, raises: [], tags: [].}
proc `*=`(a122785: var Vector2d; b122787: float) {.inline, raises: [], tags: [].}
proc `/=`(a122835: var Vector2d; b122837: Vector2d) {.inline, raises: [], tags: [].}
proc `/=`(a122839: var Vector2d; b122841: float) {.inline, raises: [], tags: [].}
proc dot(v1, v2: Vector2d): float {.raises: [], tags: [].}
proc cross(v1, v2: Vector2d): float {.raises: [], tags: [].}
proc equals(v1, v2: Vector2d; tol = 1e-006): bool {.raises: [], tags: [].}
proc `=~`(v1, v2: Vector2d): bool {.raises: [], tags: [].}
proc angleTo(v1, v2: Vector2d): float {.raises: [], tags: [].}
proc angleCCW(v1, v2: Vector2d): float {.raises: [], tags: [].}
proc angleCW(v1, v2: Vector2d): float {.raises: [], tags: [].}
proc turnAngle(v1, v2: Vector2d): float {.raises: [], tags: [].}
proc bisect(v1, v2: Vector2d): Vector2d {.noInit, raises: [], tags: [].}
proc point2d(x, y: float): Point2d {.noInit, inline, raises: [], tags: [].}
proc sqrDist(a, b: Point2d): float {.raises: [], tags: [].}
proc dist(a, b: Point2d): float {.inline, raises: [], tags: [].}
proc angle(a, b: Point2d): float {.raises: [], tags: [].}
proc `$`(p: Point2d): string {.raises: [], tags: [].}
proc `&`(p: Point2d; t: Matrix2d): Point2d {.noInit, inline, raises: [], tags: [].}
proc `&=`(p: var Point2d; t: Matrix2d) {.inline, raises: [], tags: [].}
proc transformInv(p: var Point2d; t: Matrix2d) {.inline, raises: [DivByZeroError], tags: [].}
proc `+`(p: Point2d; v: Vector2d): Point2d {.noInit, inline, raises: [], tags: [].}
proc `+=`(p: var Point2d; v: Vector2d) {.noInit, inline, raises: [], tags: [].}
proc `-`(p: Point2d; v: Vector2d): Point2d {.noInit, inline, raises: [], tags: [].}
proc `-`(p1, p2: Point2d): Vector2d {.noInit, inline, raises: [], tags: [].}
proc `-=`(p: var Point2d; v: Vector2d) {.noInit, inline, raises: [], tags: [].}
proc `=~`(p1, p2: Point2d): bool {.inline, raises: [], tags: [].}
proc polar(p: Point2d; ang, dist: float): Point2d {.noInit, raises: [], tags: [].}
proc rotate(p: var Point2d; rad: float) {.raises: [], tags: [].}
proc rotate(p: var Point2d; rad: float; org: Point2d) {.raises: [], tags: [].}
proc scale(p: var Point2d; fac: float) {.inline, raises: [], tags: [].}
proc scale(p: var Point2d; fac: float; org: Point2d) {.inline, raises: [], tags: [].}
proc stretch(p: var Point2d; facx, facy: float) {.inline, raises: [], tags: [].}
proc stretch(p: var Point2d; facx, facy: float; org: Point2d) {.inline, raises: [], tags: [].}
proc move(p: var Point2d; dx, dy: float) {.inline, raises: [], tags: [].}
proc move(p: var Point2d; v: Vector2d) {.inline, raises: [], tags: [].}
proc sgnArea(a, b, c: Point2d): float {.raises: [], tags: [].}
proc area(a, b, c: Point2d): float {.raises: [], tags: [].}
proc closestPoint(p: Point2d; pts: varargs[Point2d]): Point2d {.raises: [], tags: [].}
proc normAngle(ang: float): float {.raises: [], tags: [].}
proc degToRad(deg: float): float {.inline, raises: [], tags: [].}
proc radToDeg(rad: float): float {.inline, raises: [], tags: [].}
© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/basic2d.html