Debian Codegraph @

vendor/github.com/xyproto/gfx/math.go

raw
1 package gfx
2
3 import "math"
4
5 // Mathematical constants.
6 const (
7 Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796
8 )
9
10 // MathMin returns the smaller of x or y.
11 func MathMin(x, y float64) float64 {
12 return math.Min(x, y)
13 }
14
15 // MathMax returns the larger of x or y.
16 func MathMax(x, y float64) float64 {
17 return math.Max(x, y)
18 }
19
20 // MathAbs returns the absolute value of x.
21 func MathAbs(x float64) float64 {
22 return math.Abs(x)
23 }
24
25 // MathSqrt returns the square root of x.
26 func MathSqrt(x float64) float64 {
27 return math.Sqrt(x)
28 }
29
30 // MathSin returns the sine of the radian argument x.
31 func MathSin(x float64) float64 {
32 return math.Sin(x)
33 }
34
35 // MathSinh returns the hyperbolic sine of x.
36 func MathSinh(x float64) float64 {
37 return math.Sinh(x)
38 }
39
40 // MathCos returns the cosine of the radian argument x.
41 func MathCos(x float64) float64 {
42 return math.Cos(x)
43 }
44
45 // MathCosh returns the hyperbolic cosine of x.
46 func MathCosh(x float64) float64 {
47 return math.Cosh(x)
48 }
49
50 // MathAtan returns the arctangent, in radians, of x.
51 func MathAtan(x float64) float64 {
52 return math.Atan(x)
53 }
54
55 // MathTan returns the tangent of the radian argument x.
56 func MathTan(x float64) float64 {
57 return math.Tan(x)
58 }
59
60 // MathCeil returns the least integer value greater than or equal to x.
61 func MathCeil(x float64) float64 {
62 return math.Ceil(x)
63 }
64
65 // MathFloor returns the greatest integer value less than or equal to x.
66 func MathFloor(x float64) float64 {
67 return math.Floor(x)
68 }
69
70 // MathHypot returns Sqrt(p*p + q*q), taking care to avoid unnecessary overflow and underflow.
71 func MathHypot(p, q float64) float64 {
72 return math.Hypot(p, q)
73 }
74
75 // MathPow returns x**y, the base-x exponential of y.
76 func MathPow(x, y float64) float64 {
77 return math.Pow(x, y)
78 }
79
80 // MathLog returns the natural logarithm of x.
81 func MathLog(x float64) float64 {
82 return math.Log(x)
83 }
84
85 // MathRound returns the nearest integer, rounding half away from zero.
86 func MathRound(x float64) float64 {
87 return math.Round(x)
88 }
89
90 // Sign returns -1 for values < 0, 0 for 0, and 1 for values > 0.
91 func Sign(x float64) float64 {
92 switch {
93 case x < 0:
94 return -1
95 case x > 0:
96 return 1
97 default:
98 return 0
99 }
100 }
101
102 // Clamp returns x clamped to the interval [min, max].
103 //
104 // If x is less than min, min is returned. If x is more than max, max is returned. Otherwise, x is
105 // returned.
106 func Clamp(x, min, max float64) float64 {
107 if x < min {
108 return min
109 }
110 if x > max {
111 return max
112 }
113 return x
114 }
115
116 // Lerp does linear interpolation between two values.
117 func Lerp(a, b, t float64) float64 {
118 return a + (b-a)*t
119 }