Skip to content

Instantly share code, notes, and snippets.

@esimov
esimov / factorial.go
Last active June 11, 2023 21:19
Factorial calculation in Go lang using three different methods: first traditionally, second with closure and third using memoization. The last method is the fastest between the three.
package main
import (
"fmt"
"time"
)
const LIM = 41
var facts [LIM]uint64
@esimov
esimov / mouseposition.js
Last active November 14, 2022 13:40
Get mouse position relative to canvas in JS
BitmapData.prototype.getContext = function() {
return this.context;
};
BitmapData.prototype.getCanvas = function() {
return this.canvas;
};
BitmapData.prototype.getCanvasPos = function(el) {
var canvas = document.getElementById(el) || this.getCanvas();
@esimov
esimov / perlin.js
Created October 6, 2014 08:37
Perlin noise implementation in Javascript
var NOISE = NOISE || { };
NOISE.Perlin = (function() {
var iOctaves = 1,
fPersistence = 0.2,
fResult, fFreq, fPers,
aOctFreq, // frequency per octave
aOctPers, // persistance per octave
fPersMax; // 1 / max persistence
@esimov
esimov / fact_goroutine.go
Last active August 7, 2022 00:20
Example of parallel factorial computation in Go lang using go routines.
package main
import (
"math/rand"
"fmt"
)
const (
N int = 128
)
c.processor = &triangle.Processor{
BlurRadius: 2,
Noise: 0,
BlurFactor: 2,
EdgeFactor: 4,
PointRate: c.pointRate,
MaxPoints: c.trianglePoints,
PointsThreshold: c.pointsThreshold,
Wireframe: c.wireframe,
StrokeWidth: c.strokeWidth,
uint8Arr := js.Global().Get("Uint8Array").New(width * height * 4)
js.CopyBytesToJS(uint8Arr2, pixels.ImgToPix(blurred))
uint8Clamped := js.Global().Get("Uint8ClampedArray").New(uint8Arr2)
rawData := js.Global().Get("ImageData").New(uint8Clamped, width, height)
c.ctx.Call("putImageData", rawData, 0, 0)
rgba := c.ctx.Call("getImageData", 0, 0, width, height).Get("data")
// Convert the rgba value of type Uint8ClampedArray to Uint8Array in order to
// be able to transfer it from Javascript to Go via the js.CopyBytesToGo function.
uint8Arr := js.Global().Get("Uint8Array").New(rgba)
js.CopyBytesToGo(data, uint8Arr)
// ImgToPix converts an image to an 1D uint8 pixel array.
// In order to preserve the color information per pixel we need to reconstruct the array to a specific format.
func ImgToPix(src *image.NRGBA) []uint8 {
size := src.Bounds().Size()
width, height := size.X, size.Y
pixels := make([][][3]uint8, height)
for y := 0; y < height; y++ {
row := make([][3]uint8, width)
for x := 0; x < width; x++ {
package draw
import (
"image"
"image/color"
)
// ellipse defines the struct components required to apply the ellipse's formula.
type ellipse struct {
cx int // center x
draw.DrawMask(source.(*image.NRGBA), source.Bounds(), ellipse, image.Point{}, mask, image.Point{}, draw.Over)