Calculator/Project/Scripts/MathScript.gd
2025-04-15 11:51:57 -07:00

234 lines
8.5 KiB
GDScript

extends Control
class_name Math
## The Class is to contain everything relating to math.
##
## The goal of this class is to contain everything that there is to math,
## weather that be shape functions or that be other things
## Math constants in one class
##
## The math constants that are all around the math world, all in here!
## I just found a list of constants and put them in here.
class MathConstants:
const pi = 3.1415926535
const e = 2.7182818284
const δ = 4.6692016091
const FeigenbaumAlpha = 2.5029078750
const AperysConstant = 1.2020569031
const GoldenRatio = 1.6180339887
const EulerMascheroniConstant = 0.5772156649
const KhinchinsConstant = 2.6854520010
const GaussKuzminWirsingConstant = 0.3036630028
const BernsteinsConstant = 0.2801694990
const HafnerSarnakMcCurleyConstant = 0.3532363718
const MeisselMertensConstant = 0.2614972128
const GlaisherKinkelinConstnat = 1.2824271291
const Omega = 0.5671432904
const GolombDickmanConstant = 0.6243299885
const CahensConstant = 0.6434105462
const TwinPrime = 0.6601618158
const LaplaceLimit = 0.6627434193
const LandauRamanujanConstant = 0.7642236535
const CatalansConstant = 0.9159655941
const ViswanathsConstant = 1.13198824
const ConwaysConstant = 1.3035772690
const MillsConstant = 1.3063778838
const PlasticConstant = 1.3247179572
const RamanujanSoldnerConstant = 1.4513692348
const BackhouseConstant = 1.4560749485
const PortersConstant = 1.4670780794
const LiebsSquareIceConstant = 1.5396007178
const ErdosBorweinConstant = 1.6066951524
const NivensConstant = 1.7052111401
const UniversalParabolicConstant = 2.2955871493
const SierpinskisConstant = 2.5849817595
const FransenRobinsonConstant = 2.8077702420
const LevysConstant = 3.2758229187
const ReciprocalFibonacciConstant = 3.3598856662
const Googol = pow(10, 100)
const Googolplex = pow(10, Googol)
## Shape functions
##
## This bad boy has all of them shape functions!
class MathShapes:
## This here enum is used to make angle degrees easy.
enum AngleType {
RAD = 0, ## Short for Radians
DEG = 1, ## Short for Degrees
}
## This here enum is used to see what type of length the user is using.
enum CircleLengthType {
Radius = 0, ## it's just that.
Diameter = 1, ## it's radius but two times bigger.
}
# The next function are about spheres
## Get the surface area of a sphere (similar to a parameter of a circle)
static func SurfaceAreaOfSphere(Length: float, LenType: CircleLengthType = MathShapes.CircleLengthType.Radius):
if LenType == MathShapes.CircleLengthType.Radius:
return 4 * (MathConstants.pi * pow(Length, 2))
else:
return MathConstants.pi * pow(Length, 2)
## Get the volume of a sphere (similar to getting area of a circle)
static func VolumeOfSphere(Length: float, LenType: CircleLengthType = MathShapes.CircleLengthType.Radius):
if LenType == MathShapes.CircleLengthType.Radius:
return MathConstants.pi * pow(Length, 3) * 4 / 3
else:
return MathConstants.pi * pow(Length, 3) * 1 / 6
# The next functions are about circles
## Get the area of a circle using its radius.
static func AreaOfCircle(Length: float, LenType: CircleLengthType = MathShapes.CircleLengthType.Radius):
if LenType == MathShapes.CircleLengthType.Radius:
return MathConstants.pi * pow(Length, 2)
else:
return (MathConstants.pi * pow(Length, 2)) / 4
# The next functions are about squares
## Get the area of a square using its side.
static func AreaOfSquare(side: float):
return side * side
# The next functions are about rectangles
## get the area of a rectangle using its two sides
static func AreaOfRectangle(width: float, height: float):
return width * height
# The next functions are about triangles
## Get the area of a right triangle.
static func AreaOfRightTriangle(leg1: float, leg2 : float):
return (leg1 * leg2) / 2
## Get the leg of a right triangle
static func LegOfRightTriangle(leg1: float, area: float):
return 2 * (leg1 / area)
## Get the area of a triangle
static func AreaOfTriangle(base: float, height: float):
return (base * height) / 2
## Get the area of a triangle using side angle side
static func AreaOfTriangleUsingSAS(a: float, b: float, degrees: float, DegOrRad: AngleType = MathShapes.AngleType.DEG):
if DegOrRad == MathShapes.AngleType.DEG:
return a * b * sin(deg_to_rad(degrees)) / 2
else:
return a * b * sin(degrees) / 2
## Get the area of a triangle using side side side
static func AreaOfTriangleUsingSSS(a: float, b: float, c: float):
return sqrt((a + b + c) * (-a + b + c) * (a - b + c) * (a + b - c)) / 4
## Get the degrees of a triangle using law of sines
static func DegreeOfTriangleUsingLawOfSines(a: float, b: float, degreesB: float, DegOrRad: AngleType = MathShapes.AngleType.DEG):
if DegOrRad == MathShapes.AngleType.DEG:
return rad_to_deg(asin(sin(deg_to_rad(degreesB)) * a / b))
else:
return asin(sin(degreesB) * a / b)
## Get the leg of a triangle using law of sines
static func LegOfTriangleUsingLawOfSines(a: float, degreesA: float, degreesB: float, DegOrRad: AngleType = MathShapes.AngleType.DEG):
if DegOrRad == MathShapes.AngleType.DEG:
return a * (sin(deg_to_rad(degreesB)) / sin(deg_to_rad(degreesA)))
else:
return a * (sin(degreesB) / sin(degreesA))
## Get the leg length of a triangle using law of cosines.
static func LegOfTriangleUsingLawOfCosines(a: float, b: float, degrees: float, DegOrRad: AngleType = MathShapes.AngleType.DEG):
if DegOrRad == MathShapes.AngleType.DEG:
return sqrt(pow(a, 2) + pow(b, 2) - (2 * a * b * cos(deg_to_rad(degrees))))
else:
return sqrt(pow(a, 2) + pow(b, 2) - (2 * a * b * cos(degrees)))
## Get the degree of a triangle using the law of cosines (finding one angle with all the sides)
static func DegreeOfTriangleUsingLawOfCosines(a: float, b: float, c: float, DegOrRad: AngleType = MathShapes.AngleType.DEG):
if DegOrRad == MathShapes.AngleType.DEG:
return rad_to_deg(acos((pow(a, 2) + pow(b, 2) - pow(c, 2)) / (2 * a * b)))
else:
return acos((pow(a, 2) + pow(b, 2) - pow(c, 2)) / (2 * a * b))
## Matrix functions
##
## It has some functions for Math Matricies.
class Matrix:
## Adds two Matricies together [br]
## returns null if matricies aren't equal in size or it isn't a 2d array
static func AddMatricies(Matrix1: Array, Matrix2: Array):
if Matrix1[0][0] == null || Matrix2[0][0] == null || Matrix1.size() != Matrix2.size() || Matrix1[0].size() != Matrix2[0].size():
push_warning("Something messed up. returning null...")
return null
var i = 0
var Result: Array = []
while i != Matrix1.size():
Result.append(Array())
var j = 0
while j != Matrix1[0].size():
Result[i].append(Matrix1[i][j] + Matrix2[i][j])
j += 1
i += 1
return Result
## Subtracts two Matricies together [br]
## returns null if matricies aren't equal in size or it isn't a 2d array
static func SubtractMatricies(Matrix1: Array, Matrix2: Array):
if Matrix1[0][0] == null || Matrix2[0][0] == null || Matrix1.size() != Matrix2.size() || Matrix1[0].size() != Matrix2[0].size():
push_warning("Something messed up. returning null...")
return null
var i = 0
var Result: Array = []
while i != Matrix1.size():
Result.append(Array())
var j = 0
while j != Matrix1[0].size():
Result[i].append(Matrix1[i][j] - Matrix2[i][j])
j += 1
i += 1
return Result
## Multiplies a Matrix by a constant [br]
## returns null if the matrix isn't a 2d array
static func MultiplyMatrix(Matrix1: Array, Constant: float):
if Matrix1[0][0] == null:
push_warning("Something messed up. returning null...")
return null
var i = 0
var Result: Array = []
while i != Matrix1.size():
Result.append(Array())
var j = 0
while j != Matrix1[0].size():
Result[i].append(Matrix1[i][j] * Constant)
j += 1
i += 1
return Result
## Random functions
##
## If there is no other class to put the functions in, it will most likely end up here.
class Functions:
## Gets the factorial of a whole number (decimals are complex)
static func FactorialWhole(number: int):
if number >= 1000:
push_warning("Like fucking hell you are going to need a number this big. returning null...")
return null
if number != 1:
return number * FactorialWhole(number - 1)
else:
return 1
## Gets the Arithmetic Sequence's nth term
static func ArithmeticSequence(NthTerm: float, FirstTerm: float, CommonDif: float):
return FirstTerm + ((NthTerm - 1) * CommonDif)
## Gets the Geometric Sequence's nth term
static func GeometricSequence(NthTerm: float, FirstTerm: float, CommonRatio: float):
return FirstTerm * pow(CommonRatio, NthTerm - 1)