# Simulating Physics!

I tried do this for a while now and I have to admit it was more challenging than I thought. I had to revise some topics on physics just to get a clear handle on theories I was trying to simulate. Overall, it was a fun project and I am glad it came out just well.

My intentions were to simulate physics based environment where object would be acted upon my real-world-like physical forces. I know there are libraries out there that does this for us and also provide better performance and speed. But, I wanted to try it on my own.

Here's how I divided my work:

1. The Core
The Core package consisted mostly of physical quantities such as Position, Velocity and Acceleration. They mostly had the simple mathematics in them, that did most of the dirty works.

2. The Environment
The environment provides room for existence to the elements and it was my main engine where the elements were added and were acted up. Environment had its own properties like gravity, air frictions and boundary.

This piece of code represents general work in this module:

```for element in self.elements:
if element != selectedParticle:
element.animate()
element.draw(self.screen)
self.handleCollision(element)```

3. The Element
Element was my abstract representation of real-world object which had properties like mass, color, velocity, position and many others. Elements were added to the environment and where subjected to forces in the environment.

4. Objects
I started with a Circle, that represented a ball like object, and worked my way up-to simulating square blocks. Circles, besides representing a real-world ball, acted as pressure-points for other objects. Pressure-points were useful because these were the actual points that worked as points for exchanging forces upon collision which simulated all sorts of interesting properties like translation and rotation easily.

For example a bar consisted of two pressure points at either end. This would allow us to easily simulate bar-like properties especially rotation experienced due to difference in direction of force at either end of the bar.

The point of collision besides those pressure points could be interpolated easily. Square had a similar layout. It had these bars as its edges, protecting it from all angles.

4. Collision Handling
Collision Handling was modeled using the principle of conservation of linear momentum combined with law of conservation of energy. There was a small problem I face while implementing this approach to my models. The problem was the behavior of  objects upon interaction while there velocity neared zero. There was only one force acting that time, and it was gravity which was constantly pulling objects downwards. This was a problem, because when the objects were on top of each other, the object would collapse, unrealistically, because they had no force left in them to counteract gravity. There was a simple workaround to the problem, but it was not a wise one. Whenever two object came in contact with one another, a small force(in all four direction) would always be acting to prevent collapsing thus separating objects apart.

This was the model I went with. I am planning to share the code in near future as well. Here's a small video of the program in action:

It's not much, but it was a fun :) Let me know if you have any suggestions or feedback. Thanks!

# BusyNote! - What is it?

I recently uploaded a small app called BusyNote on PlayStore. So, in this post I am going to talk about what it is, what it does, and also share some useful resources that I used to make the app.

BusyNote is basically an app to let people know that you are currently busy and you are not currently available for cellular contact. So, when activated this app just detects missed calls and sends pre-defined notification to callers letting them know you are in a "situation" and can't answer the phone. The need to the app was initially personal. When I was on my bike, I couldn't answer my calls (obviously) and this was a problem when I was travelling during late hours and the caller was generally my parents worried and trying to reach me. So, I thought why not make an app to help my situation. And here-we-are!

The app is available on PlayStore and you can find it from this link: BusyNote

### Useful resources

Here are some resources that I used to build this app.

1. Ormlite

Object Relational Mapping Lite (ORM Lite) provides some lightweight functionality for persisting Java objects to SQL databases while avoiding the complexity and overhead of more standard ORM packages. It supports a number of SQL databases using JDBC and also supports Sqlite with native calls to Android OS database APIs. It is very handy and very easy to use.

2. Android Circle Button

Android Circle Button is a widget for android, easily available via gradle. The widget is very stylish and looks good on mobile phones. I used a modified version of the widget to support "Texts" and also added small extra features.

3. Android Sliding Up Panel

Android Sliding Up Panel is a widget by Umano Team, and I loved it. It is available on github and you can easily import it to your project via Gradle.  It provides simple way to add a draggable sliding up panel to your Android application.
Hope you find these resources useful. Also, don't forget to try the app (follow this link) and give it some love ;)

Please let me know what you thought of it or if you have any such feedbacks. Thanks!

# My Bash Script Editor - The Rights and Wrongs of how I went at it!

I have been working on developing a "Bash Script Editor" and I have been at it for weeks dedicating at least an hour a day. While I was working on it, I realized that I was doing it all wrong and now I'm in this dilemma of whether I should keep working on it till I get atleast a beta version or I should just leave it undone and concentrate on more important things.

 Snapshot of my Bash Script Editor

While I was working on ETL scripts at my work, I was using Notepad++ as my editor. It worked absolutely fine except I was totally new with the scripts to know where the functions referred in the scripts were actually called from. Having been working with powerful IDEs in the past, a simple editor to manage such a huge project seemed impossible to me and that was it. Thats what triggered me to build a more powerful editor just for Bash scripts.

The idea first seemed fine to me expect when I realized how big of a task it was and I was the only one really eager to do it. So, here I am sharing my experience so that I or anybody reading not to do/repeat such things with a list of -the rights and wrongs with my approach.

### What did I do right?

1. The concept seemed valid and I was eager to do it.

### What did I do wrong?

1. I didn't sketch a plan of how I was going to do it. A simple list to features to add, scope of functionalities to look into or may be a rough class diagram to build upon would have eased things up abit. I remember I was hesitant to do it because I thought I should just get right at it and get it done quickly. That simple hesitation went on destroying my whole effort in the end.

2. I was working on it alone. A small team of 3-4 people collaboratively working on modules would have been brilliant. With a team, the work would have been more organized, faster and optimized.

3. If I wanted to do it so eagerly, I should have first searched for any relevant open sourced projects that I could contribute with the feature. It is always a good habit to do it, only I didn't do that.

4. If I didn't want to make it big and just wanted to do it for experience and fun, then I should have done something that I could learn from. Throwing random line of codes and scratching your head to think of a solution just to make up for dirty codes done earlier is a very unhealthy habit and something that every programmer should avoid.

5. Stop before its too late. If you realize, even in slightest, that you are doing it wrong and you are only making things complicated by just adding random features, you should probably pause and look at what you've done. This would have saved a lot of my precious time.

6.  Always start with a solid base to work upon. Defining abstract classes and building your way up with it is always a good habit. A good base will always keep things organized.

So, there you go. Before you go and laugh at me or maybe judge me as a programmer, please note that I was doing it all during my office hours. I didn't have enough breathing space to look at what I was doing, all I wanted to do was to complete the task in hand when I had free time. Despite knowing it all, deep down inside, my major regret is that I started the work anyway completely underestimating the difficulty of the task.

Here's the link to the project : Bash-Script Editor (try not to laugh too hard :P)

# Perlin Noise [Implementation in Java]

I had previously talked in detail about Perlin Noise and its properties, here. Now in this blog post I will write about the implementation. My implementation is based on an excellent writeup which I found in this link (http://webstaff.itn.liu.se/~stegu/TNM022-2005/perlinnoiselinks/perlin-noise-math-faq.html).

#### Short Description

Fig: Perlin Noise(left), Noise(right)

Perlin noise is a function for generating coherent(smooth) noise over a space. I've have come across multiple implementations of Perlin Noise function and most of them are based on smoothing of multiple noise functions to generate a coherent noise. The one I am discussing in this blog post is alittle different and more or less conforms with the original implementation that Ken Perlin came up with.

#### Requirement

Perlin Noise (2D) function is a function that takes two points (x and y) and returns a noise value (say z). x, y and z are all floating point numbers.

#### The Grid

The concept of accepting floating point numbers can be explained from the concept of Grid. The space, which the noise function operates on, is assumed to be composed of grids. Grid, in terms of x and y, are the whole numbers. So, any decimal (fractions) are points lying inside a grid cell.

In the figure, (x0,y0), (x1,y1), (x0,y1), (x1,y0) represents grid points and (x,y) lies inside a grid.

Pseudorandom Gradient function takes the grid coordinates and generates pseudorandom gradient of length 1 as:

g(xgrid, ygrid) = (gx, gy)

These gradients can be visualized as:

#### Calculating grid influences

Next, We need to calculate a vector directing from grip points to the point (x,y).

vec00 = (x-x0,y-y0);
vec01 = (x-x0,y-y1);
vec10 = (x-x1,y-y0);
vec11 = (x-x1,y-y1);

The influence can be calculated by performing dot operation on the gradient and the vector as:

s = g(x0, y0) · vec00;
t = g(x1, y0) · vec10;
u = g(x0, y1) · vec01;
v = g(x1, y1) · vec11;

#### Interpolation

A smoothing function, characterized by the curver 3p2 - 2p3, is used to get a weight Sx at (x-x0) in the curve as:

Sx = smoothParam(x-x0);

and also at (y-y0) as:

Sy = smoothParam(y-y0);

where smoothParam(p) = 3p2 - 2p3 , then parameter representing weighted average of s and t is obtained by constructing a linear function mapping 0 to s and 1 to t, and evaluating it at our x dimension weight Sx. We call this ax.

ax = s + Sx*(t - s);

We also calculate bx using u and v as:

bx = u + Sx*(v - u);

Then the final result z can be obtained as:

z = ax + Sy*(bx - ax);

#### Implementation

I have implemented this algorithm in Java, and I have uploaded the project in github. You can view it from the link here.

# Triplet Extraction from Sentence [Implementation]

Many sentiment analysis tasks require extraction of sentence triplets, ie. Subject - Verb - Object, from a sentence. While there are many approaches to the problem, I recently stumbled upon a fairly easy-to-implement algorithm in a research paper (http://ailab.ijs.si/dunja/SiKDD2007/Papers/Rusu_Trippels.pdf).

# Morphological Analyzer for Nepali Text

Morphology is the part of linguistics that deals with the study of words, their internal structure and syntactically their meanings. A Morphological Analysis is the process of providing grammatical information of a word by detecting its morphemes. A morphological analysis generally consists of following information:
Word segmentation
Part-of-speech tagging
Lemmatization

These are the very basics of linguistics which lays ground to many other applications. There are various different tools and resources available for morphology analysis of popular languages such as English, but morphological analysis of Nepali texts had been in a shadow for long. Fortunately, the need of these analysis has slowly been recognized.