Skip to content

Instantly share code, notes, and snippets.

View coolbutuseless's full-sized avatar

mikefc coolbutuseless

View GitHub Profile
@coolbutuseless
coolbutuseless / chess-game.R
Created November 5, 2020 20:33
Chess game in R using the stockfish engine
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Convert a FEN string to a char matrix representing the chess board.
#'
#' @param fen fen string
#'
#' @return 8x8 character matrix representing the chess board
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fen_to_matrix <- function(fen) {
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@coolbutuseless
coolbutuseless / RTriangle-bug.R
Created October 4, 2020 22:31
RTriangle bug reprex
raw_vec <- as.raw(
c(0x42, 0x0a, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04,
0x00, 0x00, 0x05, 0x03, 0x00, 0x05, 0x00, 0x00, 0x00, 0x55, 0x54,
0x46, 0x2d, 0x38, 0x13, 0x03, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
0x0e, 0x02, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x46, 0x17, 0x5d,
0x74, 0xd1, 0x45, 0xa7, 0x3f, 0x8b, 0x2e, 0xba, 0xe8, 0xa2, 0x8b,
0xee, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x3f, 0x74,
0xd1, 0x45, 0x17, 0x5d, 0x74, 0xd1, 0x3f, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xe0, 0x3f, 0x46, 0x17, 0x5d, 0x74, 0xd1, 0x45, 0xe7,
@coolbutuseless
coolbutuseless / RTriangle-segfault.R
Created October 1, 2020 10:26
Input that will segfault RTriangle
S <- structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L,
78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L,
91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L,
103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L,
114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L,
@coolbutuseless
coolbutuseless / self-intersecting-polygon-triangulation.R
Last active September 13, 2020 07:10
Triangulation of self-intersecting polyons
library(ggplot2)
df <- data.frame(
x = c(0, 1, 0, 1),
y = c(0, 0, 1, 1)
)
p <- ggplot(df, aes(x, y)) +
geom_polygon() +
geom_point(colour = 'tomato', size = 3) +
@coolbutuseless
coolbutuseless / zstd-quick-bench.R
Created September 10, 2020 22:09
zstd quick bench
library(zstdlite)
set.seed(1)
bytes <- sample(as.raw(1:30), 1e6, replace = TRUE)
cz <- zstdlite::zstd_compress(bytes)
cb_gzip <- memCompress(bytes, type = 'gzip')
cb_bzip2 <- memCompress(bytes, type = 'bzip2')
@coolbutuseless
coolbutuseless / user-callbacks.R
Created September 8, 2020 04:32
mouse callbacks in rgl
library(rgl)
pan3d <- function(button, dev = rgl.cur(), subscene = currentSubscene3d(dev)) {
start <- list()
begin <- function(x, y) {
activeSubscene <- par3d("activeSubscene", dev = dev)
start$listeners <<- par3d("listeners", dev = dev, subscene = activeSubscene)
for (sub in start$listeners) {
@coolbutuseless
coolbutuseless / ceramic.R
Last active June 6, 2020 04:09
ceramic test
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Define the extents of the image
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roi <- raster::extent(110, 160, -45, -12)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Grab the satellite image and the elevation map
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
im <- ceramic::cc_location(roi)
el <- ceramic::cc_elevation(roi)
require(ggplot2)
ggplot() + annotate(x = -10, y=1, xend = -1.38, yend=3, geom='segment', col='white') +
geom_segment(data = data.frame(x = -1.38, y = seq(3.0, 3.0, len=30),
xend = seq(1.4, 1.1, len=30), yend = seq(3.0, 3.5, len=30)),
aes(x, y, xend=xend, yend=yend), col='#ffffff10') +
geom_segment(data = data.frame(id=1:30, x = seq(1.1, 1.35, len=30), y = seq(3.5, 3.0, len=30),
xend = seq(10, 10, len=30), yend = seq(2.0, 1.0, len=30)),
aes(x, y, xend = xend, yend = yend, col = factor(id))) +
geom_path(data=NULL, aes(c(-3, 0, 3, -3), y = c(0,5.5,0,0)), col='white') +
@coolbutuseless
coolbutuseless / reinstall-CRAN.R
Created May 30, 2020 01:05 — forked from tjmahr/reinstall-CRAN.R
maybe reinstall the github packages?
df <- as.data.frame(installed.packages(fields = c("RemoteType", "Repository")))
to_reinstall <- df[df$Repository %in% "CRAN", "Package"]
install.packages(to_reinstall)
@coolbutuseless
coolbutuseless / as.mesh3d.terrainmeshr.R
Last active May 29, 2020 06:58
terrainmesh to mesh3d with normals
library(terrainmeshr)
library(rgl)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create triangles from heigh-map
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tris <- terrainmeshr::triangulate_matrix(
volcano, maxError = 30, verbose = TRUE, y_up = FALSE
)