Skip to content

Instantly share code, notes, and snippets.

@AllanHasegawa
AllanHasegawa / serialization.kt
Last active May 29, 2023 07:47
KotlinX Serialization With Sealed Classes Example
import kotlinx.serialization.*
import kotlinx.serialization.internal.ArrayListSerializer
import kotlinx.serialization.internal.StringDescriptor
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
@Serializable
sealed class Foo {
@Serializable
@AllanHasegawa
AllanHasegawa / cnpj.kt
Last active November 13, 2023 01:47
Validador de CNPJ usando Kotlin
data class CNPJ(val value: String)
fun CNPJ.isValid(): Boolean {
val cnpj = value
return validateCNPJLength(cnpj) && validateCNPJRepeatedNumbers(cnpj)
&& validateCNPJVerificationDigit(true, cnpj)
&& validateCNPJVerificationDigit(false, cnpj)
}
// Auxiliary code!!!!!
// Ignore and go to "START FROM HERE" below!!!!
function randomInt(min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min)) + min
}
// Data
@AllanHasegawa
AllanHasegawa / FontCache.kt
Created July 8, 2017 19:58
Android Async Font Typeface Loading
object FontCache {
private val FONTS_DIR = "fonts/"
private val CACHE = Hashtable<String, Typeface>()
fun notoSansCJK(context: Context, textView: TextView) {
applyTypefaceAsync(context, textView, "NotoSansCJKjp-Regular.otf")
}
/**
* * New version: 30/May/2017
* * Made it more kotlin-like with same functionality ^^
*
* based on: https://gist.github.com/RomansBermans/6f3836188427fbd3b1efcf7e6418f06d
* Fancy ID generator that creates 20-character string identifiers with the following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs.
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly).
<link rel="import" href="../paper-slider/paper-slider.html">
<polymer-element name="my-element">
<template>
<style>
:host {
position: absolute;
width: 100%;
height: 100%;
@AllanHasegawa
AllanHasegawa / designer.html
Last active August 29, 2015 14:17
designer
<link rel="import" href="../polymer/polymer.html">
<polymer-element name="my-element">
<template>
<style>
:host {
position: absolute;
width: 100%;
height: 100%;
@AllanHasegawa
AllanHasegawa / designer.html
Last active August 29, 2015 14:17
designer
<link rel="import" href="../core-scaffold/core-scaffold.html">
<link rel="import" href="../core-header-panel/core-header-panel.html">
<link rel="import" href="../core-menu/core-menu.html">
<link rel="import" href="../core-item/core-item.html">
<link rel="import" href="../core-icon-button/core-icon-button.html">
<link rel="import" href="../core-toolbar/core-toolbar.html">
<link rel="import" href="../core-menu/core-submenu.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../core-icons/core-icons.html">
<link rel="import" href="../core-icon/core-icon.html">
@AllanHasegawa
AllanHasegawa / gist:310b526568b7157faad4
Created October 7, 2014 06:26
Cylinder Line Minimum Distance plot
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
import scipy.optimize
def cylinderLineFirstInterval(cylinderLength, lineP0, lineP1):
l = cylinderLength
p0 = lineP0
p1 = lineP1
@AllanHasegawa
AllanHasegawa / gist:db1752da2f982ea0dfb2
Last active August 29, 2015 14:06
OpenVDB - create_levelset_volume (usage)
using GridT = openvdb::FloatGrid;
auto grid1 = openvdb::tools::createLevelSetSphere<GridT>(
512.0, openvdb::Vec3f(0, 0, 0), 1, 2);
Sphere s0{glm::vec4(-512,-512,512,1), 16};
LinearSphereSweptVolume volume0(s0, /*translation=*/glm::vec3(1024, 1024, 0));
auto grid2 = create_levelset_volume<GridT>(volume0, 1, 2);
openvdb::tools::csgDifference(*grid1, *grid2);
s0 = Sphere{glm::vec4(-512,512,1024,1), 16};