Javascript: Toggle Visibility of Elements

March 9, 2012

Late-night coding sessions allowed me to whip up this quick, reusable snippet.

//Toggle visibility of a given element, specified in the "id" parameter
function toggleNav(id) {
var x = document.getElementById(id).style.display;
if(x == "") {
document.getElementById(id).style.display = 'block';
return false;
else if(x == "block") {
document.getElementById(id).style.display = '';
return false;

This function checks the current value of the display style of the element specified by “id”, then sets it accordingly, i.e., if it is set to “none” (an empty string seems to be JS’s expectations), it sets it into “block” and vice-versa.

You may attach this to an onclick event to a link and set the id parameter to a <div> that has the ‘display: none’ style property. Feel free to reuse this, and/or expand on this code. 🙂

(Of course, this could be done with less effort using jQuery, am I right?)


C++: Convert a “char” digit into an “int” digit

February 9, 2012

Nothing special. It’s just a one-line function that builds on the concept that chars are, well, also integers. To be more specific, they are integer ASCII codes.

//returns an int from a number literal
int castInt(char src) {
return src - 48;

According to this, the digits from 0-9 have codes from 0x30 to 0x39 in hex, which is 48 to 57 in decimal. What were doing is subtracting 48 from the supplied char parameter’s ASCII code to obtain a digit from 0-9.

Note that this code was intended to be a simple and quick fix and as such has many loopholes and flaws, one of which is the inability to check if the supplied parameter was actually a digit or not. Nevertheless, it could be useful in small programs, such as console prompts (“Press 1 to continue, 2 to exit…”);

Well, that’s a load of text for one C++ function.


Artwork redux

October 28, 2011

Out of seeing for myself how I’ve improved, I picked up an old piece (2007-ish) of Photoshop work (a forum signature) and see what would happen if I remade it. Results here:


Original image.

Original image.


Redone image.

Redone image.

Well, there’s still room for improvement. 🙂


(Also, I use CS3 now.)


How solving for Pi works.

October 24, 2011

So did you download my Pi program? Or you just want to know some nerd algorithms? Well, read on.

How does one go about solving for pi? Well first of all let’s define some stuff to make things easier.

Let’s discuss what pi is first. Pi (every iteration of pi makes me mistype ‘pie’. Ah, typing while hungry.) is a number which relates the dimensions of a square to a circle, for instance a circle inscribed within a square. OK, non-geometry and non-math folks can close the window now.

A visual representation of the inscribed circle

A visual representation of the inscribed circle

Establishing that fact, and using the inscribed circle as a point of reference, we now enumerate some formulas related to the two shapes.

Given that a radius of a circle is equal to r, we can assume that the side length of the outer square is 2r. Thus, the area of the circle can be represented as πr2, and the area of the square as 4r2, from the definition that its side is 2r.

Using what we have established so far, we can define a ratio, specifically the ratio between the area of the circle to the area of the square. Let’s define A as the area of the circle and B as the area of the square. With this, we can define such an equation:


Notice that we can actually cancel r2 from the equation, which leaves us with:


Rewriting and transposing some variables nets us with this:

π = 4 * (A/B)

This then forms the basis of the algorithm used in the C++ program I wrote for solving Pi. The working algorithm can be seen in the source file included with the download. As you can see by studying the code, I have the algorithm coded right, but I can’t find the right values to use, hence approximation by random number generation and incrementing is implemented. In future versions I intend to rectify this.

So there you have it. That’s my approach to solving for pi. Comments, feedbacks and discussions on the topic are strongly encouraged, so comment away! 🙂

Further Reading (for the obsessed mathematicians):


Let’s Solve For Pi!

October 24, 2011

I told myself I’d at least have one new post during this one-month break. Here goes…

I dug up one of my old programs. When I say old, I mean it’s the very first (useful) program I finished. Er, one that I actually finished. When I was young (sophomore high school, to be exact) I usually got bored and never finished any of my programming projects. 😀 (Awsm RPG comes to mind… :|)

Anyways, looking how it worked, it seemed pretty neat, though it seemed improper for this program to be written in a game development software. That’s right– I wrote this old one in Game Maker 6 if I recall, all using its scripting capabilities. That goes to show how powerful GML is, or how patient I was with an interpreted language. Either way, that makes it cool(er), doesn’t it?

So I took some time today to rewrite and refactor the program, mainly to move it into a new programming language, C++, and to simplify it. Though the program now sucks because its interface has been reduced to a command-line one, I say it’d be better this way. Without all the Game Maker code bogging it, I have more freedom to expand the program to however I want it. And also I’m more familiar with high-level languages like C++ anyways. So rewriting for simplification and for refactoring.

The premise of the program isn’t too hard to comprehend. It’s a simple looping program with an algorithm solving for Pi. Yes, Pi, as in 3.14159265358979BLAHBLAH. To avoid deviating away from the topic at hand, I’ll write all about solving Pi (and the related algorithms) in another post, for those of you who’d find the discussion too boring. (If this is too boring for you, then why are you reading my blog in the first place?)

So I’m uploading the two programs, the old Game Maker one, and the C++ one. Also I’ll include the source for anyone who’s interested, though to fully understand it you’d have to read my article on Pi. Here ya go.


Rusty skills.

October 10, 2011


After what seems to be half a year, I finally get some free time to blog again, and redo and revisit some hobbies. It was mostly studies getting in the way, but one has to know his priorities.

Now that I have a decent, one-month break, that should be some ample time to repopulate my blog with real content (how many times have I said this), improve my programming skills (really saturated with college programming so I admit I’ve somewhat lost interest in hobby programming, i.e., the stuff they don’t teach you in class), maybe finally rebuild my website, play Minecraft (LOL), etc.

See you around. 🙂


jQuery – Cookie Manipulation

May 17, 2011

Okay, you can call me the web designer who has been living under a rock (wow, now that I think about it this must hold true for some. :P) but I’ve only harnessed the power of jQuery recently (on a website I’m working on). I was reading up the documentation until I couldn’t find something I was looking for: setting cookies via jQuery.

Since I want the code to be organized I was thinking of doing everything Javascript via jQuery anyway (hey, it’s write less, do more. ;)) For those of you who can’t find it, there’s a cookie plugin for jQuery available at their repository. Click here. Kudos to the author, carhartl, as this was the exact thing I’m looking for. Everything can be done, reading, setting and deleting cookies, via a single $.cookie() function.

Hey, at least I have content for today. 😀 Stay tuned for I may have more stories to tell about my jQuery escapades. Or whatever.

%d bloggers like this: