Skip to content

Instantly share code, notes, and snippets.

@kujirahand
kujirahand / mandelbrot_video__src__main.rs
Last active May 31, 2024 03:10
マンデルブロ集合の画像を連続で書き出しするプログラム
use image::{ImageBuffer, Rgb};
fn main() {
let (sx, sy) = (1280, 720);
let (cx, cy, _zoom) = (0.2713912051561837, 0.005142969158296763, 1.4210854715202004e-14);
let fps = 24;
let image_count = fps * 30;
let mut zoom = 4.0;
for i in 0..image_count {
println!("{:04}/{} - m{:04}.png - zoom*{:.8}", i, image_count, i, zoom);
@kujirahand
kujirahand / hsl_to_rgb.rs
Created May 31, 2024 02:09
RustでHSLからRGBに変換する関数
fn hsl_to_rgb(h: f64, s: f64, l: f64) -> (u8, u8, u8) {
let (mut r, mut g, mut b) = (l, l, l);
if s != 0.0 {
let hue2rgb = |p: f64, q: f64, t: f64| -> f64 {
let mut t = t;
if t < 0.0 { t += 1.0; }
if t > 1.0 { t -= 1.0; }
if t < 1.0 / 6.0 { return p + (q - p) * 6.0 * t; }
if t < 1.0 / 2.0 { return q; }
if t < 2.0 / 3.0 { return p + (q - p) * (2.0 / 3.0 - t) * 6.0; }
@kujirahand
kujirahand / index3.html
Created May 29, 2024 09:53
マンデルブロ集合の図形(マウスイベント+カラー変更)
<!DOCTYPE html>
<html><head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>マンデルブロ集合</title>
</head>
<body>
<canvas id="cnavas" width="800" height="800"></canvas>
</body>
<script>
// キャンバスと描画用のコンテキストを取得
@kujirahand
kujirahand / index.html
Created May 29, 2024 09:51
マンデルブロ集合の図形
<!DOCTYPE html><html><head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>マンデルブロ集合</title>
</head>
<body><canvas id="cnavas" width="800" height="800"></canvas></body>
<script>
// キャンバスと描画用のコンテキストを取得 --- (*1)
const canvas = document.getElementById('cnavas');
const ctx = canvas.getContext('2d');
// 描画の為のパラメータを指定 --- (*2)
@kujirahand
kujirahand / lifegame__main.rs
Last active May 20, 2024 05:47
Rustで作ったコンウェイのライフゲーム(Life game)
// ライフゲームのプログラム
use lazyrand::rand_usize;
use std::thread;
use std::io::{stdout, Result};
use crossterm::{cursor, execute, terminal,
style::{Color, Print, SetBackgroundColor, SetForegroundColor}};
// 定数の宣言
const WIDTH: usize = 80; // グリッドの列数
const HEIGHT: usize = 35; // グリッドの行数
const MAX_TERN: usize = 1000; // 最大世代数
@kujirahand
kujirahand / my_http_server__src__main.rs
Created April 22, 2024 03:27
RustでHTTPサーバを実装しよう
use std::io::prelude::*;
use std::net::TcpListener;
// サーバアドレスを指定
const SERVER_ADDRESS: &str = "127.0.0.1:8888";
fn main() {
// HTTPサーバを起動
println!("[HTTPサーバを起動] http://{}", SERVER_ADDRESS);
let listener = TcpListener::bind(SERVER_ADDRESS).unwrap();
@kujirahand
kujirahand / main.rs
Created February 24, 2024 16:17
k近傍法を使って、アヤメの分類を行うプログラム
// k近傍法(k-nn)によるアヤメの分類プログラム
use rand::seq::SliceRandom;
// データとラベルを持つ構造体を定義 --- (*1)
#[derive(Debug, Clone)]
struct KnnItem {
data: Vec<f64>,
label: String,
}
// k近傍法でデータを予測する --- (*2)
fn knn_predict(items: &[KnnItem], test: &[f64], k: usize) -> String {
@kujirahand
kujirahand / iris.csv
Last active February 24, 2024 12:50
iris.csv for ml-test (CC BY 4.0)
sepal_length sepal_width petal_length petal_width class
5.1 3.5 1.4 0.2 Iris-setosa
4.9 3.0 1.4 0.2 Iris-setosa
4.7 3.2 1.3 0.2 Iris-setosa
4.6 3.1 1.5 0.2 Iris-setosa
5.0 3.6 1.4 0.2 Iris-setosa
5.4 3.9 1.7 0.4 Iris-setosa
4.6 3.4 1.4 0.3 Iris-setosa
5.0 3.4 1.5 0.2 Iris-setosa
4.4 2.9 1.4 0.2 Iris-setosa
@kujirahand
kujirahand / server.js
Created February 6, 2024 16:20
Node.jsとChatGPT APIを使った翻訳ツール
// ライブラリを読み込む --- (*1)
const express = require('express');
const OpenAI = require('openai')
const path = require('path')
// APIキーを設定する(以下は書き換えが必要です★★★) --- (*2)
const API_KEY = 'xxx'
// const API_KEY = process.env['OPENAI_API_KEY']
// APIキーを設定したオブジェクトを生成
const openai = new OpenAI({ apiKey: API_KEY });
@kujirahand
kujirahand / crc32__main.rs
Last active January 19, 2024 11:13
CRC32を計算するプログラム - キャッシュテーブルを利用する例
// CRC-32のためのキャッシュテーブルを作成する関数 --- (*1)
fn crc32_table() -> [u32; 256] {
// テーブルを0で初期化
let mut table: [u32; 256] = [0; 256];
let crc32poly:u32 = 0xEDB88320;
// 0-255の各値について繰り返し計算を行う --- (*2)
for i in 0..256 {
let mut crc:u32 = i as u32; // 値を初期化
// 8回(8ビット分)繰り返す --- (*4)
for _ in 0..8 {