From ca6e592cecaf90bb4e4710f39298f3a9224ca291 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Wed, 18 Jul 2018 20:05:34 +0200 Subject: [PATCH] Added linear algebra package. --- lin/projection32.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lin/projection32.go diff --git a/lin/projection32.go b/lin/projection32.go new file mode 100644 index 0000000..8522845 --- /dev/null +++ b/lin/projection32.go @@ -0,0 +1,22 @@ +package lin + +import "opslag.de/schobers/geom" + +func basisToPoints(p1, p2, p3, p4 geom.PointF32) geom.Matrix3x3F32 { + var m = geom.Mx3x3F32(geom.Vec3F32(p1.X, p1.Y, 1), geom.Vec3F32(p2.X, p2.Y, 1), geom.Vec3F32(p3.X, p3.Y, 1)) + var v = m.Adj().MulV(geom.Vec3F32(p4.X, p4.Y, 1)) + return m.MulM(geom.Mx3x3F32(geom.Vec3F32(v.X, 0, 0), geom.Vec3F32(0, v.Y, 0), geom.Vec3F32(0, 0, v.Z))) +} + +// Projection32 calculates the projection matrix to transformation a point in rectangle src1, src2, src3, src4 to a point in rectangle dst1, dst2, dst3 and dst4. +func Projection32(src1, dst1, src2, dst2, src3, dst3, src4, dst4 geom.PointF32) geom.Matrix3x3F32 { + var s = basisToPoints(src1, src2, src3, src4) + var d = basisToPoints(dst1, dst2, dst3, dst4) + return d.MulM(s.Adj()) +} + +// Project32 projects a point pt using the projection matrix p and returns the resulting point. +func Project32(p geom.Matrix3x3F32, pt geom.PointF32) geom.PointF32 { + var res = p.MulV(geom.Vec3F32(pt.X, pt.Y, 1)) + return geom.PtF32(res.X/res.Z, res.Y/res.Z) +}