diff --git a/lin/projection32.go b/lin/projection32.go index 8522845..477006b 100644 --- a/lin/projection32.go +++ b/lin/projection32.go @@ -4,19 +4,19 @@ 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))) + var v = m.Adj().MulVec(geom.Vec3F32(p4.X, p4.Y, 1)) + return m.MulMx(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()) + return d.MulMx(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)) + var res = p.MulVec(geom.Vec3F32(pt.X, pt.Y, 1)) return geom.PtF32(res.X/res.Z, res.Y/res.Z) } diff --git a/matrix3x3f32.go b/matrix3x3f32.go index 91fd22e..27290fb 100644 --- a/matrix3x3f32.go +++ b/matrix3x3f32.go @@ -12,25 +12,27 @@ func Mx3x3F32(i, j, k Vector3F32) Matrix3x3F32 { return Matrix3x3F32{i, j, k} } -// ScaleV scales the matrix m by vector v. -func (m Matrix3x3F32) ScaleV(v Vector3F32) Matrix3x3F32 { - return Matrix3x3F32{ - Vec3F32(v.X*m.I.X, v.X*m.I.Y, v.X*m.I.Z), - Vec3F32(v.Y*m.I.X, v.Y*m.I.Y, v.Y*m.I.Z), - Vec3F32(v.Z*m.I.X, v.Z*m.I.Y, v.Z*m.I.Z), - } +// Adj calculates the adjugate matrix of matrix m. +func (m Matrix3x3F32) Adj() Matrix3x3F32 { + return Mx3x3F32( + Vec3F32(m.J.Y*m.K.Z-m.K.Y*m.J.Z, m.K.Y*m.I.Z-m.I.Y*m.K.Z, m.I.Y*m.J.Z-m.J.Y*m.I.Z), + Vec3F32(m.K.X*m.J.Z-m.J.X*m.K.Z, m.I.X*m.K.Z-m.K.X*m.I.Z, m.J.X*m.I.Z-m.I.X*m.J.Z), + Vec3F32(m.J.X*m.K.Y-m.K.X*m.J.Y, m.K.X*m.I.Y-m.I.X*m.K.Y, m.I.X*m.J.Y-m.J.X*m.I.Y), + ) } -// MulV multiplies the matrix m with vector v. -func (m Matrix3x3F32) MulV(v Vector3F32) Vector3F32 { - return Vec3F32( - v.X*m.I.X+v.Y*m.J.X+v.Z*m.K.X, - v.X*m.I.Y+v.Y*m.J.Y+v.Z*m.K.Y, - v.X*m.I.Z+v.Y*m.J.Z+v.Z*m.K.Z) +// Det calculates the determinant of the matrix m. +func (m Matrix3x3F32) Det() float32 { + return m.I.X*m.J.Y*m.K.Z + + m.J.X*m.K.Y*m.I.Z + + m.K.X*m.I.Y*m.J.Z - + m.K.X*m.J.Y*m.I.Z - + m.J.X*m.I.Y*m.K.Z - + m.I.X*m.K.Y*m.J.Z } -// MulM multiplies the matrix m with matrix n. -func (m Matrix3x3F32) MulM(n Matrix3x3F32) Matrix3x3F32 { +// MulMx multiplies the matrix m with matrix n. +func (m Matrix3x3F32) MulMx(n Matrix3x3F32) Matrix3x3F32 { return Matrix3x3F32{ Vec3F32( n.I.X*m.I.X+n.I.Y*m.J.X+n.I.Z*m.K.X, @@ -47,21 +49,19 @@ func (m Matrix3x3F32) MulM(n Matrix3x3F32) Matrix3x3F32 { } } -// Det calculates the determinant of the matrix m. -func (m Matrix3x3F32) Det() float32 { - return m.I.X*m.J.Y*m.K.Z + - m.J.X*m.K.Y*m.I.Z + - m.K.X*m.I.Y*m.J.Z - - m.K.X*m.J.Y*m.I.Z - - m.J.X*m.I.Y*m.K.Z - - m.I.X*m.K.Y*m.J.Z +// MulVec multiplies the matrix m with vector v. +func (m Matrix3x3F32) MulVec(v Vector3F32) Vector3F32 { + return Vec3F32( + v.X*m.I.X+v.Y*m.J.X+v.Z*m.K.X, + v.X*m.I.Y+v.Y*m.J.Y+v.Z*m.K.Y, + v.X*m.I.Z+v.Y*m.J.Z+v.Z*m.K.Z) } -// Adj calculates the adjugate matrix of matrix m. -func (m Matrix3x3F32) Adj() Matrix3x3F32 { - return Mx3x3F32( - Vec3F32(m.J.Y*m.K.Z-m.K.Y*m.J.Z, m.K.Y*m.I.Z-m.I.Y*m.K.Z, m.I.Y*m.J.Z-m.J.Y*m.I.Z), - Vec3F32(m.K.X*m.J.Z-m.J.X*m.K.Z, m.I.X*m.K.Z-m.K.X*m.I.Z, m.J.X*m.I.Z-m.I.X*m.J.Z), - Vec3F32(m.J.X*m.K.Y-m.K.X*m.J.Y, m.K.X*m.I.Y-m.I.X*m.K.Y, m.I.X*m.J.Y-m.J.X*m.I.Y), - ) +// ScaleVec scales the matrix m by vector v. +func (m Matrix3x3F32) ScaleVec(v Vector3F32) Matrix3x3F32 { + return Matrix3x3F32{ + Vec3F32(v.X*m.I.X, v.X*m.I.Y, v.X*m.I.Z), + Vec3F32(v.Y*m.I.X, v.Y*m.I.Y, v.Y*m.I.Z), + Vec3F32(v.Z*m.I.X, v.Z*m.I.Y, v.Z*m.I.Z), + } } diff --git a/pointf.go b/pointf.go index 48dd030..fc42698 100644 --- a/pointf.go +++ b/pointf.go @@ -17,21 +17,11 @@ func PtF(x, y float64) PointF { return PointF{X: x, Y: y} } -// To32 transforms the point p into a PointF32. -func (p PointF) To32() PointF32 { - return PointF32{float32(p.X), float32(p.Y)} -} - // Add adds q as a vector to p. func (p PointF) Add(q PointF) PointF { return PointF{p.X + q.X, p.Y + q.Y} } -// Sub subtracts q as a vector from p. -func (p PointF) Sub(q PointF) PointF { - return PointF{p.X - q.X, p.Y - q.Y} -} - // AngleTo calculates the angle [0..2*Pi) from point p to point q. func (p PointF) AngleTo(q PointF) float64 { a := math.Atan((p.Y - q.Y) / (p.X - q.X)) @@ -81,3 +71,13 @@ func (p PointF) InPolygon(q PolygonF) bool { } return c } + +// Sub subtracts q as a vector from p. +func (p PointF) Sub(q PointF) PointF { + return PointF{p.X - q.X, p.Y - q.Y} +} + +// To32 transforms the point p into a PointF32. +func (p PointF) To32() PointF32 { + return PointF32{float32(p.X), float32(p.Y)} +} diff --git a/pointf32.go b/pointf32.go index 844f7a3..24c39e9 100644 --- a/pointf32.go +++ b/pointf32.go @@ -17,21 +17,11 @@ func PtF32(x, y float32) PointF32 { return PointF32{x, y} } -// To64 transforms the point p into a PointF. -func (p PointF32) To64() PointF { - return PointF{float64(p.X), float64(p.Y)} -} - // Add adds q as a vector to p. func (p PointF32) Add(q PointF32) PointF32 { return PointF32{p.X + q.X, p.Y + q.Y} } -// Sub subtracts q as a vector from p. -func (p PointF32) Sub(q PointF32) PointF32 { - return PointF32{p.X - q.X, p.Y - q.Y} -} - // AngleTo calculates the angle [0..2*Pi) from point p to point q. func (p PointF32) AngleTo(q PointF32) float32 { a := float32(math.Atan(float64((p.Y - q.Y) / (p.X - q.X)))) @@ -63,3 +53,13 @@ func (p PointF32) In(r RectangleF32) bool { } return true } + +// Sub subtracts q as a vector from p. +func (p PointF32) Sub(q PointF32) PointF32 { + return PointF32{p.X - q.X, p.Y - q.Y} +} + +// To64 transforms the point p into a PointF. +func (p PointF32) To64() PointF { + return PointF{float64(p.X), float64(p.Y)} +} diff --git a/rectanglef.go b/rectanglef.go index 3461d21..2bc003f 100644 --- a/rectanglef.go +++ b/rectanglef.go @@ -25,6 +25,21 @@ func (r RectangleF) Add(p PointF) RectangleF { } } +// Center returns the center of the rectangle. +func (r RectangleF) Center() PointF { + return PointF{r.Min.X + .5*(r.Max.X-r.Min.X), r.Min.Y + .5*(r.Max.Y-r.Min.Y)} +} + +// Dx returns the width of r. +func (r RectangleF) Dx() float64 { + return r.Max.X - r.Min.X +} + +// Dy returns the height of r. +func (r RectangleF) Dy() float64 { + return r.Max.Y - r.Min.Y +} + // Inset returns the rectangle r inset by n. The resulting rectangle will never have // a negative size. func (r RectangleF) Inset(n float64) RectangleF { @@ -45,21 +60,6 @@ func (r RectangleF) Inset(n float64) RectangleF { return r } -// Dx returns the width of r. -func (r RectangleF) Dx() float64 { - return r.Max.X - r.Min.X -} - -// Dy returns the height of r. -func (r RectangleF) Dy() float64 { - return r.Max.Y - r.Min.Y -} - -// Center returns the center of the rectangle. -func (r RectangleF) Center() PointF { - return PointF{r.Min.X + .5*(r.Max.X-r.Min.X), r.Min.Y + .5*(r.Max.Y-r.Min.Y)} -} - // Size returns the size of the rectangle. func (r RectangleF) Size() PointF { return PointF{r.Max.X - r.Min.X, r.Max.Y - r.Min.Y} diff --git a/rectanglef32.go b/rectanglef32.go index 8f4e756..698737c 100644 --- a/rectanglef32.go +++ b/rectanglef32.go @@ -25,6 +25,21 @@ func (r RectangleF32) Add(p PointF32) RectangleF32 { } } +// Center returns the center of the rectangle. +func (r RectangleF32) Center() PointF32 { + return PointF32{r.Min.X + .5*(r.Max.X-r.Min.X), r.Min.Y + .5*(r.Max.Y-r.Min.Y)} +} + +// Dx returns the width of r. +func (r RectangleF32) Dx() float32 { + return r.Max.X - r.Min.X +} + +// Dy returns the height of r. +func (r RectangleF32) Dy() float32 { + return r.Max.Y - r.Min.Y +} + // Inset returns the rectangle r inset by n. The resulting rectangle will never have // a negative size. func (r RectangleF32) Inset(n float32) RectangleF32 { @@ -45,21 +60,6 @@ func (r RectangleF32) Inset(n float32) RectangleF32 { return r } -// Dx returns the width of r. -func (r RectangleF32) Dx() float32 { - return r.Max.X - r.Min.X -} - -// Dy returns the height of r. -func (r RectangleF32) Dy() float32 { - return r.Max.Y - r.Min.Y -} - -// Center returns the center of the rectangle. -func (r RectangleF32) Center() PointF32 { - return PointF32{r.Min.X + .5*(r.Max.X-r.Min.X), r.Min.Y + .5*(r.Max.Y-r.Min.Y)} -} - // Size returns the size of the rectangle. func (r RectangleF32) Size() PointF32 { return PointF32{r.Max.X - r.Min.X, r.Max.Y - r.Min.Y}