Skip to content

Instantly share code, notes, and snippets.

View bohnacker's full-sized avatar

Hartmut Bohnacker bohnacker

  • Stuttgart, Germany
View GitHub Profile
@bohnacker
bohnacker / circleOfDots_AdvancedRings.js
Last active April 29, 2024 08:33
Calculate positions of dots to draw a filled circle made of dots. This version creates rings in a more advanced way, so that the outer ring is always completely filled.
// Calling this function will return a function that gives you the position of the dot at an index.
// You must also pass the total number of dots of the circle to this function to calculate correctly.
//
// This version creates rings in a more advanced way, so that the outer ring is always completely filled.
// This is made as a closure so that the array of positions is only calculated once.
//
// Usage:
// let getDotPosition = initCircleOfDots_AdvancedRings();
// let p = getDotPosition(50, 100);
//
@bohnacker
bohnacker / circleOfDots_SimpleRings.js
Created January 24, 2024 12:06
Calculate positions of dots to draw a filled circle made of dots. This version creates simple rings. The outer ring will be partially filled most of the time.
// Calling this function will return a function that gives you the position of the dot at an index.
// This version creates rings of dots. The outer ring will be partially filled most of the time.
// This is made as a closure so that the array of positions is only calculated once.
//
// Usage:
// let getDotPosition = initCircleOfDots_SimpleRings();
// let p = getDotPosition(50);
//
// This will return a point in the format { x: 1.4724, y: 3.7191 }
// The distance between the dots is approximately 1, so you probably have to scale the positions.
@bohnacker
bohnacker / circleOfDots_Spiral.js
Created January 24, 2024 12:01
Calculate positions of dots to draw a filled circle made of dots.
// Calling this function will return a function that gives you the position of the dot at an index.
// This version uses the golden angle to create a tightly packed spiral.
// This is made as a closure so that the array of positions is only calculated once.
//
// Usage:
// let getDotPosition = initCircleOfDots_Spiral();
// let p = getDotPosition(50);
//
// This will return a point in the format { x: 2.9227, y: 1.9894 }
// The distance between the dots is approximately 1, so you probably have to scale the positions.
@bohnacker
bohnacker / TaggableTextArea.svelte
Last active August 11, 2023 08:30
Editable text field as a Svelte component, where words starting with # or @ gets highlighted while typing. If a list of tags or persons is given, autocompletion hints are given and may be accepted using tab.
<script>
import { tick, onMount } from "svelte";
export let readonly = true; // whether the text is readonly
export let text = ""; // text to be displayed
export let placeholder = ""; // placeholder text
export let highlightTag = true; // whether to highlight tags
export let tagColor = "DodgerBlue"; // color of tags
export let tagList = []; // list of tags for autocompletion
export let highlightPerson = true; // whether to highlight persons
@bohnacker
bohnacker / dropable.js
Last active February 11, 2021 11:00
Svelte action to turn a node into a file drop area. Demo at: https://svelte.dev/repl/d3af7296998346aeb50d6657b7e31d26?version=3.32.3
// The action "dropable" turns a node into a file drop area and dispatches the custom event "files" when
// on or more files were dropped. You'll get an array of files in event.detail.
// You can provide filetypes as a string or an array of strings, e.g.: "text/html" or ["image/png", "image/jpeg"]
// Matching will be done by searching for the substring, so filetype "image/" will match all images
// or "/svg" will match "image/svg+xml".
export function dropable(node, filetypes) {
if (typeof filetypes === "string") filetypes = [filetypes]
@bohnacker
bohnacker / csv-manipulate.txt
Last active December 17, 2019 15:07
Python scripts to manipulate CSV files. I moved these to https://github.com/bohnacker/data-manipulation because it got to messy here.
Moved to a github repository:
https://github.com/bohnacker/data-manipulation
@bohnacker
bohnacker / LICENCE
Created July 18, 2019 10:10
This licence applies to all of my public gists
MIT License
Copyright (c) 2019 Hartmut Bohnacker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@bohnacker
bohnacker / points_to_curve.js
Last active August 20, 2021 15:31
A javascript function that takes a list of points and calculates a curvy path that passes all these points. See https://hartmut-bohnacker.de/projects/points-to-curve for more information.
// Calculates a curve that goes through a number of points.
// There are lots of mathematical approaches to do so
// (see: https://en.wikipedia.org/wiki/Cubic_Hermite_spline).
// The most commonly used seems to be the Catmull–Rom spline.
// My approch here is not intended to be a new general
// solution to this problem, but it should fit some geometrical
// and graphical needs. See
// https://hartmut-bohnacker.de/projects/points-to-curve
// for more explanation.
@bohnacker
bohnacker / angleDifference.js
Created May 5, 2019 07:50
Returns the difference of two angles given in radians. The result is always between -PI and PI.
// Returns the difference of two angles given in radians.
// The result is always between -PI and PI.
function angleDifference(angle1, angle2) {
const TWO_PI = Math.PI * 2;
let a1 = (angle1 % TWO_PI + TWO_PI) % TWO_PI;
let a2 = (angle2 % TWO_PI + TWO_PI) % TWO_PI;
if (a2 > a1) {
let d1 = a2 - a1;
@bohnacker
bohnacker / crossingPoint.js
Last active May 5, 2019 09:18
Checks if two lines are crossing and returns the crossing point. There are two modes: if infinite is false, there will be a result only if the lines are crossing between their start and end points. Otherwise the lines will be thought of being infinite.
// Checks, if two lines (p1a to p1b) and (p2a to p2b) are crossing.
// Points are given as [x, y]. If inifinte1 is true, the first line
// will be continued before start and after end points. Same for line 2.
// Returns the crossing point as [x, y] or false if there is none.
// ATTENTION: this calculation is not completely precice in some cases!
function crossingPoint(p1a, p1b, p2a, p2b, infinite1, infinite2) {
// vectors from start to end points
let d1x = p1b[0] - p1a[0];