Skip to content

Instantly share code, notes, and snippets.

@micycle1
micycle1 / demo.java
Last active August 5, 2024 09:57
Exterior bisecting angle and tangent
void testBisector() {
PVector C = new PVector(100, 300);
PVector B = new PVector(mouseX, mouseY);
PVector A = new PVector(500, 500);
float r = 100;
float[] angle = new float[1]; // To store the calculated angle
PVector point = findPointOnBisector(A, B, C, r, angle);
stroke(0);
@micycle1
micycle1 / OrganizeMusic.ps1
Created July 5, 2024 16:57
Groups flac or mp3 files into album folders, saving with appropriate filenames. Preserves singles.
# Ensure ffprobe is available in the system's PATH or specify its full path
$ffprobePath = "ffprobe"
$files = Get-ChildItem -Path "." -File | Where-Object { $_.Extension -eq ".mp3" -or $_.Extension -eq ".flac" }
Write-Host "Found $($files.Count) files to process."
# Store album information
$albums = @{}
@micycle1
micycle1 / _.py
Created October 10, 2023 16:33
Bond duration, convexity and return
def modified_duration(y, m):
"""
Approximates the modified duration of a risk-free bond at par value.
:param y: Yield-to-maturity in decimal terms
:param m: Maturity in years
:return: Modified duration of a risk-free bond
"""
return (1-(1/(1+0.5*y)**(2*m)))/(y)
@micycle1
micycle1 / func.py
Created July 4, 2023 15:20
Function to print the date of the first non-null value for each column in a DataFrame, sorting the output by date.
def print_first_non_null_dates(df):
non_null_dates = {}
for column in df.columns:
non_null_values = df[column].dropna()
if not non_null_values.empty:
first_non_null_date = non_null_values.index[0]
non_null_dates[column] = first_non_null_date
sorted_dates = sorted(non_null_dates.items(), key=lambda x: x[1])
@micycle1
micycle1 / howto.md
Last active January 22, 2023 15:19
HQ Album Art from Apple Music
@micycle1
micycle1 / function
Created December 27, 2022 13:34
Java fast fixed-divisor integer division.
/**
* Computes a pair of magic numbers used to optimise integer division by a fixed
* divisor <code>d</code>.
* <p>
* The function is given the maximum value of the dividend <code>nmax</code> and
* the divisor <code>d</code>. It returns a pair of integers: the magic number
* <code>m</code> and a shift amount <code>p</code>.
* <p>
* To divide a dividend x by d, one multiplies x by m and then shifts the (full
* length) product right p bits: <code>x / d === ((x * m) >> p)</code>.
@micycle1
micycle1 / example.java
Created September 1, 2021 17:06
Make java.applet.Applet runnable
public class T2Q extends java.applet.Applet {
public static void main(String[] args) {
T2Q applet = new T2Q();
applet.init();
applet.start();
// Create a window (JFrame) and make applet the content pane.
javax.swing.JFrame window = new javax.swing.JFrame("T2Q");
window.setContentPane(applet);
@micycle1
micycle1 / MinimumEnclosingTriangle.java
Created April 19, 2021 14:20
MinimumEnclosingTriangle
import org.locationtech.jts.geom.Coordinate;
import java.util.ArrayList;
import java.util.List;
/**
* Implementation of linear minimum area enclosing triangle algorithm.
* Computational and Applied Mathematics, 35(2), 423438.
* doi:10.1007/s40314-014-0198-8
*
@micycle1
micycle1 / EarClippingTriangulator.java
Created March 28, 2021 19:56
GDX EarClippingTriangulator
package com.badlogic.gdx;
/**
* A simple implementation of the ear cutting algorithm to triangulate simple
* polygons without holes. For more information:
* <ul>
* <li><a href=
* "http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian/algorithm2.html">http://cgm.cs.mcgill.ca/~godfried/
* teaching/cg-projects/97/Ian/algorithm2.html</a></li>
* <li><a href=
@micycle1
micycle1 / fastCubic
Created February 7, 2021 22:40
A fast solution to cubic equations with three real roots
http://momentsingraphics.de/CubicRoots.html#_Blinn06a
Returns the three real roots of the cubic polynomial (of form 1 + 2x + 3x^2 + 4x^3)
float3 SolveCubic(float4 Coefficient){
// Normalize the polynomial
Coefficient.xyz/=Coefficient.w;
// Divide middle coefficients by three
Coefficient.yz/=3.0f;
// Compute the Hessian and the discrimant