Skip to content

Instantly share code, notes, and snippets.

View aolo2's full-sized avatar

Алексей Олохтонов aolo2

View GitHub Profile
@aolo2
aolo2 / zoom.js
Last active January 17, 2024 10:48
zoom around the mouse
// What we want is the mouse position to stay the same relative to the zoomable object (it's a map in this example)
// To do that we solve the equation "oldMouseRelativePos = newMouseRelativePos" to find the value of newMapOffset.
// We know the values of oldZoom, newZoom, oldMapOffset and mousePos
//
// It is assumed that canvas->screen (something you would use in OpenGL, for example) transform is "(p - offset) / zoom",
// meaning screen->canvas (something you would use in Canvas2D) transform is "p / zoom - offset".
// mousePos is assumed to be in some kind of screen coordinates
// mapZoom is assumed to be zooming relative to the top left corner of the map
//
// "map relative mouse position" is:
#!/bin/bash
sudo tc qdisc del dev lo root netem
sudo ifconfig lo mtu 65535
#!/bin/bash
sudo tc qdisc add dev lo root netem loss 10% delay 150ms 100ms distribution normal # tune these numbers to make it less or more terrible
sudo ifconfig lo mtu 1500 # like on the internet
#version 300 es
precision highp float;
uniform sampler2D u_texture_points;
uniform highp usampler2D u_texture_indices;
in vec2 v_texcoord;
in vec3 v_color;
flat in float v_thickness;
@aolo2
aolo2 / points_processing.js
Created November 23, 2022 10:21
Example of simple 2 step processing you can run on raw points in a drawing program
function rdp_find_max(points, start, end) {
const EPS = 0.5;
let result = -1;
let max_dist = 0;
const a = points[start];
const b = points[end];
const dx = b.x - a.x;
const dy = b.y - a.y;
@aolo2
aolo2 / rects.c
Created October 22, 2022 11:27
Collision bench
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
struct rect {
float x, y;
float w, h;
@aolo2
aolo2 / reserve_commit.c
Last active July 20, 2022 07:45
Stable growable array based on virtual memory (linux)
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
struct bc_vm {
uint8_t *base;
uint64_t size;
uint64_t commited;
};
// compile with
// gcc -O2 001_ok_cow_initial.c -o 001_ok_cow_initial -pthread -Wall -Wextra
#include <stdint.h> // uint8_t
#include <string.h> // memcmp
#include <pthread.h> // pthread_create, pthread_join, pthread_mutex_lock, pthread_mutex_unlock
#include <stdbool.h> // bool, true, false
#define xylen 1024
static void
render_line(struct v2 from, struct v2 to, u32 *pixels, int width, int height, u32 color)
{
if (from.x >= width) {
from.x = width - 1;
}
if (to.x >= width) {
to.x = width - 1;
}
@aolo2
aolo2 / whatevs.cpp
Created August 12, 2021 20:07
4coder code index usability patch: function prototypes as separate note kind & full function signature in lister
//////////////////////////////////////
// 4coder_code_index.h
//////////////////////////////////////
/* There are new fields in these two enums/structures */
typedef i64 Code_Index_Note_Kind;
enum{
CodeIndexNote_Type,
CodeIndexNote_Function,
CodeIndexNote_Macro,