Skip to content

Instantly share code, notes, and snippets.

# %%
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)
@pgtwitter
pgtwitter / AppDelegate.h
Last active November 29, 2024 06:04
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
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@end
@pgtwitter
pgtwitter / .py
Created November 4, 2024 05:24
ポアンカレの円板(参考 https://qiita.com/hibit/items/5a49bedaa826fddf0a33 を 縦横比を1にしたもの)
# %%
# reference https://qiita.com/hibit/items/5a49bedaa826fddf0a33
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(aspect=1)
theta = np.linspace(0, 2*np.pi, 100)
colorlist = ["r", "g", "b", "c", "m", "y"]