Skip to content

Instantly share code, notes, and snippets.

@bogovicj
Created February 14, 2022 20:43
Show Gist options
  • Save bogovicj/cbfa381f29ca6d7877eaf104d5197959 to your computer and use it in GitHub Desktop.
Save bogovicj/cbfa381f29ca6d7877eaf104d5197959 to your computer and use it in GitHub Desktop.
Draw a pretty heart with imglib2
/**
* Happy Valentine's day 2022
*
* author: John Bogovic
*/
import net.imglib2.*;
import net.imglib2.view.*;
import net.imglib2.img.array.*;
import net.imglib2.img.imageplus.*;
import net.imglib2.type.numeric.*;
def cardioid( img, radius, color ) {
nx = img.dimension( 0 );
xc = img.dimension( 0 ) * 0.7;
yc = img.dimension( 1 ) * 0.5;
ra = img.randomAccess();
for( double t = 0; t < 2 * Math.PI; t += 0.001 ) {
cos = Math.cos( t );
sin = Math.sin( t );
x = 2 * radius * ( 1 - cos ) * cos;
y = 2 * radius * ( 1 - cos ) * sin;
ra.setPosition(nx - Math.round(xc+x), 1);
ra.setPosition(Math.round(yc+y), 0);
ra.get().set(color);
}
}
imgRaw = ImagePlusImgs.argbs(256, 256);
imp = imgRaw.getImagePlus();
imp.show();
imp.getCanvas().zoomIn( 128, 128 );
imp.getCanvas().zoomIn( 128, 128 );
imp.getCanvas().zoomIn( 128, 128 );
for( i in 1..128) {
r = 0.25 * i;
c = ARGBType.rgba(255, 255-(2*i), 255-(2*i), 255);
cardioid( imgRaw, r, c );
imp.repaintWindow();
Thread.sleep( 50 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment