Skip to content

Instantly share code, notes, and snippets.

View JoaoBaptMG's full-sized avatar

João Baptista de Paula e Silva JoaoBaptMG

View GitHub Profile
@JoaoBaptMG
JoaoBaptMG / context.h
Last active March 15, 2023 00:11
Simple context switching subroutines for ARMv4 and possibly other platforms
//----------------------------------------------------------------------------
// context.h
//----------------------------------------------------------------------------
// Provides an API for context switching (a primitive form of coroutines)
//----------------------------------------------------------------------------
// Copyright 2023 João Baptista de Paula e Silva
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
template <typename Period2, typename Rep, typename Period>
constexpr auto fractional_count(const std::chrono::duration<Rep, Period>& duration)
{
return std::chrono::duration<double, Period2>(duration).count();
}
template <typename Rep, typename Period>
std::ostream& operator<<(std::ostream& out, const std::chrono::duration<Rep, Period>& duration)
{
namespace ch = std::chrono;
@JoaoBaptMG
JoaoBaptMG / buddy.cpp
Created October 26, 2019 00:38
Buddy Allocator
#include <iostream>
#include <cstdint>
#include <cassert>
#include <vector>
// Definitions in powers of two
constexpr std::size_t MemBlockExp = 6;
// A single memory block to try the algorithm
struct Block
@JoaoBaptMG
JoaoBaptMG / SweptRectangle.cs
Last active October 12, 2019 18:28
Swept rectangle collision with time of impact
//
// Copyright (c) 2019 João Baptista de Paula e Silva.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
@JoaoBaptMG
JoaoBaptMG / RectangleIntersection.cs
Last active June 29, 2019 13:42
Rectangle Intersection
/// <summary>
/// Check if two rotated rectangles intersect, assuming they are rotated relative to their center points
/// </summary>
/// <param name="r1">The first rectangle</param>
/// <param name="r2">The second rectangle</param>
/// <param name="rotation1">The first rectangle's rotation in radians</param>
/// <param name="rotation2">The second rectangle's rotation in radians</param>
/// <returns>Whether the two rectangles intersect</returns>
public static bool RectangleIntersection(Rectangle r1, Rectangle r2, float rotation1, float rotation2)
{