Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
pgtwitter / voronoi.py
Last active May 6, 2025 14:56
scipy.spatialのVoronoiにおいて無限遠点を含む領域(ポリゴン)をどうにか描画領域内だけでも覆うようにして着色したい.
# %%
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from sklearn.decomposition import PCA
from scipy.spatial import Voronoi
from scipy.spatial import ConvexHull
import colorsys
@pgtwitter
pgtwitter / lockfile.m
Created May 1, 2025 11:33
/tmp/lockfileを確認し,存在していないもしくは一時間以上前の作成ならロックファイルを作成もしくは更新して正常終了0.ファイルが存在し一時間以内なら有効なロックファイルとして正常終了0.なにかしら問題があれば異常終了1.Grok3作.
// clang -fobjc-arc -framework Foundation lockfile.m -o lockfile
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// ロックファイルのパス
NSString *lockFilePath = @"/tmp/lockfile";
// NSFileManagerのインスタンス
NSFileManager *fileManager = [NSFileManager defaultManager];
# %%
def morse2Dot(in_filename, out_filename):
ROOT_LABEL = '(start)'
ROOT_CODE = 'START'
DUMMY = 'dummy'
DOT_SHAPE = 'shape=circle'
BAR_SHAPE = 'shape=rectangle'
ROOT_SHAPE = 'shape=diamond'
PROP_LINES = '\n\trankdir=TB;\n\tnodesep=0.5;\n\tpad=0.2;'
DUMMY_NODE_ATTR = ', label="(N/A)", margin=0'
@pgtwitter
pgtwitter / morse.py
Last active March 29, 2025 04:27
モール信号をツリー表示(dotファイル)にするpythonスクリプト(自作 / Grok3版 https://gist.github.com/pgtwitter/1d66a655c3c53b021b4ccc46f3fc8cc3 ).morse.txtを読み込んでmorse_tree.dotを作成する.
def morse2Dot(fin, fout):
class m:
def __init__(self, l, c):
self.c, self.n, self.cms, self.eA, self.nL, self.l, self.p = c, c.count('.'), [], DOT_eA if c[-1] == '.' else '', f'"{NA+c if l == NA else l}" [shape={SC if c[-1] == "." else SR}{NA_nA if l == NA else ""}];', f'{NA}{c}' if l == NA else l, RC if len(c) == 1 else c[0:-1]
def eLines(n):
ret = [f'"{n.l}" -> "{c.l}"[penwidth=2{c.eA}];' for c in n.cms]
ret.extend([l for c in n.cms for l in eLines(c)])
return ret
RL, RC, NA, SC, SR, NA_nA, DOT_eA, L1 = '(start)', 'START', 'DUMMY', 'circle', 'rectangle', ',label="(N/A)", margin=0', ', weight=1000, style=dashed', '{rank=same;'
L0, ms = f'rankdir=TB;msep=0.5;pad=0.2;"{RL}" [shape=diamond];', {RC: m(RL, RC)}
@pgtwitter
pgtwitter / morse.py
Last active March 21, 2025 15:38
モール信号をツリー表示(dotファイル)にするpythonスクリプトをGrok3に書かせてみた.morse.txtを読み込んでmorse_tree.dotを作成する.
# %%
def read_morse_file(filename):
"""モールス信号データをファイルから読み込む"""
morse_data = {}
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
char, code = line.strip().split('\t')
morse_data[char] = code
return morse_data
@pgtwitter
pgtwitter / morse.dot
Last active March 20, 2025 15:48
欧文モールス信号のツリーをGrok3に考えさせて延々とやりとりした成果(?)
digraph MorseTree {
rankdir=TB;
bgcolor="white";
nodesep=0.5; // 左右間隔を維持
pad=0.2; // 余白を維持
// ノード定義
START [shape=rectangle];
E [shape=rectangle]; A [shape=rectangle]; W [shape=rectangle]; J [shape=rectangle];
I [shape=rectangle]; S [shape=rectangle]; H [shape=rectangle]; V [shape=rectangle];
@pgtwitter
pgtwitter / .py
Last active February 6, 2025 22:21
停止点のあるsin(cos)を用いたモーションをつけるコード
# %%
import bpy
import numpy as np
def modified_sin(frames, target_fs, delta, amp, freq, shift, fn):
hold_fs = [f for fs in [list(range(v-delta, v+delta)) for v in target_fs] for f in fs]
cycle = frames - len(hold_fs)
ny = [amp*fn(freq*2*np.pi/cycle*t)-shift for t in range(cycle)]
result = []
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });
</script>
<div class="mermaid">
flowchart TB
A[Start] --> B{Is it?}
B -->|Yes| C[OK]
B -->|No| D[Not OK]
</div>
@pgtwitter
pgtwitter / .m
Last active November 20, 2024 05:34
Open a URL with an application ( Objective-C / Cocoa )
[[NSWorkspace sharedWorkspace] openURLs:@[url]
withApplicationAtURL:[NSURL fileURLWithPath:@"/Applications/Safari.app"]
configuration:nil
completionHandler:^(NSRunningApplication * _Nullable app, NSError * _Nullable error) {
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}];
@pgtwitter
pgtwitter / .r
Last active November 19, 2024 13:27
Find the distance between any point and a Bézier curve (find the parameter t that is the closest point on the Bézier curve). reference: https://shikitenkai.blogspot.com/2024/11/bezier.html
# %%
colors <- rainbow(length(1:5))
bezier_poly <- function(a, b, c, d, t) {
return((1 - t)^3 * a
+ 3 * (1 - t)^2 * t * b
+ 3 * (1 - t) * t^2 * c
+ t^3 * d)
}
bezier_point <- function(ps, t) {
x <- bezier_poly(ps[1, 1], ps[2, 1], ps[3, 1], ps[4, 1], t)