Skip to content

Instantly share code, notes, and snippets.

@badcc
badcc / ThreeVecAng.lua
Created August 10, 2016 20:20
Three different ways to calculate angle between two vectors.
local acos = math.acos
local asin = math.asin
local sqrt = math.sqrt
-- Define unit vectors
local mx = sqrt(1*1+2*2+3*3)
local x,y,z = 1/mx,2/mx,3/mx
local ma = sqrt(4*4+5*5+6*6)
local a,b,c = 4/ma,5/ma,6/ma
-- Dot product
print(acos(x*a+y*b+z*c))
@badcc
badcc / optcalc.lua
Created March 6, 2016 17:55
Optimal respective add, mul, div (a, b, c) to reach target number
-- Given the target number n
-- Starting with accumulator at 0
-- what is the optimal number of times
-- you can add a, multiply by b, or divide by
-- c to reach the target number?
-- Output is in Java, but it can be any language that
-- supports integer division.
-- (or use // in Lua 5.3 <3)
local HttpService = game:GetService('HttpService')
local apid = HttpService:JSONDecode(HttpService:GetAsync('http://anaminus.github.io/rbx/json/api/latest.json'))
local function CoreDump(Reference, ClassName)
ClassName = ClassName or Reference.ClassName
print('Dump: ' .. ClassName)
local Table,LongestKey = {}, 0
for _,Entry in next,apid do
if (Entry.type == 'Property' and Entry.Class == ClassName) then
local Skip = false
@badcc
badcc / regularexpressions.c
Created September 5, 2015 16:34
Depends on my personal library (currently private).
#include <stdio.h>
#define AB_WIN
#include "ab.h"
void MatchString(char *Match, const char *String, char *Result) {
int StringIndex = 0;
int MatchLength = 0;
int ResultLength = 0;
bool Class = false;
bool JustRestart = false;
// Derived from
// http://burtleburtle.net/bob/rand/smallprng.html
// TODO(alex): SSE intrinsic
#if !defined(AB_RAND_H)
#define AB_RAND_H
#define ULONG_MAXF 4294967295.0 // 2^32-1
// Internal affixation, _
// 32 bit (u4, ulong)
#define _ab_rand_rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
local Message = "This is a very powerful function and can be used in multiple ways. Used simply it can replace all instances of the pattern provided with the replacement. A pair of values is returned, the modified string and the number of substitutions made. The optional fourth argument n can be used to limit the number of substitutions made:"
local Methods = {
{ "%s+", " " },
{ "are", "r" },
{ "be", "b" },
{ "you", "u" },
{ "i%sam", "i'm" },
{ "first", "1st" },
{ "second", "2nd" },
@badcc
badcc / IKSolve.lua
Last active December 30, 2017 20:53
-- Localized variables that I didn't copy over go here.
local function Solve(r0, r1, c, p)
local x,y,z,r00,r01,r02,r10,r11,r12,r20,r21,r22 = c:components()
local px,py,pz = p.x-x,p.y-y,p.z-z
local tx = r00*px+r10*py+r20*pz
local ty = r01*px+r11*py+r21*pz
local tz = r02*px+r12*py+r22*pz
local d = (tx*tx+ty*ty+tz*tz)^0.5
d = r0+r1<d and r0+r1 or d
local Tests = {
'<string name="Name">TEST</string>', -- pass
'<>TEST</string>', -- load
'< >TEST</string>', -- load
'<a a>TEST</string>', -- fail = not found
'<string a=" name="Name">TEST</string>', -- fail = not found
'<string a= a=""="">TEST</string>', -- ??? load
var EXEC_FILE_LOCATION = process.argv[2]
var LUA_BINARY_NAME = process.argv[3] ? process.argv[3] : 'lua'
var chokidar = require('chokidar');
var fs = require('fs');
var http = require('http');
var exec = require('child_process').exec
var responses = []
@badcc
badcc / mulall.s
Created July 6, 2015 04:47
Multiply Passed Arguments Together (x86, GAS)
.intel_syntax noprefix
.global main
.format:
.asciz "%d\n"
main:
xor rbx, rbx
inc rbx
add rsi, 8
dec rdi