The Pursuit of More

Hey! I wanted to let you know about a project I’ve begun. It’s going to take some time, maybe a year or seven, but I am really excited about it. clayjones.me is a personal blog, for code snippets and updates on Clay. I’ve been juggling the idea of taking it in an entrepreneurial direction I’ve always wanted, but clayjones.me wasn’t the place for that. I have a deep interest in how to get more time, do more things, but not sacrifice sanity or happiness. So I’m going to start writing about that.

I’ve called it The Pursuit of More, and there is more info over there. It’s definitely a work in progress, but it’s something I’ve been wanting to do for a while.

This also means that there might be more things here on clayjones.me. Now that I have a divide in personal and non-personal realms, I can post more personal stuff on here!

So that’s that. I probably wont post anything on The Pursuit of More for a few months. There is a lot of ground work to get done. I need to fix the layout. I need to hone my writing craft, get some posts ready before going live. From an entrepreneurial point of view, I’m taking this pretty seriously. It will be fun if it succeeds or fails. I just hope I stick to it for a little while.

That being said, this website is to make money. I’m not going to lie. However, don’t expect to make money off of it for at least 10 months. I’d rather build a small following and provide them actual value for money than litter the layout with Adsense and make $0.06 a day. This isn’t a get rich quick scheme or get rich ever scheme. It’s an entrepreneurial attempt at giving people stuff they like. It’s something to work on in the morning and weekends. I hope you’ll check it out.

You can go check it out, and hit up the mailing list if you’d like. Then you’ll get updates when things start happening.

Thanks :)

Clay

Type Less in the Console

There exist lengthy console commands that need to be typed over and over. Let’s take 5 minutes and shorten those commands using a few Bash (Terminal) tricks.

There are a few different cases that we will look at:

  1. Run a command from anywhere.
  2. Run a very specific command that is temporarily useful.

Let’s get to it. So we want to run a command from anywhere. There is a useful file, located at ~/.bash_profile that will run every time a Terminal window is opened. We can define functions here, and use them all over! Let’s see how to do this. To edit the file, I’ll use vim ~/.bash_profile. A quick function I use a lot is one I call mkcd, that simply creates a directory and then changes into that directory. It is mdkir foo enter cd foo all in one line. To construct such a function, all we need to put into ~/.bash_profile is the following.

mkcd() {
mkdir -p "$*"
cd "$*"
}

The -p flag creates intermediate directories, so you could type mkcd /a/very/deep/path/to/a/folder, and 7 directories would actually be created, and you’d be in the right place. The "$*" symbol holds all arguments to our mkcd function, so it simply inserts what we type after mkcd right there. Simple, eh?

A few others that I use multiple times a day are simply shortcuts to a long ssh and sftp login that I use for school. This let’s me type the function name, and the function body gets executed. Saves quite a bit of typing. (It’s still password protected). I use “sc” as an abbreviation for school. These also go in ~/.bash_profile.

sshsc() {
ssh cjones52@unix3.csc.calpoly.edu
}
sftpsc() {
sftp cjones52@unix3.csc.calpoly.edu
}

An example of the second case is when I need to compile and run some program that I’m working on, and need to type this multiple times:

javac *.java
java SomeClassName

or this, which I probably typed thousands of time in CPE101.

gcc -ansi -Wall -pedantic someFile.c someOtherFile.c -o output
./output

These will only be used for a couple of days, have very specific arguments, so I don’t really want to make a full function that will be around forever. Instead I can create a local bash script, and throw the commands in there. Then I can run the command in 6 or 7 keystrokes, saving quite a bit of time. It’s really that simple. Using vim, or any text editor, we can make a file called comp (for compile) that contains these commands and run it with sh comp.

Within comp:

gcc -ansi -Wall -pedantic someFile.c someOtherFile.c -o output
./output

Then, from in the directory that has somefile.c and someOtherFile.c we type

sh comp

and everything happens. Yay!

Baselines

In a good study, you needs baselines to tell you if you have actually caused change. I’ve been doing a little research into self knowledge through numbers. (An eloquent way of putting it, via Quantified Self.)

The idea is to track various metrics about yourself, change something, and see what other metrics change.

When I stumbled upon the idea, it was very appealing for a few reasons. I like numbers, I like graphs, I like numerical analysis, I like myself, and I like making myself better. Science is making pretty good progress in a variety of fields, and numbers are used everywhere in science. It’s almost like “the science of x” is just “quantified x”. So the science of Clay Jones is quantified Clay Jones. Call be selfish, but I’m pretty interested in the science of Clay Jones.

Another proponent of finally taking the time to quantify my habits was what I saw with running. For a few years I’ve tracked my running habits, and I’ve had a positive experience with it. Sometimes I don’t run for months, but when I do, I always get back into tracking them. It validates my runs and keeps me honest when I skip a month (or year).

I also decided to see what would happen to my body if I quit meat for a week. Since last Tuesday, the only meat I have consumed was one hamburger and one chicken sandwich. Both were in social situations, but that was part of the experiment. I was testing my internal willpower, and failure to adhere to a new diet is a data point on willpower. I failed at being vegetarian, but the experiment was a success. The flaw, however, was a lack of numbers. I often felt hungry, or perhaps tired that I thought could be attributed to a lack of protein, but I never took the time to do any real numerical analysis. That is why I am now quantifying my habits.

Tracking these numbers on yourself is especially useful when you want to create a change in your life. For example, I have been thinking about the idea of drastically reducing my caffeine intake. I worry my brain will be useless, I will slog around, fail all my tests, drop out, and have to fall back on my taco business. Having tangible data will help me analyze these changes. I’ve also wanted to avoid white breads, avoid dairy, avoid meat, just to see what happens to my body. I can’t stop eating all of these things at once, because in the event that I notice some small benefit, I won’t know which caused it.

For now, I am going to not change much, but start tracking some numbers just to set a baseline. I have a small paper chart that I carry around. I’ve found physical things make me stick to something, rather than a phone application. I then put them into the website Chart Myself. It may seem like a timely process, but it only takes a few minutes a day and keeps me conscious of my choices and my body.

If you’re curious, here are the things I’m looking at for the next couple weeks. These are pretty specific to me.

  • Physical Vitality
  • Intellectual Vitality
  • Mood
  • Pulse (BPM, average late morning and early evening)
  • Acne
  • Canker sores
  • Anger
  • Anxiety
  • Appetite (Cravings)
  • Sleep duration
  • Stress
  • Study duration
  • Nap
  • Exercise duration
  • Caffeine
  • B12
  • Melatonin
  • Vitamin D
  • Meditation
  • Programming Duration (time spent on things not school related, those would go to study duration)

Mornings

I love mornings. I love relaxing for the first hour of every day and I love slowly going about my morning routine.

Around 7:30 this morning I had a brief conversation with the dude making my breakfast sandwich (It was delish).

Him: “How’s your schedule today?”
Me: “Good, no class until 2. How’s yours?”
Him: “Oh that’s awesome. I have class at 12.”
Him: “Wait, if you don’t have class until 2 why are you up??”

For me, mornings are void of interruption and contain hours of peak energy. I simply get a lot done. If I were to shift my sleep schedule 4 hours later, sleeping 2am to 10am (as student-culture often promotes), I would get less done. I know hours after 9pm aren’t productive for me. Waking up early also sets a peaceful standard for your day because you don’t rush through your morning routine.

We all know the scenario all to well. Alarm, snooze, alarm, snooze, alarm, snooze… Alarm, oh dear I need to be somewhere in 20 minutes. RUSH RUSH RUSH. We’ve all been there! I hate that feeling with excessive passion.

If this sounds like you, I ask you to give peaceful mornings a try. Still get 8 hours of sleep (let’s do the math real quick… yeah, 10pm bedtime), but get up at 6 one day and do something you’ve been meaning to do. I like to read or work on a project, usually programming. It’s really peaceful and sets a peaceful standard for the day. That’s what it’s really about.

Yo, you! Think I’m dumb? Think I’m silly? Think I’m awesome? (yeah) Tweet me! Let me know what you’re thinking!

Simple UIView Vertical Scroll Animation

The iPhone’s keyboard takes up a whopping 216px, and sometimes text fields are positioned towards the bottom. When the keyboard appears, they are hidden.

This article shows how to add a scroll animation to your UIView that scrolls up when the text field is active, and scrolls back to normal when the text field is no longer active.

Note that the keyboards have custom toolbars, which you can learn about here. The “Done” button dismisses the keyboard.

I have defined two values for readability.

static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat ANIMATE_VIEW_PADDING = 10;

First, let’s write a generalized method that animates to any Y value:

- (void) animateViewToYValue: (float) pixels {
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y = pixels;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:.3];
    [UIView setAnimationDelay:0];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];
    NSLog(@"frame origin y %f",self.view.frame.origin.y);
}

On line 1. we copy the existing frame of the view, and on line 2. we change the y value of the origin component to that frame. We now have a frame to animate to with all values exactly like the original frame except the value we are animating (the y value). Line 4 tells the system that we are now going to specify animations to perform. Notice this is a class method to UIView, not to the instance you’re working with. Next, we set some simple values regarding the animation. Then we change the frame of our view to the one we altered earlier, and then commit the animations. I am also logging a message for debug purposes.

So that gives us a generalized animation, but what values do I pass it? Let’s write another helper method, -(void) animateViewToShow: (CGRect) frame; that we can pass a frame, and that method will calculate the value to animate to.

- (void) animateViewToShow: (CGRect) frame {
    float bottom = frame.origin.y + frame.size.height + ANIMATE_VIEW_PADDING;
    float frameKeyboardDifference = bottom - PORTRAIT_KEYBOARD_HEIGHT;
    [self animateViewToYValue:-frameKeyboardDifference];
}

On line 2. we calculate the bottom of the frame, because the x,y, coordinate system begins at the upper-left corner. A little padding is added for aesthetics. On line 3. we find the difference between the frame and the keyboard, because this is the amount we will actually want to move! If the text field is 40 pixels below the top of the keyboard, we want to move up 40 px. Finally, on line 4. we can call the method we wrote earlier. However, notice that it is negative. This is because the coordinate system begins at the upper left hand corner, and a negative y coordinate is actually above the origin.

This sets up our animation, but when should we call this method? Well, if our current class is the delegate of the text fields, we will get a call to -(void)textFieldDidBeginEditing:(UITextField *)textField; when any text field begins editing. Here is the code I use:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField.frame.origin.y > (PORTRAIT_KEYBOARD_HEIGHT - 
        textField.frame.size.height - ANIMATE_VIEW_PADDING)) {
    [self animateViewToShow:textField.frame];
    }
}

On line 2, we check if the text field will be obscured by the keyboard. We just need to check if the keyboard height minus the height of the text field, minus a little padding (so it will move if the text field is within 10px of the keyboard) is less than the position of the text field. In the example, you’ll notice that the top text field doesn’t move, which is what we want. Then, we can send the frame to our helper method, which then calls the animation.

The last thing to do is to animate back to 0px whenever the text field is no longer in use. In the example project it happens when the “Done” button is pressed. Pretty simple!

Here is the example Xcode project if you want to check it out!

Simple UIToolbar as Keyboard Accessory

UITextField has a property called inputAccessoryView that can be set to a UIToolbar to give the user something to do after inputting text.

I have already set up text field as a synthesized property of my view controller, called nameTextField, and linked it to by nib.

viewDidLoad is a good place to create this toolbar, and I’ll call it keyboardToolBar.

UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
keyboardToolBar.barStyle = UIBarStyleBlackTranslucent;

This gives us a black, translucent UIToolbar that is 320 x 50. We can set the items by passing an array of UIBarButtonItem to [keyboardToolBar setItems:(NSArray*)]. The constructor of a UIBarButtonItem that I am using [[UIBarButtonItem alloc] initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action];

UIBarButtonItemStyle are predefined styles you can use, I suggest you browse through the options. The target is the object to send action (a method/message) to, which is triggered when the button is pressed. You need to implement these methods to avoid a crash.

I have decided on three buttons, and a spacer in between. The spacer is simply a UIBarButtonItem initialized with [UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];. This pushes all previous buttons to be left aligned and all buttons after it to be right aligned.

All together we have:

[keyboardToolBar setItems: [NSArray arrayWithObjects:[[UIBarButtonItem
    alloc]initWithTitle:@"Cancel"style:UIBarButtonItemStyleBordered target:self
    action:@selector(cancelNumberPad)],
    [[UIBarButtonItem alloc]initWithBarButtonSystemItem:
    UIBarButtonSystemItemFlexibleSpace
    target:nil action:nil],
    [[UIBarButtonItem alloc] initWithTitle:@"Next"
    style:UIBarButtonItemStyleBordered target:self
    action:@selector(nextTextField)],
    [[UIBarButtonItem alloc]initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone target:self
    action:@selector(doneWithTextField)], nil]];

Finally, we make the connection from our text field to the new toolbar with

nameTextField.inputAccessoryView = keyboardToolBar;

You can grab the Xcode project here.

jQuery Weather Application

we1
This is a little app I built to learn jQuery. It’s pretty responsive but could still use a little work.

  • Pulls weather from a Yahoo API
  • Creates draggable clouds based on conditions, moving across the screen at current windspeed
  • Places the sun or moon along the right side of the screen based on time of day and time of sunset/sunrise
  • Fades screen color based on position of planet
  • Radial gradient of opaque to dark color originating at planet
  • Neon light emulating text at night time. Flicker animation. (This could be prettier, not a concern though)
  • Day cycle speed up – for looking at a full day cycle without wasting 24 hours of your day
  • Type a U.S. zip code into the text box and press enter, takes you there
  • Hurricane button – spanws a bunch of clouds and puts the wind speed high. Click a different location to leave the hurricane

Check it out here: jQuery Weather.

A few warnings: the clouds will prevent you from clicking on links at the bottom. They are draggable so just drag them away. I have tested this on Chrome, Firefox, and Safari, so use one of those. It seems to be pretty slow on Firefox, so use Chrome. Use Chrome regardless.

Banana GUI

I had a little time today and wanted to work on some Objective-C stuff. I had written this in C and accidentally deleted it, so I figured I’d redo it with a GUI in Objective-C.

The theme is the Infinite Monkey Theorem, which basically says that a monkey hitting random keys on a typewriter for an infinite amount of time will surely type a given text.
Luckily a computer can type random charachters very very (very) quickly. Still though, the worst case time of this algorithm is 26^n where n is the number of letters in the string. This only takes the lowercase a-z.

You just hand it some input, and it will take generate random strings until it finds it.

Short words come very quickly
bg22
But longer words a little more. (The gui is broken, I know.)
bg3
bg4

Some future updates I want to implement might be: Flash each generated word to the GUI – unforunately this requires multiple threads.

Interest

It was somewhat random for me to become interested in computing. At the time I wouldn’t have even been aware enough of the industry to say what I’m interested in. I might have said I really like Python and I really like making computers do stuff. At 16, I realized that I couldn’t live off playing Call of Duty, but maybe I could live off of making computers do cool stuff. I hadn’t considered the money beyond that.

When I tell people I’m in computer science, some of them will bring up money. Other CS majors probably told them that they got into it for money.

(These assumptions are mostly directed at just first year students, but definitely not all first year students.)

The people in computing statistically aren’t going to make the money that doctors or lawyers or high profile financial and business professionals will make. If your passion is money, and you’re willing to work 20 hours a day for it, I advise you to take one of those paths. Computer science ends up with some people who don’t want to put in the work to become one of those professions, but they are focused on money enough that they decided computing had a favorable effort-to-earning ratio.

I have noticed in industry that a lot of software engineers are actually pretty interested in what they’re doing. I’m excited to see when the light switch goes off in peoples head, because a lot of my peers aren’t excited about what they’ve chosen to do.

Every once in a while, I overhear a common theme in one of my computing or programming classes, “I don’t care why it works, as long as it works.” or “Just fix it for me.” or “Let me see your code.” That’s suicide in computing, or any technical field. You might still get a job, make 80k a year until you’re 60, and die. But it’s suicide of any attempt at doing something you’re genuinely passionate about. It’s not going to foster interest. You would never hear somebody write an effective piece of prose or poetry and say “I’m not sure why this was effective, but it was. Cool, let’s move on.” They would dive deeper, beyond “school requirements”, just to get better at something they love.

Some people ended up in a technical industry because they wanted to make money, their parents told them to, or they thought it might be fun. But some, rather, were fortunate enough to have had the opportunity to explore the field as a teenager, and made a confident choice to spend the next 60 years with it.

That’s why I feel fortunate. I knew I liked it. And I like it now. I enjoy going to class and I enjoy doing homework.

Sometimes I end up sort of tutoring other students, meaning I’ll offer to study with them because I know they’re failing, and I know teaching it to them will help me remember. It’s selfish but not selfish. Anyways, sometimes I’ll ask if they went over practice problems, read the chapters, whatever. When they say no, I ask why. The responses range from “I didn’t want to”, “I forgot”, “I was busy” to “It was my friends 19th birthday so we got drunk” (Seriously? It was a Tuesday!). Regardless, I like to remind them (brutally enough) that this is what they’ll be thinking about at work for the next 40 years, and if they really, honestly dislike the work, they should change majors.

The last thing I’m going to briefly discuss is the weekend. It blows my mind how many people (in the US) think weekends should be without productive work. If you have a goal, and I think most people come to college with a goal of getting better at something, you should always be working on that goal in some way. Why is it strange to spend weekends doing school work, or extracurricular programming/writing/sculpting/dancing/reading about architecture. I appreciate balance. You’d probably die if you read programming and math books for every waking hour of your life. I realize people can have multiple interests, that’s good. Relaxing is good. I am just frustrated at the amount of wasted time I see around me, especially on a college campus. If you want to do something, do it, and do it a lot. This is something that everyone can benefit from.

Project: Ray Casting In C

For my CSC 101 course, we built a ray caster to create images from numerical input of spheres. It translates a viewport to a image depending on the size of the output. We used a format called PPM3 for it’s simplicity. PPM3 is just a header and rgb integer values in a predefined scale for every pixel in the image. The files end up being huge, but the images are super simple to make. A 1024 x 768 image was around 10mb. The images below are png screen captures. Here is a zip of the source ray_caster.zip. It’s pretty convoluted, but if you have any questions feel free to ask.

The input is a file of “spheres”, represented by a string of numbers, in a specified order. They happen to go x, y, z, radius, color(r, g, b) finish (ambience, diffusion, specular, roughness). You can optionally input data on the light source, viewport, eye, and ambience.

A file like this
rc0
Would become this
rc2
Or after some command line arguments to move the light source, it would be this

He provided us with some silly files of numerical representations of thousands of spheres for us to generate with our programs images like these two. Although not as beautiful, this is actually similar to the way companies like Pixar generate their images. They use millions of tiny triangles and ours are thousands of tiny spheres.
rc3

rc6

A few more images, messing around with the ambience and such. The background is just a giant sphere pushed far back. It create a nice place for a shadow to land.
rc4

This one is my favorite.

rc7
I’m not sure why this sphere looks like Hey Arnold. I need to look back through and see what was wrong. It could also be from the viewport and “eye” not aligning right.
rc5