github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/web/render/matrix.go (about)

     1  package render
     2  
     3  import "math"
     4  
     5  type Matrix struct {
     6  	XX, YX, XY, YY, X0, Y0 float64
     7  }
     8  
     9  func Identity() Matrix {
    10  	return Matrix{
    11  		1, 0,
    12  		0, 1,
    13  		0, 0,
    14  	}
    15  }
    16  
    17  func Translate(x, y float64) Matrix {
    18  	return Matrix{
    19  		1, 0,
    20  		0, 1,
    21  		x, y,
    22  	}
    23  }
    24  
    25  func Scale(x, y float64) Matrix {
    26  	return Matrix{
    27  		x, 0,
    28  		0, y,
    29  		0, 0,
    30  	}
    31  }
    32  
    33  func Rotate(angle float64) Matrix {
    34  	c := math.Cos(angle)
    35  	s := math.Sin(angle)
    36  	return Matrix{
    37  		c, s,
    38  		-s, c,
    39  		0, 0,
    40  	}
    41  }
    42  
    43  func Shear(x, y float64) Matrix {
    44  	return Matrix{
    45  		1, y,
    46  		x, 1,
    47  		0, 0,
    48  	}
    49  }
    50  
    51  func (a Matrix) Multiply(b Matrix) Matrix {
    52  	return Matrix{
    53  		a.XX*b.XX + a.YX*b.XY,
    54  		a.XX*b.YX + a.YX*b.YY,
    55  		a.XY*b.XX + a.YY*b.XY,
    56  		a.XY*b.YX + a.YY*b.YY,
    57  		a.X0*b.XX + a.Y0*b.XY + b.X0,
    58  		a.X0*b.YX + a.Y0*b.YY + b.Y0,
    59  	}
    60  }
    61  
    62  func (a Matrix) TransformVector(x, y float64) (tx, ty float64) {
    63  	tx = a.XX*x + a.XY*y
    64  	ty = a.YX*x + a.YY*y
    65  	return
    66  }
    67  
    68  func (a Matrix) TransformPoint(x, y float64) (tx, ty float64) {
    69  	tx = a.XX*x + a.XY*y + a.X0
    70  	ty = a.YX*x + a.YY*y + a.Y0
    71  	return
    72  }
    73  
    74  func (a Matrix) Translate(x, y float64) Matrix {
    75  	return Translate(x, y).Multiply(a)
    76  }
    77  
    78  func (a Matrix) Scale(x, y float64) Matrix {
    79  	return Scale(x, y).Multiply(a)
    80  }
    81  
    82  func (a Matrix) Rotate(angle float64) Matrix {
    83  	return Rotate(angle).Multiply(a)
    84  }
    85  
    86  func (a Matrix) Shear(x, y float64) Matrix {
    87  	return Shear(x, y).Multiply(a)
    88  }