Skip to content

Instantly share code, notes, and snippets.

View OptimisticPeach's full-sized avatar
💭
Enjoying Rust!

Patrik Buhring OptimisticPeach

💭
Enjoying Rust!
View GitHub Profile
@OptimisticPeach
OptimisticPeach / example.typ
Created October 23, 2023 06:35
Objects in Typst
#import "obj.typ"
// Methods
#let inc(n) = { n + 1 }
#let dec(n) = { n - 1 }
#let mul(n, m) = { n * m }
// Constructor
#let makeint(i) = obj.obj(
i,
(
@OptimisticPeach
OptimisticPeach / rref.typ
Last active October 18, 2023 19:23
RREF Solver and Typesetter For Typst
#let __fract(num, denom) = (num: num, denom: denom)
#let __fract_add(v1, v2) = __fract(v1.num * v2.denom + v2.num * v1.denom, v1.denom * v2.denom)
#let __fract_sub(v1, v2) = __fract_add(v1, __fract(-v2.num, v2.denom))
#let __fract_mul(v1, v2) = __fract(v1.num * v2.num, v1.denom * v2.denom)
#let __fract_div(v1, v2) = __fract(v1.num * v2.denom, v1.denom * v2.num)
#let __fract_simplify(v1) = {
if(v1.num == 0) {
return __fract(0, 1)
}
if(v1.num < 0 and v1.denom < 0) {
@OptimisticPeach
OptimisticPeach / rotation_4d.rs
Created August 23, 2023 02:11
Decompose a 4D orthogonal matrix as a pair of unit quaternions
/// Decomposes a 4D rotation into two isoclinic rotations represented by
/// unit quaternions.
// https://digital.csic.es/bitstream/10261/132980/1/ON-CAYLEYS.pdf
fn factor_cayley(m: Mat4) -> (Quat, Quat) {
let a00 = m.x_axis.x;
let a10 = m.x_axis.y;
let a20 = m.x_axis.z;
let a30 = m.x_axis.w;
let a01 = m.y_axis.x;
let a11 = m.y_axis.y;
@OptimisticPeach
OptimisticPeach / camera.rs
Created June 7, 2022 02:18
Camera controller code.
use bevy::prelude::*;
use crate::{CameraTag, PlanetCentre, Quat, Res, Vec3};
pub struct CameraState {
pub pos_retention: f32,
target_pos: Quat,
pub want_target_pos: Quat,
pub inclination_retention: f32,
inclination: f32,
@OptimisticPeach
OptimisticPeach / explanation.txt
Created January 7, 2021 23:54
How to calculate the number of vertices on the surface of an icosahedron.
// Number of triangles:
N = 20
// Number of edges:
E = 30
// Number of vertices:
V = 12
// Number of points within a triangle (triangular numbers):
inner(s) = (s^2 + s) / 2
// Number of points on an edge:
edges(s) = s
@OptimisticPeach
OptimisticPeach / nodes.rs
Last active July 9, 2019 20:46 — forked from wtholliday/nodes.rs
Exercise in graph data structure with undo/redo.
#[derive(Debug)]
struct Node;
#[derive(Copy, Clone, Debug)]
struct Wire {
from: usize, // node index
to: usize, // node index
}
#[derive(Debug)]
// NOTE: DO NOT USE THIS IN MISSION CRITICAL STUFF, BECAUSE THIS IS RATHER INEFFICIENT
// GRAPH STRUCTURES ARE SLOW ON CPUs, USE THIS AS REFERENCE
macro_rules! ac {
($col:ident, $text:expr) => {{
Colour::apply_colour(Colour::$col, $text.to_string())
}};
}
/// <summary>
/// Spherically interpolates a pair of vectors given a percentage <c>t</c>
/// </summary>
/// <param name="end">The End result once t is equal to 1</param>
/// <param name="t">The percent such that 0 < t < 1 </param>
/// <remarks>
/// Adapted from https://gist.github.com/manveru/384873
/// </remarks>
public static Vector3 Slerp(this Vector3 source, Vector3 end, float t)
{
public static Vector2 Rotate(Vector2 CurrentPoint, Vector2 Origin, float AngleInRadians)
{
Vector2 NewPoint = CurrentPoint - Origin;
float X = CurrentPoint.X * (float)Math.Cos(AngleInRadians) - CurrentPoint.Y * (float)Math.Sin(AngleInRadians);
float Y = CurrentPoint.Y * (float)Math.Cos(AngleInRadians) + CurrentPoint.X * (float)Math.Sin(AngleInRadians);
return new Vector2(X, Y) + Origin;
}