TIL global variables in javascript are stored in the window object.

ellievsbear
I'd rather be in outer space 🛸
Peter Solarz
Monterey Bay Aquarium
"I'm Dorothy Gale from Kansas"

Discoholic 🪩

JBB: An Artblog!
Stranger Things
Xuebing Du

Love Begins
Misplaced Lens Cap
d e v o n

tannertan36
Cosimo Galluzzi

titsay

祝日 / Permanent Vacation

roma★
occasionally subtle
seen from China

seen from United Kingdom
seen from United States

seen from Brazil
seen from Austria
seen from United States
seen from United States

seen from United States

seen from Indonesia
seen from United States

seen from United States

seen from Türkiye

seen from Malaysia

seen from Malaysia
seen from Brazil

seen from United States
seen from Peru

seen from Switzerland

seen from Malaysia

seen from United States
@blufiro
TIL global variables in javascript are stored in the window object.

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Running a Github Ruby on Rails instance on OSX
I recently collaborated with a friend to build a Web App in a Hackathon. We used GitHub as our version control for the server source files which is written in Ruby on Rails and Heroku for the actual production app.
For the Hackathon, I contributed mainly in html and css but due to time constraints, I was not able to set up a local test environment of Ruby on Rails on my machine. To make matters worse, I'm completely new to Ruby and Rails and Postgre, so we decided to test directly on Heroku for time frame of the hackathon.
Now that the hackathon is over, I've had some time to actually sit down and figure out how to install everything on OSX. In particular, how to set up postgre and use it with the GitHub Ruby on Rails files.
Start the Ruby on Rails server
After installing Ruby, Rails and Postgre, I wanted to run the server to test. Firstly, open a Terminal and navigate to the directory of the GitHub Repository:
cd [GitHub Repo]
start the server:
rails s
which is short (and equivalent) for:
rails server
Stop the server
I also quickly learned to stop the server is simple to stop the process. So use Ctrl+C in your terminal to stop it.
DB Error?
Upon accessing the localhost page (typically http://localhost:3000), I encountered this error: "Could not load database configuration. No such file - /Users/../config/database.yml"
I found that this file 'database.yml' was a settings file for the server to user to connect to the database. In my case, I was using Postgres, so I copied this template and edited the values accordingly and saved the file into the config directory:
development: adapter: postgresql encoding: utf8 database: db_name username: user_name password: db_password host: localhost test: adapter: postgresql encoding: utf8 database: db_name username: user_name password: my_db_password host: localhost
For mysql, check this.
The reason is the file may not be uploaded to GitHub by your collaborators is because it contains the user and password of their local database. So one alternative is to change the yml contents to use environment variables like so that you are able to upload the yml file:
password: <%= ENV['APPNAME_PROD_PWD'] %>
Once the file is in place in the config directory, double check your database that you have created the user and the database with the name specified in this database.yml file so that Ruby on Rails server can communicate with it.
Setup the database from Schema
Finally, the last step is to set up the database based on the schema from your repository. To do this, run this from terminal:
bundle exec rake db:setup bundle exec rake db:migrate
This command is explained further here and here. Essentially, it will use the schema from the GitHub repository to set up the database on your local machine, then potentially fill (seed) it with some values, and finally, if there are any migrations, it should run those. To quote from the rails guide, "You can think of each migration as being a new 'version' of the database. A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries."
Setting up Postgres
As for setting up Postgres database itself, I found this video tutorial very concise and useful.
Conclusion
Hope this will help newbies to Ruby and Rails be able to run their own instance on their local machines when they clone a Ruby on Rails repository from GitHub!
Spent the weekend at a hackathon organised by UPSingapore with the National Environmental Agency (http://cgs.sg/hackathon/about/). Was quite interested in the environmental themes. However, the data-sets seemed to consist mostly of Excel spreadsheets of locations or past measurements. Only the...
I worked with Ming Ding to create a Web App for people to give away their unwanted items. Here are his thoughts!
I haven't been a big fan of component game object architecture. Checking out functional reactive programming. It sounds very powerful. I do prefer a more declarative approach.
http://lambdor.net/?p=171 http://stackoverflow.com/questions/1901251/component-based-game-engine-design
How to enable NTFS write support in Snow Leopard - Sharing files between Macs & PCs
UPDATE: exFAT [aka FAT64] has since been released and is fully supported by:
-Snow Leopard 10.6.5 & up
-Windows XP/Vista after an update
-Window 7
It’s faster than FAT32 and can handle much larger file sizes and capacities. It’s probably the simpler option vs NTFS now. Soon it will be the default format used on memory cards and small USB drives and this will all be behind us. Larger drives will probably still come with NTFS, HFS etc. though; so you may want to reformat it with exFAT/FAT64 if you want to share it between Mac OS X & Windows.
More info at http://en.wikipedia.org/wiki/ExFAT
-
ORIGINAL POST:
So now that I have switched to a Mac, I discovered that Macs use a different disk format system than Windows uses. Windows uses what is called NTFS (New Technology File System). So to transfer files between a Mac and a Windows PC (such as friend’s PC), most folks use an external drive that that uses yet another format called FAT32 that can be read by nearly any computer. But it’s an old, slow and limited format that can’t handle any files larger than 4 GB. Screw that.
So I found a better alternative. Just format an external drive as NTFS and flip a hidden switch that enables Snow Leopard to write to NTFS. NTFS write support was apparently dropped in the final release build of Snow Leopard. It turns out the support is still there, but disabled by default.
This way, (as opposed to FAT32) -
File transfers are faster.
Files can be as big as you wish.
Any Windows PC will be able to read it and write to it.
Any Mac will be able to read it.
In case you’re confused there, you should know that Macs can “read” NTFS out of the box. It’s “writing” that we’re going to enable here.
So the only limitations with this approach would be that -
You would have to use this trick explained below if you want to use this same drive to bring to another Mac and write to it and bring the files back home with you (remember this trick only works for Snow Leopard though).
You NEED to always safely “eject” the drive to ensure smooth operation. This will be explained more at the end.
Read More
I'm still on Snow Leopard and writing to NTFS is disabled. Luckily this worked for me.

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Might be handy for HTML5 preloading?
Lots of high-res images can really spruce up a Web site. But they can also slow it down-- images are files, files use bandwidth, and bandwidth is directly related to wait times. It's time you get yourself an education on how to speed things up with a little trick called image preloading.
Trails Effects in Starling
I started working on trail effects! I've always been mesmerized by them in Japanese games, and perhaps because of using Unity3D recently for a jam, I came across this effect but I've never yet written the code for this effect, so I wanted to try and build one for my own games.
What is a Trail?
A trail is a series of polygons that follow your cursor or object around and leaves some marks or lines behind. Unlike particle trails however, the one I set out to make are made of geometry instead of just drawing a particle at the position.
Quads Vs Triangles?
Since I was focusing on Starling, I decided to keep the trail in quads rather than triangles, the reason being quads could be batched better in Starling.
It would be also easier to texture quads than to texture individual triangles
It is not obvious how to maintain a width for the trail if we were dealing with triangles.
Easy-Peasy-Repeat
A trail code loop sounds simple enough to do, here's the pseudocode:
//set the head of the trail update(quads[0]); for(int i=1; i < quads.length; i++) quads[i].copyFrom(quads[i-1]);
Done? I thought it'd be easy to do as well but the proper trail effect took me a few full days over the course of a week to actually finish. In this post, I'll be documenting my mistakes and progress for this effect.
First Mistake
What's wrong with the code above?
The problem is that the iteration is done from 1 to n, where 0 is the head. Head here is defined as the most recent trail position recorded, which is where the object should be at.
So the iteration should start from the back instead, otherwise, the entries will not be copied in the correct order. We also want to set the head last because of this reason.
for(int i=quads.length-1; i >= 1; i--) quads[i].copyFrom(quads[i-1]); //set the head of the trail update(quads[0]);
How to update the Quad?
I did several iterations to get this right and learnt a fair bit along the way. I believe some of these methods could be used on purpose to achieve other kinds of trail effects. In fact you can come up with your own algorithm!
Extending Closest Edge At first, I was thinking that quads should be updated by extending the vertice of closest side of the quad towards the direction. Then each quad vertex is simply copied to the previous quad every update. This resulted in a really cool but unintended effect. There were holes and several disconnected parts due to twisting of the quads.
Rotating the Quad and stretching it So every time a new position is set for the trail, we rotate so the quad edge is a tangent to the direction of the new position and we stretch the quad from the previous position to the new position. This worked relatively well but ended up becoming very blocky and quads did not connect with each other.
Rotating and extending the Top Edge only After trying the rotating quad approach, it became obvious that I should not rotate the bottom vertices as well, so I only moved and rotated the top edge and this resulted in a very nice trail effect. However, one small problem was that the trail would still overlap when turn sharp corners. This was the result of the tangent crossing over the previous quad's space.
Rotating Top Edge but snap vertices if crossed previous space So finally, I managed to solve the problem with the sharp corners by first checking if the tangent crosses the space and that is done by calculating the: dotDir = dot product of direction and the previous direction; dotPos = dot product of the vertex relative to the previous position and the previous direction;
After which, we check if (dotDir * dotPos < 0) because if they have opposite signs, it means that they are on different sides of the quad and would mean the vertex crossed the space. If this condition is true, we snap it back to the previous quad vertex position to avoid the crossover.
Gradients
A trail of 5 quads colored according to a gradient from white to black.
Naturally, one of the important features is to add a gradient to the trail.
Updating Colors While coding the trails. I was originally thinking that I could get away with just setting the color upon initialization and not have to update it. Because I was stretching the quads every time a new position is added to the trail, this resulted in a non-uniform gradient that might not be intended. As such, updating the colors for every vertex is required. However, this introduces another problem.
Gouraud shading / Sampling issue One problem that this current system faces at the moment is the same problem that gouraud shading has and that is that if there are not enough quads in the trail, the color could be diluted because we will miss the position of the color we are looking for. However, since trails are very fast effects, and the color is being updated, we can get away with it for now (until an artist comes and complains to me!).
Optimization As an optimizaiton, the top vertices of a previous quad will have the same color as the bottom vertices of the current quad. So color only has to be computed 2n times in a chain of n quads, rather 4n times for each quad vertex.
Texturing
After coloring a trail, the next most obvious feature is to be able to texture it. With texturing, we can achieve many effects. Here are some:
One side gradient texture
Middle gradient texture
Trail Width
Trails also have a certain width that is associated with them and this would affect the tangent calculation mentioned in the update step.
One further feature I implemented was to allow the trails to taper off.
I originally wanted the tapering feature to be able to set exactly how many pixels to taper to, or essentially a start and end trail width, and linearly shrink the width across the quads. However, I did not do this because it would require me to store the tangents and recalculate the width of each quad every update.
I decided that this was too expensive a calculation for such a simple feature, so I went with a multiplier instead. So instead of calculate an exact length, I just multiplied the tangent width by the taper multiple (e.g. 0.9) each update to keep things simple for now.
It is entirely possible to recalculate and update each quad but perhaps I will do that as a separate implementation so as to keep the simple trail features cheap and fast.
Future Additions
One possible addition could be to change the vertices into splines instead. That would allow more vertices to be generated and allow the curvature of the geometry to be smoother.
If there are any suggestions on how I can improve this trails, do let me know.
Unity3d Unlit Tint Shader
One of my friends was having a problem with transparency and tinting of 2D sprites in Unity3d, so I decided to help him out. The problem was that there did not seem to be a 2D unlit shader which had tint support built-in, or at least it was mostly confusing as to which shader he should have picked. We googled around a lot and did not find much help, noting that many people had chosen to write their own tint shaders too. So I also decided to write the shader for him.
Shader "Custom/Unlit/UnlitTint" { Properties { _MultColor ("Color Tint Mult", Color) = (1,1,1,1) _AddColor("Color Tint Add", Color) = (0,0,0,0) _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" } Category { Lighting Off ZWrite Off Cull back Blend SrcAlpha OneMinusSrcAlpha Tags {Queue=Transparent} SubShader { Pass { SetTexture [_MainTex] { ConstantColor [_MultColor] Combine Texture * constant } SetTexture [_MainTex] { ConstantColor [_AddColor] Combine previous + constant } } } } }
It was simple enough. The part that got me a little stuck was the lack of documentation on the combine instruction. It was not very clear whether there could be multiple constantColors declared in the SetTexture block or not. I wish there was though. At this point in time, I'm not sure if calling SetTexture twice (as seen above) has an additional cost rather than writing our own custom shader code and doing the multiplication and addition together. Tinting
Just a brief explanation on how tinting works.
Assume we have a pixel color. And here, color is defined as a set of [r,g,b,a] values each of which range from 0 to 1.
We could have gotten this color from geometry or a texture, it doesn't matter, just assume we have an input color from somewhere and we want to tint it, meaning we want to modify that color value. Here's the obvious way to do that. OutputColor = InputColor * TintColor
However, one of the problems with this is that you can only bring down the color value since you are only multiplying it from 0-1. That means if you start with a white input color, you can tint it to any color, if you start with a yellow input color, you can tint to any value between blue and green but not red. You cannot tint black color to anything. 0 * 1 = 0. Therefore, an offset is needed.
OutputColor = InputColor * TintColor + OffsetColor
Now, we can add color to the input! In other words, we can colorize (I think that's the photoshop term) black to any other color.
If you refer again to the shader code above, you can see that that's all I'm doing, just multiplying the input color which is from the texture and adding another color offset to it. These operations also support modifying of alphas since color has an alpha component.
Binary Search Trees practice
Started the practice with red black trees, but I figured I've forgotten how to code Binary Search Trees as well, so I might as well get my basics right.
The node structure and tree find and insertion were easy enough to do. Binary trees are easy to define. Every node has two children, nodes in the left sub-tree are smaller than itself, and nodes in the right sub-tree are greater than itself.
Node Structure
struct BinarySearchNode { int value; BinarySearchNode* left; BinarySearchNode* right; BinarySearchNode* parent; BinarySearchNode(int value_, BinarySearchNode* parent_ = nullptr) : value(value_) , left(nullptr) , right(nullptr) , parent(parent_) { } ~BinarySearchNode() { left = nullptr; right = nullptr; parent = nullptr; } };
Insertion and Find
class BinarySearchTree { private: BinarySearchNode* findRecurse(BinarySearchNode* node, int value) { if(node->value == value) return node; else if(node->left && node->value > value) return findRecurse(node->left, value); else if(node->right && node->value < value) return findRecurse(node->right, value); else return nullptr; } void insertRecurse(BinarySearchNode* node, int value) { if(value < node->value) if(node->left == nullptr) node->left = new BinarySearchNode(value, node); else insertRecurse(node->left, value); else //if(value > node->value){ if(node->right == nullptr) node->right = new BinarySearchNode(value, node); else insertRecurse(node->right, value); } public: BinarySearchNode* root; BinarySearchTree() : root(nullptr) { } BinarySearchNode* find(int value) { return findRecurse(root, value); } void insert(int value) { if(root == nullptr) root = new BinarySearchNode(value, nullptr); else insertRecurse(root, value); } };
However, the deletion got me a little stumped. Reading wikipedia, I learnt of the ingenious technique of swapping the in-order successor or predecessor node with a node that has two children. This is because the in-order successor or predecessor has only one child or zero children, by definition of the Binary Tree. Thus, it is easy to remove a leaf node or node with one child instead of messing around with the node ordering in the Binary Search Tree. Its no wonder I have forgotten about it.
void remove(BinarySearchNode* node) { // deleting node with two children if(node->left != nullptr && node->right != nullptr) { // have to choose either in-order successor node or in-order predecessor node to take position // in-order successor node is the left-most child in the right sub-tree // in-order predecessor node is the right-most child in the left sub-tree // in either case, node will have either 0 or 1 child. // for now we implement choosing in-order sucessor always // but this may lead to an unbalanced tree BinarySearchNode* newSuccessor = getInOrderSuccessor(node); // replace value of successor with self node->value = newSuccessor->value; // remove successor node remove(newSuccessor); } // deleting node with one child else if(node->left != nullptr || node->right != nullptr) { if(node->left != nullptr) { // deleting root node with one child if(node->parent == nullptr) root = node->left; else node->parent->left = node->left; // update parent of child node->left->parent = node->parent; delete node; } else { // deleting root node with one child if(node->parent == nullptr) root = node->right; else node->parent->right = node->right; // update parent of child node->right->parent = node->parent; delete node; } } // deleting a leaf else { if(node->parent->left == node) node->parent->left = nullptr; else node->parent->right = nullptr; delete node; } } BinarySearchNode* getInOrderSuccessor(BinarySearchNode* node) { if(!node || !node->right) return nullptr; BinarySearchNode* inOrderSuccessor = node->right; while(inOrderSuccessor->left) { inOrderSuccessor = inOrderSuccessor->left; } return inOrderSuccessor; } BinarySearchNode* getInOrderPredecessor(BinarySearchNode* node) { if(!node || !node->left) return nullptr; BinarySearchNode* inOrderPredecessor = node->left; while(inOrderPredecessor->right) { inOrderPredecessor = inOrderPredecessor->right; } return inOrderPredecessor; }
After I completed my binary search tree, I was thinking about how to iterate over it and was also curious about iteration of trees in general, especially without recursion or use of the stack. I researched some links (1), (2) and learnt that most iteration still requires some sort of stack.
Stack Iterator
class BinarySearchStackIterator { public: BinarySearchNode* lastSeenNode; BinarySearchTree* tree; vector<BinarySearchNode*> parentStack; BinarySearchStackIterator(BinarySearchTree* tree_) : tree(tree_) , lastSeenNode(nullptr) , parentStack() { parentStack.clear(); pushLeftChildren(tree->root); } BinarySearchNode* getNext() { // once the stack is empty, we are done if(parentStack.empty()) return nullptr; lastSeenNode = parentStack.at(parentStack.size()-1); parentStack.pop_back(); if(lastSeenNode->right) { pushLeftChildren(lastSeenNode->right); } return lastSeenNode; } private: void pushLeftChildren(BinarySearchNode* node) { while(node) { parentStack.push_back(node); node = node->left; } } };
Alternatively if you have a parent pointer in your node structure and if the iterator stores the lastSeenNode, it is also possible to iterate through the tree. I had a lot of trouble wrapping my head around this iteration. Essentially, we have to split it into a few cases:
Traversing to the left-most child of the root or a right child, i.e. the in-order sucessor of a node.
Traversing to the parent if we're coming from the left child upwards.
Traversing multiple parents if we're coming fom the right child, up to the point where the child is no longer a right child.
No stack iteration
class BinarySearchParentIterator { public: BinarySearchNode* lastSeenNode; BinarySearchNode* currNode; BinarySearchTree* tree; BinarySearchParentIterator(BinarySearchTree* tree_) : tree(tree_) , lastSeenNode(nullptr) , currNode(nullptr) { } BinarySearchNode* getNext() { // first access if(currNode == nullptr && lastSeenNode == nullptr) { currNode = leftMostChild(tree->root); } else if(currNode) { // get in-order successor if there is a right child that has not been seen if(currNode->right && lastSeenNode != currNode->right) { lastSeenNode = currNode; currNode = leftMostChild(currNode->right); } else if(currNode->parent) { // right sub tree is done // move up one level // if moving up from left node to parent if(currNode->parent->left == currNode) // && lastSeenNode != currNode->parent) { // just move to parent lastSeenNode = currNode; currNode = currNode->parent; } // else moving up from right node to parent else //if(currNode->parent->right == currNode) { lastSeenNode = currNode; // keep going up until you are not the right child while(currNode->parent && currNode == currNode->parent->right) { currNode = currNode->parent; } currNode = currNode->parent; // if currNode recurses all the way to root, it will terminate since root parent is nullptr } } else { currNode = nullptr; } } return currNode; } private: BinarySearchNode* leftMostChild(BinarySearchNode* node) { while(node->left) { node = node->left; } return node; } };
Trie Trees practice
It's my first attempt writing a trie tree. I read the wikipedia page and tried to understand it, the image on stuck to my mind and I tried to code it from my understanding of it.
After much time spent, I figured I didn't know enough and I searched online for more information. It turns out this image is more like a radix tree, which is a specific type of trie tree, where intermediate substrings can be grouped. I decided to back up a little and went back to basics to focus on just getting the trie tree right first before moving on to a radix tree implementation (in future).
As a small challenge, I tried to code the bitwise implementation instead of character implemention. The code below is what I came up with. It turns out to be easier than expected to code, since basic trie trees create a node for every element (be it a char or a bit) in a string.
At first, I assumed encoding a bit string had to be done from the Most Significant Bit (MSB) to Least Significant Bit (LSB), meaning more significant bits would appear higher in the Trie Tree. However, after much thought, I coded the structure to go from LSB to MSB, and the resultant tree seems to be more elegantly formed than if it were to be MSB to LSB.
While researching trie trees, I also learnt a lot about:
Deterministic acyclic finite state automaton (DAFSA), where basically child nodes can combine thus saving space but not allowing auxillary information to be stored.
Radix trees, where the nodes in a trie trees and be condensed, saving space, but adding more complexity to the insertion and deletion algorithms.
_BitScanReverse and other compiler Intrinsic functions, it's the first time I encountered these, specifically, there are fast low level functions to do various operations like count the leading zeros in an unsigned long.
#include <intrin.h> #include using namespace std; #pragma intrinsic(_BitScanReverse) typedef char int8_t; class TrieNode { public: TrieNode* children[2]; bool value; void* leafObject; private: int getIndex(int8_t val, int startIndex=0) { unsigned long index = 0; unsigned char isNonZero; if(startIndex > 0) { int8_t mask = (0xFF >> startIndex); val = val & mask; } isNonZero = _BitScanForward(&index, val); return index; } public: explicit TrieNode(bool value_) : value(value_) , leafObject(nullptr) { children[0] = nullptr; children[1] = nullptr; } virtual ~TrieNode() { for(int i=0; i <2; i++) { if(children[i] != nullptr) { delete children[i]; children[i] = nullptr; } } } bool hasChildren() { return (children[0] || children[1]); } bool isLeaf() { return leafObject != nullptr; } }; class TrieTree { public: TrieNode* root; static const int maskOne = 0x1; TrieTree() : root(new TrieNode(false)) { } void* find(int8_t key) { TrieNode* currNode = root; TrieNode* nextNode = currNode->children[ (key & maskOne) ]; while(nextNode != nullptr) { currNode = nextNode; key = key >> 1; nextNode = currNode->children[ (key & maskOne) ]; } if(currNode->isLeaf() && key == 0) return currNode->leafObject; else return nullptr; // not found } void insert(int8_t key, void* value) { if(value == nullptr) return; insert(root, key, value); } void remove(int8_t key) { remove(root, key); } void print() { printf("\n\ntree"); print(root, 0); } private: TrieNode* insert(TrieNode* node, int8_t key, void* value) { unsigned long index = 0; unsigned char isNonZero = _BitScanReverse(&index, key); // if end of string if(isNonZero == 0) { // node will become a leaf node->leafObject = value; return node; } else { // we still have more significant bits int nodeIndex = (key & maskOne); TrieNode* childNode = node->children[ nodeIndex ]; if(childNode == nullptr) { childNode = new TrieNode(static_cast(nodeIndex)); node->children[ nodeIndex ] = childNode; } return insert(childNode, key>>1, value); } }//end insert // returns true if we want to delete the child node that we recursed to bool remove(TrieNode* node, int8_t key) { unsigned long index = 0; unsigned char isNonZero = _BitScanReverse(&index, key); // if end of string if(isNonZero == 0) { // double check if node is a leaf if(node->isLeaf()) { node->leafObject = nullptr; return true; } } else { // we still have more significant bits int nodeIndex = (key & maskOne); TrieNode* childNode = node->children[ nodeIndex ]; if(childNode == nullptr) { // nothing to delete return false; } else { if(remove(childNode, key>>1)) { delete childNode; node->children[nodeIndex] = nullptr; // check if we should remove self since child node doesn't exist anymore return (!node->hasChildren()); } // else do nothing } } }//end remove void print(TrieNode* node, int depth) { if(node == nullptr) return; printf("\n|"); for(int i=0; i<depth; i++) { printf("-"); } if(node->isLeaf()) printf("%d leaf:%s", node->value, node->leafObject ); else printf("%d", node->value); print(node->children[0], depth+1); print(node->children[1], depth+1); } };

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Hash Table practice
I implemented a simple hash table today in c++, with separate chaining with linked lists. My first pass was to use open addressing but I read up on Wikipedia and decided to switch to linked lists. But it looks like dynamic arrays are still the best?
Either way, I learnt a lot about the Hash Tables, Universal Hash function as well as The Power of Two Choices.
struct Entry { public: int key; int value; Entry* next; Entry() : key(-1) , value(0) , next(nullptr) {} Entry(int key_, int value_) : key(key_) , value(value_) , next(nullptr) {} }; class HashTable { public: HashTable() { m_array.resize(16, nullptr); } ~HashTable() { m_array.clear(); } void set(int key, int value) { int index = getHash(key); // not added yet if(m_array[index] == nullptr) { m_array[index] = new Entry(key, value); } // if same key, replace value else if(m_array[index]->key == key) { m_array[index]->value = value; } // deal with collision else { Entry* parent = m_array[index]; while(parent->next != nullptr) { parent = parent->next; } parent->next = new Entry(key, value); } } void print() { for(int i=0; i < m_array.size(); i++) { Entry* e = m_array[i]; printf("r %d:\n", i); int space = 1; while(e != nullptr) { for(int j=0; j < space; j++) { printf(" "); } printf("(%d,%d)\n", e->key, e->value); e = e->next; space++; } } } private: int getHash(int key) { return key % m_array.size(); } vector<Entry*> m_array; };
Insertion Sort practice
Got some practice for insertion sort. Oddly enough, I think I remember an alternative version from the wikipedia one where it sorted from the back. Also I confused myself with Bubble Sort, since this had swapping of adjacent cells, I thought it couldn't be Insertion Sort. Then I realized that, actually if it were sorted, it would be O(n), whereas in Bubble Sort, it would still take O(n^2). I guess I must be careful to remember that difference.
void insertionsort(vector& arr) { for(unsigned int i= 0; i < arr.size(); ++i) { int valueToInsert = arr[i]; int holePos = i; while(holePos > 0 && valueToInsert < arr[holePos-1]) { arr[holePos] = arr[holePos-1]; holePos -= 1; } arr[holePos] = valueToInsert; } }
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
"if you read this and understand it, I promise you, you will be a better C++ coder than most C++ coder" - bludo
*most coders.
Merge Sort practice
Coded merge sort for practice. This time I succeeded in doing this completely from memory of the behaviour of the algorithm and I did not have to Google for it. Surprisingly simple and elegant. After I finished, I did end up looking at the wikipedia page and learned that it is useful for external sorting. I must have somehow forgotten all these from school days.
vector<int> mergesort(vector<int>& arr, int left, int right) { if(left >= right) return vector<int>(1, arr[left]); // split into half int mid = (right+left)>>1; vector<int> sorted1 = mergesort(arr, left, mid); vector<int> sorted2 = mergesort(arr, mid+1, right); vector<int> finalSorted; int s1Index = 0; int s2Index = 0; size_t sorted1Size = sorted1.size(); size_t sorted2Size = sorted2.size(); while(sorted1Size > s1Index || sorted2Size > s2Index) { if( (s2Index >= sorted2Size) || (s1Index < sorted1Size && sorted1[s1Index] < sorted2[s2Index]) ) { finalSorted.push_back(sorted1[s1Index]); s1Index++; } else { finalSorted.push_back(sorted2[s2Index]); s2Index++; } } return finalSorted; }
Quick Sort practice
Leveling up and revising algorithms starting with Quick Sort. I tried to code this completely from memory but I failed to remember the partition step, in particular, I forgot about the storeIndex. I think the algorithm can also be further optimized slightly by avoiding the swap if the swap indices are the same. I also made some mistakes with the recursion in quicksort function.
void swap(vector<int>& arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } int partition(vector<int>& arr, int left, int right) { // random pivot int pivot = (rand() % (right-left)) + left; int pivotVal = arr[pivot]; // swap pivot to the end temporarily swap(arr, pivot, right); // run through elements and see if greater or less than int storeIndex = left; for(int i=left; i <right; i++) { if(arr[i] < pivotVal) { swap(arr, i, storeIndex); storeIndex++; } } // move pivot into final place swap(arr, storeIndex, right); return storeIndex; } void quicksort(vector<int>& arr, int left, int right) { if(left >= right) return; int pivot = partition(arr, left, right); quicksort(arr, left, pivot-1); quicksort(arr, pivot+1, right); }

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Trouble with Aliasing in Unity3d
It's a funny thing Aliasing. We used to hate it, but now we love it.
Here's an image of anti-aliased rendering of text in Unity, it's not as bad as it seems but it's not purrfect either...
I wanted to avoid smoothing out the text in Unity, but apparently there's no easy way to do it. I started out with Unity's built-in 3D Text, then I found out that I couldn't change any setting to make it render as aliased.
I thought maybe it could be the shader and I was looking into the Shader Lab settings and figuring the tags and all that. I then came across this 3DText template and copied it.
But then found these: http://forum.unity3d.com/threads/13037-Is-it-possible-to-turn-anti-aliasing-off-in-a-shader http://answers.unity3d.com/questions/172516/shader-for-pixel-art.html
Which led me to think that it could be the texture setting of the font. I then went back to Unity, dismayed to find that I cannot change the texture setting of the font texture (generated from the TTF). So I guess that meant I had to work with bitmap fonts.
I had prior experience with bitmap fonts and used BMFont to export fonts which were used in mobile games. However, I was not sure if Unity had support for them. Finally, I found this post Pixel Perfect Bitmap Font, which doesn't look well design code-wise. It's a great demo but it's not a library.
I'll be honest, I'm a bit lazy right now, so I think I'll decide to skip over this whole aliased text issue for now. Maybe I'll figure it out after the jam too.
P.S. I did managed to find TextBox unity plugin which I had to pay for, but this being a jam, I'll refrain for now.
Meteors Incoming!
Import Blender to Unity3d Next was to import it into Unity3D, I also only just found out that Unity supports blender files! http://docs.unity3d.com/Documentation/Manual/HOWTO-ImportObjectBlender.html
Export Blender file with Textures Next up is to find out how to export the blender file with the textures. I couldn't really figure this out and read something about uv unwrapping which I did not fully understand. Its clear that Unity does not import the materials directly, so I fumbled around a bit but managed to magically get it working after a while. I'll come back to this after the jam.
Meteor Particles I also worked on the particles systems for the meteor. I made them change color over their lifetime but I almost forgot how fire looked like for a moment and totally messed up the gradient! I set it like red->white->yellow->black for some strange reason. Luckily Google comes to the rescue once again.
Next was to make the particles always move up in world space. I found the solution here: http://answers.unity3d.com/questions/281475/how-do-you-make-a-particle-effect-always-emit-upwa.html
After that, I also realized I had to split the particles from the meteor, because I wanted to randomize the rotation of the meteor mesh, while still have the particles emit upwards, I could not find a way to do that with the particles system, so I made an empty gameobject with just the particles and sphere collider, and set the meteor mesh as a child of the new object. This meant I could rotate the mesh without affect the particles.