Skip to content

Instantly share code, notes, and snippets.

View fjenett's full-sized avatar
💤
(offline)

Florian Jenett fjenett

💤
(offline)
View GitHub Profile
// which part of the screen is my mouse in?
int grid;
void setup ()
{
size( 500, 500 );
grid = 3;
}
// dashed and dotted lines in Processing
// by Josh Nimoy, Based on Bresenham
void dashedLine ( float x0, float y0, float x1, float y1 )
{
dashedLine( (int)x0, (int)y0, (int)x1, (int)y1 );
}
void dashedLine ( int x0, int y0, int x1, int y1 )
{
@fjenett
fjenett / gist:350026
Created March 31, 2010 06:59
Is int even?
// quickly find out if an int is even or uneven with:
// (i & 1) == 0
// ... which "extracts" the last bit.
for ( int i = 0; i < 10; i++ )
println( i + " " + (i & 1) + ( (i & 1) == 0 ? " even" : " uneven" ) );
@fjenett
fjenett / gist:350027
Created March 31, 2010 07:00
Naive bresenham
// simple line algorithm to draw / read along a line.
//
// assumes x1 < x2 and y1 < y2
int x1=10,x2=100;
int y1=20,y2=40;
// assumes xdiff > ydiff
float xdiff = x2-x1;
float ydiff = y2-y1;
@fjenett
fjenett / gist:350028
Created March 31, 2010 07:01
Simple pixel tracer
// simple-trace pixel-images
//
int w2,h2;
int[] values;
int[][] pairs;
// extracting single channels from Processings color type
//
color c = color( 123, 234, 95, 125 ); // this is: color ( red, green, blue, alpha )
println( "Color in Hex:\t0x" + Integer.toHexString( c ).toUpperCase() );
int alpha = ( c >> 24 ) & 0xFF;
int red = ( c >> 16 ) & 0xFF;
int green = ( c >> 8 ) & 0xFF;
// benchmark to see if right-shift is faster than floor( / ).
// this is to quickly find which grid-element a location belongs to.
// test is in 1D only.
void draw ()
{
//setup
int grid = 3;
float gridP = pow(2,grid); // 16 >> 4 == 16 / 2^4
// manually setting the window (frame) title in Processing
void setup()
{
size( 200, 200 ); // needs to be first!
if ( this.frame != null )
this.frame.setTitle( "my funky title" );
}
// benchmark different sqrt implementations
void setup ()
{
int runs = 100000;
long ts = System.currentTimeMillis();
for ( int i=0; i < runs; i++ )
{
// gradient fill using PImage and texture()
Gradient gradient;
void setup ()
{
size(100, 100, P3D); // needs to be P3D / OPENGL since we use texture()
// create a Gradient size 100 x 100 px with two colors
gradient = new Gradient( 100, 100, 0xFF880000, 0xFFFFFFFF );