Skip to content

Instantly share code, notes, and snippets.

@tuket
tuket / ray_vs_circle.cpp
Created July 10, 2020 22:09
ray_vs_circle
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;
}
@tuket
tuket / generateIcosahedron.cpp
Last active April 12, 2021 18:28
generate icosahedron
#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;
@tuket
tuket / sdl_main.zig
Created November 12, 2019 20:57
basic SDL game loop in Zig
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);
@tuket
tuket / paint.zig
Created August 13, 2019 09:31
paint in zig using SDL2
// 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");
});
@tuket
tuket / fastDiv.cpp
Created July 20, 2019 09:11
Integer division can be decomposed in a bitshift and a multiplication
/*
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
*/
@tuket
tuket / variant_numbers.cpp
Created July 13, 2018 15:54
variant_numbers.cpp
#include <variant>
#include <string>
#include <vector>
#include <exception>
#include <functional>
#include <iostream>
using namespace std;
void numToStringNum(variant<int, string>& x)
@tuket
tuket / shift_rotate.cpp
Created June 2, 2018 21:24
Shift Rotate
#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) {
@tuket
tuket / 000_INDEX.md
Last active March 22, 2018 22:19
type_traits example
@tuket
tuket / solve_hanoi.cpp
Created January 18, 2018 23:37
hanoi towers
#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);
@tuket
tuket / powmod.cpp
Created November 9, 2017 22:09
compute power modulo n
#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)
{