Life as Clay

Archive for the ‘Code’ Category

Silly small Processing sketch for trig reference

leave a comment »

This is just a small Processing sketch that I’m throwing up here for reference. It seems that every time I’m trying to calculate the point on a circle, I forget whether I’m working in angles or radians, etc…

int     radius, lines;
float   angle;
PVector center;

void setup() {
  size(600, 600); 
  lines = (int)random(4, 20);
  if (lines % 2 > 0) {
    lines += 1;
  radius = 200;
  angle  = 360.0 / lines;
  center = new PVector(width/2.0,height/2,0);

void draw() {
  ellipse(width/2, height/2, radius*2, radius*2);
  for(int i = 0; i < lines; i++){
    float thisAngle = i * angle;
    float ptX = center.x + sin(radians(thisAngle))*radius;
    float ptY = center.y + cos(radians(thisAngle))*radius;

Written by Clay

February 20, 2014 at 10:55

Posted in Processing, Reference

Tagged with ,

Nested arrays in d3.js

leave a comment »

I find nested arrays to be difficult to work with in d3.js. Every time I begin a new d3 project, I spend a few minutes confused about how to access the data. I built this small file to be able to play with the values and remind myself what they do.

<!DOCTYPE html>
<meta charset="utf-8">

body {
  font: 14px sans-serif;
  font-weight: bold;

 .chart {
    background-color: #efefef;
    shape-rendering: crispEdges;

 .series {
    background-color: red;

 .text0 {
    fill: red;

 .text1 {
    fill: green;

 .text2 {

 .text3 {
    fill: yellow;

 .text4 {
    fill: purple;

 .text5 {

<script src=""></script>

<script type="text/javascript">
var nestedArray = [[5, 10, 15, 20, 25],
                   [10, 20, 30, 40, 50],
                   [43, 34, 17, 9, 2]];

var chart ="body").append("svg")
            .attr("class", "chart")
            .attr("id", "chart")
            .attr("width", 550)
            .attr("height", 40 * 5);

var series = chart.selectAll(".series")
                .attr("class", "series");

var point  = series.selectAll(".point")
             .data(         function(d,i,j) { return d;          } )
             .text(         function(d,i,j) { return d;          } )
             .attr("class", function(d,i,j) { return "text" + j; } )
             .attr("x",     function(d,i,j) { return 10 * d;     } )
             .attr("y",     function(d,i,j) { return 40 * (j+1); } );


Written by Clay

September 15, 2013 at 17:16

Posted in Code, d3, javascript

Tagged with

Processing class for finding normal vectors

leave a comment »

Processing uses the PVector class to store vector data. The .normalize() method will normalize any PVector to a unit vector. What I needed to do was to find the normal of a vector — a vector perpendicular to the original vector. This is easy enough to do with some simple math. However, I wanted to encapsulate the logic into a class so that I could store multiple perpendicular vectors (to an original vector) and different locations along the original vector, and at different magnitudes. That is what this class does:


class PerpVectorPack {
  // Store the original data
  PVector originalVector;

  // Keep track of values used to calculate points
  ArrayList<Float>   percentOfOriginalVector;
  ArrayList<Float>   magnitudeOfPerpendicularVector;
  ArrayList<Boolean> onRightSide;

  // Storage of calculated perpendicular line points
  ArrayList pointsOnLine;
  ArrayList perpVectorPoints;

  // Constructors
  PerpVectorPack(PVector origVector) {
    originalVector = origVector.get();

  PerpVectorPack(PVector origVector, float perpVectDist, float perpVectMag, boolean rightSide) {    
    originalVector = origVector.get();
    addPerpVector(perpVectDist, perpVectMag, rightSide);

  private void initializeLists() {
    pointsOnLine                    = new ArrayList();
    perpVectorPoints                = new ArrayList();

    percentOfOriginalVector         = new ArrayList<Float>();
    magnitudeOfPerpendicularVector  = new ArrayList<Float>();
    onRightSide                     = new ArrayList<Boolean>();

  // Methods
  public void addPerpVector(float perpVectDist, float perpVectMag, boolean rightSide) {

    // Keep track of the values used to create the perpendicular points

    PVector full    = originalVector.get();
    float   fullMag = full.mag();

    // Determine where along the original vector the perpendicular vector should lie (lay?)
    full.mult(perpVectDist * fullMag);

    // Set the first local variable

    // Get the normal of the original full vector and normalize it
    PVector perpVector  = new PVector(-originalVector.y, originalVector.x);

    // Determine if it should be on the right or left side of the original vector
    int dir = 1;
    if (!rightSide) dir = -1;

    // Make it the proper length
    perpVector.mult(fullMag * perpVectMag * dir);

    // Offset it from the origin

    // Set the second local variable

  public PVector ptOnOriginalLine() { 
    return ptOnOriginalLine(0);
  public PVector ptOnOriginalLine(int idx) {
    PVector thePoint = (PVector)pointsOnLine.get(idx);
    return thePoint.get();

  public PVector perpVectorPt() { 
    return perpVectorPt(0);
  public PVector perpVectorPt(int idx) {
    PVector thePoint = (PVector)perpVectorPoints.get(idx);
    return thePoint.get();

Written by Clay

February 21, 2013 at 22:44

Posted in Code, Processing, Resource

Tagged with ,

New Population Pyramid Generator

leave a comment »

I created a new OS X population pyramid generator application, called Pyramids. Click here to see it in the Mac App Store. It’s $0.99. If you have a Mac and need to make population pyramids with any regularity, or even just a single one, then give it a shot!


Dict elements for working with US Census Bureau via Cocoa

leave a comment »

I’m querying from a part of the US Census Bureau’s website that doesn’t have a public API and I needed the value codes they are using in their countries popup. I create an NSDictionary with them and then bind an NSPopUpButton to them via an NSDictionaryController. Here are the objects and keys that I use to display the values:

@"AF",	@"Afghanistan", 
@"AL",	@"Albania",                         
@"AG",	@"Algeria",                         
@"AQ",	@"American Samoa",                  
@"AN",	@"Andorra",                         
@"AO",	@"Angola",                          
@"AV",	@"Anguilla",                        
@"AC",	@"Antigua and Barbuda",             
@"AR",	@"Argentina",                       
@"AM",	@"Armenia",                         
@"AA",	@"Aruba",                           
@"AS",	@"Australia",                       
@"AU",	@"Austria",                         
@"AJ",	@"Azerbaijan",                      
@"BF",	@"Bahamas, The",                    
@"BA",	@"Bahrain",                         
@"BG",	@"Bangladesh",                      
@"BB",	@"Barbados",                        
@"BO",	@"Belarus",                         
@"BE",	@"Belgium",                         
@"BH",	@"Belize",                          
@"BN",	@"Benin",                           
@"BD",	@"Bermuda",                         
@"BT",	@"Bhutan",                          
@"BL",	@"Bolivia",                         
@"BK",	@"Bosnia and Herzegovina",          
@"BC",	@"Botswana",                        
@"BR",	@"Brazil",                          
@"BX",	@"Brunei",                          
@"BU",	@"Bulgaria",                        
@"UV",	@"Burkina Faso",                    
@"BM",	@"Burma",                           
@"BY",	@"Burundi",                         
@"CB",	@"Cambodia",                        
@"CM",	@"Cameroon",                        
@"CA",	@"Canada",                          
@"CV",	@"Cape Verde",                      
@"CJ",	@"Cayman Islands",                  
@"CT",	@"Central African Republic",        
@"CD",	@"Chad",                            
@"CI",	@"Chile",                           
@"CH",	@"China",                           
@"CO",	@"Colombia",                        
@"CN",	@"Comoros",                         
@"CF",	@"Congo (Brazzaville)",             
@"CG",	@"Congo (Kinshasa)",                
@"CW",	@"Cook Islands",                    
@"CS",	@"Costa Rica",                      
@"IV",	@"Cote d'Ivoire",                   
@"HR",	@"Croatia",                         
@"CU",	@"Cuba",                            
@"UC",	@"Curacao",                         
@"CY",	@"Cyprus",                          
@"EZ",	@"Czech Republic",                  
@"DA",	@"Denmark",                         
@"DJ",	@"Djibouti",                        
@"DO",	@"Dominica",                        
@"DR",	@"Dominican Republic",              
@"EC",	@"Ecuador",                         
@"EG",	@"Egypt",                           
@"ES",	@"El Salvador",                     
@"EK",	@"Equatorial Guinea",               
@"ER",	@"Eritrea",                         
@"EN",	@"Estonia",                         
@"ET",	@"Ethiopia",                        
@"FO",	@"Faroe Islands",                   
@"FJ",	@"Fiji",                            
@"FI",	@"Finland",                         
@"FR",	@"France",                          
@"FP",	@"French Polynesia",                
@"GB",	@"Gabon",                           
@"GA",	@"Gambia, The",                     
@"GZ",	@"Gaza Strip",                      
@"GG",	@"Georgia",                         
@"GM",	@"Germany",                         
@"GH",	@"Ghana",                           
@"GI",	@"Gibraltar",                       
@"GR",	@"Greece",                          
@"GL",	@"Greenland",                       
@"GJ",	@"Grenada",                         
@"GQ",	@"Guam",                            
@"GT",	@"Guatemala",                       
@"GK",	@"Guernsey",                        
@"GV",	@"Guinea",                          
@"PU",	@"Guinea-Bissau",                   
@"GY",	@"Guyana",                          
@"HA",	@"Haiti",                           
@"HO",	@"Honduras",                        
@"HK",	@"Hong Kong",                       
@"HU",	@"Hungary",                         
@"IC",	@"Iceland",                         
@"IN",	@"India",                           
@"ID",	@"Indonesia",                       
@"IR",	@"Iran",                            
@"IZ",	@"Iraq",                            
@"EI",	@"Ireland",                         
@"IM",	@"Isle of Man",                     
@"IS",	@"Israel",                          
@"IT",	@"Italy",                           
@"JM",	@"Jamaica",                         
@"JA",	@"Japan",                           
@"JE",	@"Jersey",                          
@"JO",	@"Jordan",                          
@"KZ",	@"Kazakhstan",                      
@"KE",	@"Kenya",                           
@"KR",	@"Kiribati",                        
@"KN",	@"Korea, North",                    
@"KS",	@"Korea, South",                    
@"KV",	@"Kosovo",                          
@"KU",	@"Kuwait",                          
@"KG",	@"Kyrgyzstan",                      
@"LA",	@"Laos",                            
@"LG",	@"Latvia",                          
@"LE",	@"Lebanon",                         
@"LT",	@"Lesotho",                         
@"LI",	@"Liberia",                         
@"LY",	@"Libya",                           
@"LS",	@"Liechtenstein",                   
@"LH",	@"Lithuania",                       
@"LU",	@"Luxembourg",                      
@"MC",	@"Macau",                           
@"MK",	@"Macedonia",                       
@"MA",	@"Madagascar",                      
@"MI",	@"Malawi",                          
@"MY",	@"Malaysia",                        
@"MV",	@"Maldives",                        
@"ML",	@"Mali",                            
@"MT",	@"Malta",                           
@"RM",	@"Marshall Islands",                
@"MR",	@"Mauritania",                      
@"MP",	@"Mauritius",                       
@"MX",	@"Mexico",                          
@"FM",	@"Micronesia, Federated States of", 
@"MD",	@"Moldova",                         
@"MN",	@"Monaco",                          
@"MG",	@"Mongolia",                        
@"MJ",	@"Montenegro",                      
@"MH",	@"Montserrat",                      
@"MO",	@"Morocco",                         
@"MZ",	@"Mozambique",                      
@"WA",	@"Namibia",                         
@"NR",	@"Nauru",                           
@"NP",	@"Nepal",                           
@"NL",	@"Netherlands",                     
@"NC",	@"New Caledonia",                   
@"NZ",	@"New Zealand",                     
@"NU",	@"Nicaragua",                       
@"NG",	@"Niger",                           
@"NI",	@"Nigeria",                         
@"CQ",	@"Northern Mariana Islands",        
@"NO",	@"Norway",                          
@"MU",	@"Oman",                            
@"PK",	@"Pakistan",                        
@"PS",	@"Palau",                           
@"PM",	@"Panama",                          
@"PP",	@"Papua New Guinea",                
@"PA",	@"Paraguay",                        
@"PE",	@"Peru",                            
@"RP",	@"Philippines",                     
@"PL",	@"Poland",                          
@"PO",	@"Portugal",                        
@"RQ",	@"Puerto Rico",                     
@"QA",	@"Qatar",                           
@"RO",	@"Romania",                         
@"RS",	@"Russia",                          
@"RW",	@"Rwanda",                          
@"TB",	@"Saint Barthelemy",                
@"SH",	@"Saint Helena",                    
@"SC",	@"Saint Kitts and Nevis",           
@"ST",	@"Saint Lucia",                     
@"RN",	@"Saint Martin",                    
@"SB",	@"Saint Pierre and Miquelon",       
@"VC",	@"Saint Vincent and the Grenadines",
@"WS",	@"Samoa",                           
@"SM",	@"San Marino",                      
@"TP",	@"Sao Tome and Principe",           
@"SA",	@"Saudi Arabia",                    
@"SG",	@"Senegal",                         
@"RI",	@"Serbia",                          
@"SE",	@"Seychelles",                      
@"SL",	@"Sierra Leone",                    
@"SN",	@"Singapore",                       
@"NN",	@"Sint Maarten",                    
@"LO",	@"Slovakia",                        
@"SI",	@"Slovenia",                        
@"BP",	@"Solomon Islands",                 
@"SO",	@"Somalia",                         
@"SF",	@"South Africa",                    
@"OD",	@"South Sudan",                     
@"SP",	@"Spain",                           
@"CE",	@"Sri Lanka",                       
@"SU",	@"Sudan",                           
@"NS",	@"Suriname",                        
@"WZ",	@"Swaziland",                       
@"SW",	@"Sweden",                          
@"SZ",	@"Switzerland",                     
@"SY",	@"Syria",                           
@"TW",	@"Taiwan",                          
@"TI",	@"Tajikistan",                      
@"TZ",	@"Tanzania",                        
@"TH",	@"Thailand",                        
@"TT",	@"Timor-Leste",                     
@"TO",	@"Togo",                            
@"TN",	@"Tonga",                           
@"TD",	@"Trinidad and Tobago",             
@"TS",	@"Tunisia",                         
@"TU",	@"Turkey",                          
@"TX",	@"Turkmenistan",                    
@"TK",	@"Turks and Caicos Islands",        
@"TV",	@"Tuvalu",                          
@"UG",	@"Uganda",                          
@"UP",	@"Ukraine",                         
@"AE",	@"United Arab Emirates",            
@"UK",	@"United Kingdom",                  
@"US",	@"United States",                   
@"UY",	@"Uruguay",                         
@"UZ",	@"Uzbekistan",                      
@"NH",	@"Vanuatu",                         
@"VE",	@"Venezuela",                       
@"VM",	@"Vietnam",                         
@"VI",	@"Virgin Islands, British",         
@"VQ",	@"Virgin Islands, U.S.",            
@"WF",	@"Wallis and Futuna",               
@"WE",	@"West Bank",                       
@"WI",	@"Western Sahara",                  
@"YM",	@"Yemen",                           
@"ZA",	@"Zambia",                          
@"ZI",	@"Zimbabwe", 

Written by Clay

November 30, 2012 at 13:50

Posted in Cocoa, Objective-C, Resource

Obj-C Voronoi library

with 6 comments

I ported Raymond Hill’s excellent Javascript-Voronoi library to Objective-C. You can find it on github here:


Written by Clay

March 28, 2012 at 21:08

Processing: HTML table monster concept

leave a comment »

I was working on a website for a client the other day when I sketched out a table that I needed to code in ERB / HTML. I doodled a bit, filling it in with patterns for the various header levels. I liked the appearance and decided to write a Processing script that would generate similar doodles.

Here is the original


Here’s an example of the output from the script:

Read the rest of this entry »

Written by Clay

February 21, 2012 at 19:29

Posted in Code, Processing

Tagged with ,