This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bool rayVsCircle(float& depth, | |
| glm::vec2 rayOrig, glm::vec2 rayDir, | |
| glm::vec2 circlePos, float circleRad) | |
| { | |
| const float R = circleRad; | |
| const glm::vec2 op = circlePos - rayOrig; | |
| if(dot(op, op) < R*R) { // is rayOrigin inside the circle ? | |
| depth = 0; | |
| return true; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <glm/glm.hpp> | |
| #include <glm/gtc/constants.hpp> | |
| constexpr float PI = glm::pi<float>(); | |
| using glm::vec3; | |
| /*static void generateIcosahedronVerts(vec3 verts[12]) | |
| { | |
| using glm::sqrt; | |
| using glm::mat2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const std = @import("std"); | |
| const warn = std.debug.warn; | |
| const fmt = std.fmt; | |
| const c = @cImport({ | |
| @cInclude("SDL2/SDL.h"); | |
| }); | |
| const assert = @import("std").debug.assert; | |
| const SDL_WINDOWPOS_UNDEFINED = @bitCast(c_int, c.SDL_WINDOWPOS_UNDEFINED_MASK); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // compile in ubuntu: | |
| // $ zig build-exe paint.zig --library SDL2 --library SDL2main --library c -isystem "/usr/include" --library-path "/usr/lib/x86_64-linux-gnu" | |
| const std = @import("std"); | |
| const warn = std.debug.warn; | |
| const fmt = std.fmt; | |
| const c = @cImport({ | |
| @cInclude("SDL2/SDL.h"); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| When I was looking at the disassembly of my compiler, I noticed that it decided to, instead of | |
| dividing by 3, multiply by 2863311531. | |
| At first I was puzzled by this, but it wasn't as complicated as I thought. | |
| So it turns out that an integer division can be decomposed in two operatations: | |
| 1) A bit shift: that's just deviding by a power of 2 | |
| 2) A multiplication that overflows | |
| The compiler dicides to do this because division is an expensive operation for CPUs | |
| So here I wrote some code that computes the right pair of values for the given divider | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <variant> | |
| #include <string> | |
| #include <vector> | |
| #include <exception> | |
| #include <functional> | |
| #include <iostream> | |
| using namespace std; | |
| void numToStringNum(variant<int, string>& x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <algorithm> | |
| #include <numeric> | |
| #include <chrono> | |
| using namespace std; | |
| template <typename T> | |
| void vecSwap(vector<T>& v, int i1, int i2, int n) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| // there are 3 sticks: 0, 1, 2 | |
| void move(int x, int from, int to) | |
| { | |
| if(x == 1) printf("%d->%d\n", from, to); | |
| else | |
| { | |
| int other = 3-from-to; | |
| move(x-1, from, other); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <CppUTest/CommandLineTestRunner.h> | |
| #include <CppUTest/TestHarness.h> | |
| #include <numeric> | |
| using namespace std; | |
| // (x^p)%n | |
| unsigned powMod(unsigned x, unsigned p, unsigned n) | |
| { |