Skip to content

Instantly share code, notes, and snippets.

View treeform's full-sized avatar

Andre von Houck treeform

View GitHub Profile
@treeform
treeform / metal.m
Created October 2, 2021 05:54
glfw metal example
#define GLFW_INCLUDE_NONE
#import <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_COCOA
#import <GLFW/glfw3native.h>
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>
#import <simd/simd.h>
#include <assert.h>
#include <stdlib.h>
## Based on the work of https://github.com/edin/raytracer
## MIT License
## Copyright (c) 2021 Edin Omeragic
import benchy, chroma, math, times, glm
from pixie import Image, newImage, writeFile, setRgbaUnsafe
type Vec3 = glm.Vec3[float32]
{.push inline, noinit, checks: off.}
@treeform
treeform / parsehttp.nim
Created March 2, 2021 07:50
Some code to do http chunked encoding.
proc fetchStr(req: Request): (string, seq[string]) =
## Fetches a URL and returns header and chunks.
var socket = newSocket()
if req.url.scheme == "https":
var ctx =
try:
let caCertPath = getAppDir() / "cacert.pem"
@treeform
treeform / binomial.nim
Created November 2, 2020 16:38
How to compute Binomial Coefficients in Nim (Useful as a kind of integer version of Gaussian distribution)
import math
# See https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
# for details of this function
func binomialCoeff(n, k: int): int =
var res = 1
var k = k
if (k > n - k):
k = n - k
for i in 0 ..< k:
import fidget, vmath
proc drawMain() =
... exported code here ....
windowFrame = vec2(620, 140)
startFidget(drawMain)
@treeform
treeform / build.yaml
Created July 11, 2020 16:19
windows github actions build.yaml for nim
name: Run tests
on: [push, pull_request]
jobs:
build:
runs-on: windows-2019
steps:
- name: "install choosenim"
id: cache-choosenim
run: curl -OL https://github.com/dom96/choosenim/releases/download/v0.6.0/choosenim-0.6.0_windows_amd64_debug.exe
# nim c -r --threads:on --gc:orc
import cpuinfo, os, random, locks, deques
type
WorkReq = ref object
id: int
WorkRes = ref object
id: int
import math
func genCache(): array[100, int] =
for i in 0 .. 99:
let
a = i mod 10
b = (i div 10) mod 10
result[i] = int(a ^ a) + int(b ^ b)
const
import math
func genCache(): array[10, int32] =
for i in 1 .. 9:
result[i] = int32(i ^ i)
const
MAX = 440_000_000.int32
cache = genCache()
import fidget, flippy, chroma
var f = newImage(400, 400, 4)
for x in 0 ..< 400:
for y in 0 ..< 400:
f.putRgba(x, y, hsl(x.float/400*360, y.float/400*100, 75).color.rgba)
f.save("data/generated.png")
setTitle("Image viewer")
proc drawMain() =