From e3e3c1fec6ea3317a7aa6b272696892ed563b537 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 27 Mar 2021 20:33:09 +0100 Subject: [PATCH] Math.h and cpp. --- 02_math_osztaly.txt | 37 ++++++---- 02_math_osztaly/.clang-format | 128 ++++++++++++++++++++++++++++++++++ 02_math_osztaly/compile.sh | 15 ++++ 02_math_osztaly/main.cpp | 15 ++++ 02_math_osztaly/math.cpp | 90 ++++++++++++++++++++++++ 02_math_osztaly/math.h | 89 +++++++++++++++++++++++ 6 files changed, 361 insertions(+), 13 deletions(-) create mode 100755 02_math_osztaly/.clang-format create mode 100755 02_math_osztaly/compile.sh create mode 100644 02_math_osztaly/main.cpp create mode 100644 02_math_osztaly/math.cpp create mode 100644 02_math_osztaly/math.h diff --git a/02_math_osztaly.txt b/02_math_osztaly.txt index 7b47c3c..881432f 100644 --- a/02_math_osztaly.txt +++ b/02_math_osztaly.txt @@ -78,13 +78,13 @@ float Math::atan2(float a, float b) { return ::atan2(a, b); } -Akkor a feladat: +Akkor a feladatok: 1. Írd meg az alábbi segédosztályt: (Az includeok: #include , #include , #include , #include ) -Defineok a tetejére: +Defineok a header tetejére: #define MATH_PI 3.1415926535897932384626433833 #define EPSILON 0.00001 @@ -110,8 +110,8 @@ UML: | + acos(x : double) : double { static } | -> return ::acos(x); | + atan(x : float) : float { static } | -> return ::atanf(x); | + atan(x : double) : double { static } | -> return ::atan(x); -| + atan2(x : float) : float { static } | -> return ::atan2f(x); -| + atan2(x : double) : double { static } | -> return ::atan2(x); +| + atan2(x : float, y : float) : float { static } | -> return ::atan2f(x, y); +| + atan2(x : double, y : double) : double { static } | -> return ::atan2(x, y); | | | + sqrt(x : float) : float { static } | -> return ::sqrtf(x); | + sqrt(x : double) : double { static } | -> return ::sqrt(x); @@ -126,30 +126,36 @@ UML: | + log(x : float) : float { static } | -> return ::logf(x); | + log(x : double) : double { static } | -> return ::log(x); | | -| + inv_sqrt(x : float) : float { static } | -> return (float)(1.0 / ::sqrt(x)); -| + fast_inv_sqrt(x : float) : float { static } | -> Implementáció lejjebb +| + inv_sqrt(x : float) : float { static } | -> return (float)(1.0 / ::sqrtf(x)); +| + fast_inv_sqrt(number : float) : float { static } | -> Implementáció lejjebb | | -| + abs(x : float) : float { static } | -> return absd(x); -| + abs(x : double) : double { static } | -> return absf(x); +| + abs(x : float) : float { static } | -> return x > 0 ? x : -x; +| + abs(x : double) : double { static } | -> return x > 0 ? x : -x; | + abs(x : int) : int { static } | -> return x > 0 ? x : -x; | | +| + deg2rad(x : float) : float { static } | -> return x * MATH_PI / 180.0; | + deg2rad(x : double) : double { static } | -> return x * MATH_PI / 180.0; | + deg2rad(x : int) : int { static } | -> return x * MATH_PI / 180.0; +| | +| + rad2deg(x : float) : float { static } | -> return x * 180.0 / MATH_PI; | + rad2deg(x : double) : double { static } | -> return x * 180.0 / MATH_PI; | + rad2deg(x : int) : int { static } | -> return x * 180.0 / MATH_PI; | | -| + is_equal_approx(float a, float b) : bool { static } | -> mint vektorban -| + is_zero_approx(float a) : bool { static } | +| + is_equal_approx(a : float, b : float) : bool { static } | -> mint vektorban +| + is_zero_approx(a : float) : bool { static } | | | -| + seed(unsigned int s) { static } | -> srand(s); +| + seed(s : unsigned int) { static } | -> srand(s); | + randomize() { static } | -> srand(time(NULL)); | | | + rand() : int { static } | -> return rand(); | + randf() : float { static } | -> return rand() / RANDOM_32BIT_MAX; | + randd() : float { static } | -> return rand() / RANDOM_32BIT_MAX; | | -| + rand(float from, float to) : float { static } | -> return randf() * to + from; -| + rand(double from, double to) : double { static } | -> return randd() * to + from; +| + rand(int m) : int { static } | -> return rand() % m; +| | +| + rand(from : int, to : int) : int { static } | -> return (rand() % (to - from)) + from; +| + rand(from : float, to : float) : float { static } | -> return randf() * (to - from) + from; +| + rand(from : double, to : double) : double { static } | -> return randd() * (to - from) + from; |------------------------------------------------------------| fast_inv_sqrt: @@ -190,3 +196,8 @@ Megj: Ezek a random (ranf, randd) függvények nem a leggyorsabbak, nagyon sokat alapszinű /low level/ hardwares ismeretekkel, és matematikával. Akit érdekel érdemes kürölnézni nagyobb projektekben, hogy hogy vannak ilyenek implementálva. + +2. Most, hogy már kész a Math osztályod, menj visza a Vector2, Vector3, és Rect2 osztályokba, +nézd végig a függvényeiket, és cserélj le mindent amit lehet, Math osztálybelire. + + diff --git a/02_math_osztaly/.clang-format b/02_math_osztaly/.clang-format new file mode 100755 index 0000000..cb67d39 --- /dev/null +++ b/02_math_osztaly/.clang-format @@ -0,0 +1,128 @@ +# Commented out parameters are those with the same value as base LLVM style +# We can uncomment them if we want to change their value, or enforce the +# chosen value in case the base style changes (last sync: Clang 6.0.1). +--- +### General config, applies to all languages ### +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: DontAlign +# AlignConsecutiveAssignments: false +# AlignConsecutiveDeclarations: false +# AlignEscapedNewlines: Right +# AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +# AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: true +# AllowShortLoopsOnASingleLine: false +# AlwaysBreakAfterDefinitionReturnType: None +# AlwaysBreakAfterReturnType: None +# AlwaysBreakBeforeMultilineStrings: false +# AlwaysBreakTemplateDeclarations: false +# BinPackArguments: true +# BinPackParameters: true +# BraceWrapping: +# AfterClass: false +# AfterControlStatement: false +# AfterEnum: false +# AfterFunction: false +# AfterNamespace: false +# AfterObjCDeclaration: false +# AfterStruct: false +# AfterUnion: false +# AfterExternBlock: false +# BeforeCatch: false +# BeforeElse: false +# IndentBraces: false +# SplitEmptyFunction: true +# SplitEmptyRecord: true +# SplitEmptyNamespace: true +# BreakBeforeBinaryOperators: None +# BreakBeforeBraces: Attach +# BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: false +# BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: AfterColon +# BreakStringLiterals: true +ColumnLimit: 0 +# CommentPragmas: '^ IWYU pragma:' +# CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +# DerivePointerAlignment: false +# DisableFormat: false +# ExperimentalAutoDetectBinPacking: false +# FixNamespaceComments: true +# ForEachMacros: +# - foreach +# - Q_FOREACH +# - BOOST_FOREACH +# IncludeBlocks: Preserve +IncludeCategories: + - Regex: '".*"' + Priority: 1 + - Regex: '^<.*\.h>' + Priority: 2 + - Regex: '^<.*' + Priority: 3 +# IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: true +# IndentPPDirectives: None +IndentWidth: 4 +# IndentWrappedFunctionNames: false +# JavaScriptQuotes: Leave +# JavaScriptWrapImports: true +# KeepEmptyLinesAtTheStartOfBlocks: true +# MacroBlockBegin: '' +# MacroBlockEnd: '' +# MaxEmptyLinesToKeep: 1 +# NamespaceIndentation: None +# PenaltyBreakAssignment: 2 +# PenaltyBreakBeforeFirstCallParameter: 19 +# PenaltyBreakComment: 300 +# PenaltyBreakFirstLessLess: 120 +# PenaltyBreakString: 1000 +# PenaltyExcessCharacter: 1000000 +# PenaltyReturnTypeOnItsOwnLine: 60 +# PointerAlignment: Right +# RawStringFormats: +# - Delimiter: pb +# Language: TextProto +# BasedOnStyle: google +# ReflowComments: true +# SortIncludes: true +# SortUsingDeclarations: true +# SpaceAfterCStyleCast: false +# SpaceAfterTemplateKeyword: true +# SpaceBeforeAssignmentOperators: true +# SpaceBeforeParens: ControlStatements +# SpaceInEmptyParentheses: false +# SpacesBeforeTrailingComments: 1 +# SpacesInAngles: false +# SpacesInContainerLiterals: true +# SpacesInCStyleCastParentheses: false +# SpacesInParentheses: false +# SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Always +--- +### C++ specific config ### +Language: Cpp +Standard: Cpp03 +--- +### ObjC specific config ### +Language: ObjC +Standard: Cpp03 +ObjCBlockIndentWidth: 4 +# ObjCSpaceAfterProperty: false +# ObjCSpaceBeforeProtocolList: true +--- +### Java specific config ### +Language: Java +# BreakAfterJavaFieldAnnotations: false +JavaImportGroups: ['org.godotengine', 'android', 'androidx', 'com.android', 'com.google', 'java', 'javax'] +... diff --git a/02_math_osztaly/compile.sh b/02_math_osztaly/compile.sh new file mode 100755 index 0000000..2b2b32c --- /dev/null +++ b/02_math_osztaly/compile.sh @@ -0,0 +1,15 @@ + +if [ ! -d "obj" ]; then + mkdir obj +fi + +if [ ! -d "bin" ]; then + mkdir bin +fi + +#-Iinclude + +g++ -Wall -g -c math.cpp -o obj/math.o +g++ -Wall -g -c main.cpp -o obj/main.o +g++ -o bin/program obj/math.o obj/main.o + diff --git a/02_math_osztaly/main.cpp b/02_math_osztaly/main.cpp new file mode 100644 index 0000000..7525b90 --- /dev/null +++ b/02_math_osztaly/main.cpp @@ -0,0 +1,15 @@ + +#include + +#include "math.h" + +int main() { + + std::cout << Math::sin(1.43) << std::endl; + + std::cout << Math::fast_inv_sqrt(1.43) << std::endl; + std::cout << Math::inv_sqrt(1.43) << std::endl; + + std::cout << "asd" << std::endl; + return 0; +} \ No newline at end of file diff --git a/02_math_osztaly/math.cpp b/02_math_osztaly/math.cpp new file mode 100644 index 0000000..38c3885 --- /dev/null +++ b/02_math_osztaly/math.cpp @@ -0,0 +1,90 @@ +#include "math.h" + +float Math::inv_sqrt(const float x) { + return (float)(1.0 / ::sqrtf(x)); +} + +float Math::fast_inv_sqrt(const float number) { + long i; + float x2, y; + const float threehalfs = 1.5F; + + x2 = number * 0.5F; + y = number; + i = *(long *)&y; + i = 0x5f3759df - (i >> 1); + y = *(float *)&i; + y = y * (threehalfs - (x2 * y * y)); + + return y; +} + +float Math::is_equal_approx(const float a, const float b) { + if (a + EPSILON < b && a - EPSILON > b) { + return true; + } + + return false; +} + +float Math::is_zero_approx(const float a) { + if (a + EPSILON < 0 && a - EPSILON > 0) { + return true; + } + + return false; +} + +void Math::seed(const unsigned int s) { + srand(s); +} + +void Math::randomize() { + srand(time(NULL)); +} + +int Math::rand() { + return rand(); +} + +float Math::randf() { + return rand() / RANDOM_32BIT_MAX; +} + +double Math::randd() { + return rand() / RANDOM_32BIT_MAX; +} + +int rand(const int m) { + return rand() % m; +} + +int rand(const int from, const int to) { + return (rand() % (to - from)) + from; +} + +float Math::rand(const float from, const float to) { + return randf() * (to - from) + from; +} + +float Math::rand(const double from, const double to) { + return randd() * (to - from) + from; +} + +/* + + +| + is_equal_approx(float a, float b) : bool { } | -> mint vektorban +| + is_zero_approx(float a) : bool { } | +| | +| + seed(unsigned int s) { } | -> srand(s); +| + randomize() { } | -> srand(time(NULL)); +| | +| + rand() : int { } | -> return rand(); +| + randf() : float { } | -> return rand() / RANDOM_32BIT_MAX; +| + randd() : float { } | -> return rand() / RANDOM_32BIT_MAX; +| | +| + rand(float from, float to) : float { } | -> return randf() * to + from; +| + rand(double from, double to) : double { } | -> return randd() * to + from; +|------------------------------------------------------------| +*/ diff --git a/02_math_osztaly/math.h b/02_math_osztaly/math.h new file mode 100644 index 0000000..cd5fb8f --- /dev/null +++ b/02_math_osztaly/math.h @@ -0,0 +1,89 @@ +#ifndef MATH_H +#define MATH_H + +#include +#include +#include +#include + +#define MATH_PI 3.1415926535897932384626433833 +#define EPSILON 0.00001 + +class Math { +public: + + static const uint64_t RANDOM_32BIT_MAX = 0xFFFFFFFF; + + inline static float sin(const float x) { return ::sinf(x); } + inline static double sin(const double x) { return ::sin(x); } + + inline static float cos(const float x) { return ::cosf(x); } + inline static double cos(const double x) { return ::cos(x); } + + inline static float tan(const float x) { return ::tanf(x); } + inline static double tan(const double x) { return ::tan(x); } + + inline static float asin(const float x) { return ::asinf(x); } + inline static double asin(const double x) { return ::asin(x); } + + inline static float acos(const float x) { return ::acosf(x); } + inline static double acos(const double x) { return ::acos(x); } + + inline static float atan(const float x) { return ::atanf(x); } + inline static double atan(const double x) { return ::atan(x); } + + inline static float atan2(const float x, const float y) { return ::atan2f(x, y); } + inline static double atan2(const double x, const float y) { return ::atan2(x, y); } + + inline static float sqrt(const float x) { return ::sqrtf(x); } + inline static double sqrt(const double x) { return ::sqrt(x); } + + inline static float fmod(const float x, const float y) { return ::fmodf(x, y); } + inline static double fmod(const double x, const float y) { return ::fmod(x, y); } + + inline static float floor(const float x) { return ::floorf(x); } + inline static double floor(const double x) { return ::floor(x); } + + inline static float ceil(const float x) { return ::ceilf(x); } + inline static double ceil(const double x) { return ::ceil(x); } + + inline static float pow(const float x, const float y) { return ::powf(x, y); } + inline static double pow(const double x, const float y) { return ::pow(x, y); } + + inline static float log(const float x) { return ::logf(x); } + inline static double log(const double x) { return ::log(x); } + + static float inv_sqrt(const float x); + static float fast_inv_sqrt(const float x); + + inline static float abs(const float x) { return x > 0 ? x : -x; } + inline static double abs(const double x) { return x > 0 ? x : -x; } + inline static int abs(const int x) { return x > 0 ? x : -x; } + + inline static float deg2rad(const float x) { return x * MATH_PI / 180.0; } + inline static double deg2rad(const double x) { return x * MATH_PI / 180.0; } + inline static int deg2rad(const int x) { return x * MATH_PI / 180.0; } + + inline static float rad2deg(const float x) { return x * 180.0 / MATH_PI; } + inline static double rad2deg(const double x) { return x * 180.0 / MATH_PI; } + inline static int rad2deg(const int x) { return x * 180.0 / MATH_PI; } + + static float is_equal_approx(const float a, const float b); + static float is_zero_approx(const float a); + + static void seed(const unsigned int s); + static void randomize(); + + static int rand(); + static float randf(); + static double randd(); + + static int rand(const int m); + + static int rand(const int from, const int to); + static float rand(const float from, const float to); + static float rand(const double from, const double to); + +}; + +#endif