Skip to content

Instantly share code, notes, and snippets.

View arce's full-sized avatar

Armando Arce arce

  • Tecnológico de Costa Rica
  • Cartago, Costa Rica
View GitHub Profile
@arce
arce / search.lua
Last active November 16, 2021 21:04
Search Algorithms
function seqSearch(x,A,n)
local i=0
while (i < n) do
if (x == A[i]) then
return i
end
i = i + 1
end
return -1
end
@arce
arce / p5_Arc.c
Created March 27, 2020 04:32
P5_Arc
static int P5_Arc(lua_State *L) {
const float xc = luaL_checknumber(L, 1);
const float yc = luaL_checknumber(L, 2);
const float radius = luaL_checknumber(L, 3);
const float start = _deg(luaL_checknumber(L, 4));
const float stop = _deg(luaL_checknumber(L, 5));
#ifdef __linux__
const float x1 = xc + radius * STBTT_cos(start);
const float y1 = yc + radius * STBTT_sin(start);
@arce
arce / bezier.h
Created March 27, 2020 02:55
bezier
static float getApproximationFactor(float angleStart, float angleEnd) {
int arc = angleEnd - angleStart;
if (abs(arc) > M_PI) {
arc -= M_PI * 2;
arc /= M_PI * 2;
}
return (4 / 3) * tan(arc / 4);
}
@arce
arce / code.txt
Created March 26, 2020 22:39
Arc by bezier
This is an 8-year-old question, but one that I recently struggled with, so I thought I'd share what I came up with. I spent a lot of time trying to use solution (9) from this text and couldn't get any sensible numbers out of it until I did some Googling and learned that, apparently, there were some typos in the equations. Per the corrections listed in this blog post, given the start and end points of the arc ([x1, y1] and [x4, y4], respectively) and the the center of the circle ([xc, yc]), one can derive the control points for a cubic bezier curve ([x2, y2] and [x3, y3]) as follows:
ax = x1 – xc
ay = y1 – yc
bx = x4 – xc
by = y4 – yc
q1 = ax * ax + ay * ay
q2 = q1 + ax * bx + ay * by
k2 = 4/3 * (√(2 * q1 * q2) – q2) / (ax * by – ay * bx)
@arce
arce / arc.js
Last active March 26, 2020 22:36
Arc by bezier
I stumbled upon this problem recently. I compiled a solution from the articles mentioned here in the form of a module.
It accepts start angle, end angle, center and radius as input.
It approximates small arcs (<= PI/2) pretty well. If you need to approximate something arcs from PI/2 to 2*PI you can always break them in parts < PI/2, calculate the according curves and join them afterward.
This solution is start and end angle order agnostic - it always picks the minor arc.
As a result you get all four points you need to define a cubic bezier curve in absolute coordinates.
@arce
arce / libsahpe.c
Created March 26, 2020 15:49
libshape.c
Pull requests
Issues
Marketplace
Explore
@arce
mgthomas99
/
@arce
arce / Array.lua
Last active February 20, 2020 21:16
Graphics Benchmark
write = io.write
function Array(arg1)
local table = (type(arg1) == "table") and arg1 or {}
local _table = table
table = {}
local metatable = {
__newindex = function(table,k,v)
_table[k+1] = v
end,
@arce
arce / dataVector.lua
Last active December 8, 2019 01:14
dataVector REDIS
//full userdata
extern "C" int newarray(lua_State* L)
{
int n = luaL_checkint(L, 1);
size_t nbytes = sizeof(CharArray) + (n - 1)*sizeof(char);
CharArray* a = (CharArray*)lua_newuserdata(L, nbytes);
a->size = n;
return 1;
}
@arce
arce / OpenGL.c
Last active January 18, 2019 16:16
OpenGL example
/* An example of an OpenGL animation loop using the Win32 API. Also
demonstrates palette management for RGB and color index modes and
general strategies for message handling. */
#include <windows.h> /* must include this before GL/gl.h */
#include <GL/gl.h> /* OpenGL header file */
#include <stdio.h>
HDC hDC; /* device context */
@arce
arce / IMGUI.pde
Last active February 20, 2019 19:23
IMGUI
// https://bl.ocks.org/arce/06627a44f961cfe92ef984fa80ce80a7
void setup() {
size(640,480);
}
void draw() {
background(243);
ui.begin();