Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/apps/lua/lualilka_geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ int lualilka_geometry_intersectLines(lua_State* L) {
float dx = luaL_checknumber(L, 7);
float dy = luaL_checknumber(L, 8);

// I have no idea what I'm doing - Copilot wrote this for me, lol
// Solve A + t * (B - A) = C + u * (D - C) using 2D cross products: segments intersect when t, u are in [0, 1]
float rx = bx - ax, ry = by - ay; // B - A
float sx = dx - cx, sy = dy - cy; // D - C
float qx = cx - ax, qy = cy - ay; // C - A

float denominator = (by - ay) * (dx - cx) - (bx - ax) * (dy - cy);
float denominator = rx * sy - ry * sx;
if (denominator == 0) {
lua_pushboolean(L, false);
return 1;
}

float t = ((cy - ay) * (dx - cx) - (cx - ax) * (dy - cy)) / denominator;
float u = -((ay - by) * (cx - ax) - (ax - bx) * (cy - ay)) / denominator;
float t = (qx * sy - qy * sx) / denominator;
float u = (qx * ry - qy * rx) / denominator;

if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
lua_pushboolean(L, true);
Expand Down
25 changes: 13 additions & 12 deletions src/apps/lua/lualilka_imageTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static int lualilka_imageTransform_get_matrix(lua_State* L) {
for (int i = 0; i < 2; ++i) {
lua_newtable(L);
for (int j = 0; j < 2; ++j) {
lua_pushinteger(L, (*userdata)->matrix[i][j]);
lua_pushnumber(L, (*userdata)->matrix[i][j]);
lua_rawseti(L, -2, j + 1);
}
lua_rawseti(L, -2, i + 1);
Expand All @@ -77,26 +77,27 @@ static int lualilka_imageTransform_get_matrix(lua_State* L) {

static int lualilka_imageTransform_set_matrix(lua_State* L) {
lilka::Transform** userdata = TRANSFORM_PTR(luaL_checkudata(L, 1, IMAGE_TRANSFORM));
if (!lua_istable(L, 2)) {
return luaL_error(L, "Expected a table as the second argument");
}
luaL_checktype(L, 2, LUA_TTABLE);

// Validate the whole matrix first, so a bad argument leaves the transform untouched
float matrix[2][2];
for (int i = 0; i < 2; ++i) {
lua_rawgeti(L, 2, i + 1);
if (lua_rawgeti(L, 2, i + 1) != LUA_TTABLE) {
return luaL_error(L, "Expected a 2x2 table, row %d is not a table", i + 1);
}
for (int j = 0; j < 2; ++j) {
lua_rawgeti(L, -1, j + 1);

if (!lua_isnumber(L, -1)) {
lua_pop(L, 2);
return luaL_error(L, "Element at (%d, %d) is not a number", i, j);
int isNumber = 0;
matrix[i][j] = lua_tonumberx(L, -1, &isNumber);
if (!isNumber) {
return luaL_error(L, "Element at (%d, %d) is not a number", i + 1, j + 1);
}

(*userdata)->matrix[i][j] = lua_tonumber(L, -1);
lua_pop(L, 1);
}

lua_pop(L, 1);
}

memcpy((*userdata)->matrix, matrix, sizeof(matrix));
return 0;
}

Expand Down
14 changes: 14 additions & 0 deletions src/apps/lua/luarunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,27 @@ void AbstractLuaRunnerApp::luaTeardown() {
lua_close(L);
}

void AbstractLuaRunnerApp::resetCanvasState() {
for (lilka::Canvas* c : {canvas, backCanvas}) {
if (!c) continue;
// Match defaults of a freshly constructed lilka::Canvas
c->setFont(FONT_10x20);
c->setTextBound(0, 0, c->width(), c->height());
c->setTextColor(lilka::colors::White);
c->setTextSize(1);
c->setTextWrap(true);
c->setCursor(0, 0);
}
}

int AbstractLuaRunnerApp::execute() {
// Calls Lua code that's on top of the stack (previously loaded with luaL_loadfile or luaL_loadstring)

int jmpCode = setjmp(stopjmp);

if (jmpCode == 0) {
// Run script
resetCanvasState();
canvas->fillScreen(0);
int retCode = lua_pcall(L, 0, LUA_MULTRET, 0);
if (retCode) {
Expand Down
2 changes: 2 additions & 0 deletions src/apps/lua/luarunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class AbstractLuaRunnerApp : public App {
void luaSetup(const char* dir);
void luaTeardown();
int execute();
// Restore default text state on both canvas buffers, so scripts don't inherit runner UI settings
void resetCanvasState();
lua_State* L;
};

Expand Down
11 changes: 8 additions & 3 deletions src/apps/mjs/mjsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ static void mjs_geometry_intersect_lines(struct mjs* mjs) {
float dx = mjs_get_double(mjs, mjs_arg(mjs, 6));
float dy = mjs_get_double(mjs, mjs_arg(mjs, 7));

float denominator = (by - ay) * (dx - cx) - (bx - ax) * (dy - cy);
// Solve A + t * (B - A) = C + u * (D - C) using 2D cross products: segments intersect when t, u are in [0, 1]
float rx = bx - ax, ry = by - ay; // B - A
float sx = dx - cx, sy = dy - cy; // D - C
float qx = cx - ax, qy = cy - ay; // C - A

float denominator = rx * sy - ry * sx;
if (denominator == 0) {
mjs_return(mjs, mjs_mk_boolean(mjs, 0));
return;
}

float t = ((cy - ay) * (dx - cx) - (cx - ax) * (dy - cy)) / denominator;
float u = -((ay - by) * (cx - ax) - (ax - bx) * (cy - ay)) / denominator;
float t = (qx * sy - qy * sx) / denominator;
float u = (qx * ry - qy * rx) / denominator;

mjs_return(mjs, mjs_mk_boolean(mjs, t >= 0 && t <= 1 && u >= 0 && u <= 1));
}
Expand Down
Loading