How to find the Curve Length in AS3

Recently when working on one of the features of Creately (an online collaborative diagram software) I had the need to calculate the curve length of a quadratic curve. The Flex/AS3 curves are quadratic (when you draw using the curveTo function of the Graphics class).The requirement I had was a pretty simple and straightforward one. However I didn’t find a simple straightforward piece of code that I could use to get this done. Of course there are frameworks such as Degrafa (an article on the topic) and Singularity libraries that allow you to achieve this very easily using those libraries itself. But if you are looking for just a function to get this done and not so worried about the accuracy of the calculation, the following should help. I’ve tried my best to explain this in the simplest for so that anyone can make use of it.

The most simple and naive method of calculating a curve length is to go through the curve in regular intervals and sum up the length of the line segments that are formed by the intervals. The accuracy of the curve length will vary by the frequency of the intervals. The higher the frequency the accurate and curve length would be, and the lower the frequency the more efficient your calculation will be. Depending on your requirement you can adjust the frequency when calculating the curve length. The following diagrams would further explain this (Please note that these diagrams are purely for the purpose of conceptualizing and are not accurate).

The above diagram explains the fundamental rule of a quadratic curve. Value t being a value between 0 and 1 can be used to find a point on the curve in relation to the full length of the curve. If t=0.5 (middle), the line interconnecting the middle point on line p1,cp and middle point of line cp,p2 will touch the the middle point of the curve at the middle of the formed line.

By deriving f number of regular intervals of t values (minimum being 1/f), we can form line segments along the curve that can be used to calculate the full length of  the curve. This can be done using the following two AS3 functions.

//Function that calculates the curve length
public function curveLength ( p1 : Point, p2 : Point, cp : Point,
	f : int ) : Number {

	var length : Number = 0;
	var step : Number = (1/f);
	var tx : Number;
	var lastPoint : Point;
	var curPoint : Point;

	for ( tx = step; tx < 1; tx += step ) {
		curPoint = interpolateCurve( p1, p2, cp, tx );
		if ( !lastPoint ) {
			length += Point.distance( p1, curPoint );
		} else {
			length += Point.distance( lastPoint, curPoint );
		lastPoint = curPoint;

	length += Point.distance( lastPoint, p2 );
	return length;

//Function to interpolate a curve on t
public function interpolateCurve ( p1 : Point, p2 : Point, cp : Point,
	t : Number ) : Point {

	var ret : Point;
	var x1 : Point = Point.interpolate( cp, p1, t );
	var x2 : Point = Point.interpolate( p2, cp, t );
	ret = Point.interpolate( x2, x1, t );

	return ret;

You can find a suitable f value for your purpose. To better understand this I have written a demo app that draws a given curve using both the above method and the Flex Graphics.curveTo method on top of each other. By setting different f values you can understand the difference between each curve visually. I have found 10-20 to be a decent value for f depending on the size of the curve. You can further play with this by downloading the below mxml file.

Download Curve Length Demo (Simply click on three points of the canvas to draw the curve )


Do your Time Machine backups really work?

The real time machine!As a Apple fanboy I have been one to explore and make use of most of the features apple products have to provide. Specifically this time I am talking about Time Machine on my MacBook Pro. A little experience I had over the weekend trying to get Lion (Apparently the most advanced desktop OS in the world… Will talk about this sometime later) on to my MBP.

First to my rant on installing Lion. So I download the whole 3.66GB over couple of days through Mac app store and then happily clicked open the install to realize that I was one of the dishonorable one’s who had to re-partition the disk to be able to get the glorious Lion on! I had Master Boot Record partitions on my disk and for OSX to install it requires a GUID partition table. So guess what.. now I have to get the whole 500GB HDD emptied out, then just change the partiton scheme, then get all the data back on and then install lion! Didn’t sound the least bit fun, but I had to pledge my loyalty to Apple (not really… Just being a geeky hipster). But I had Time Machine with me, why did I have to worry?? you’re soon gonna find out.

I have been using Time Machine to backup couple of my drives since the beginning. So I didn’t have to worry about the “important” stuff which is already backed up. So I went ahead and backed up the other data on to another HDD I had and I was all set. Ran another Time Machine backup for one last time and then went ahead with the repartitioning. So I had to boot from Snow Leopard on the disk so I can erase all my partitions to create the same set of partitions on GUID scheme. Once done, went right into “Restore from Time Machine backup” to get my “important” stuff back into the drive. The restore started as I sat there experiencing the time travelling magic. After sometime I realized that I was stuck in time (so to speak). The restore had frozen and did not progress any further. Waited for ages with no progress and tried many times with absolutely no luck. Clearly I was in a panik state and felt like I had lost my identity. While I was behaving like Tom Hanks in Cast Away, I dugg deeper to find out that my Time Machine HDD had errors on the directory structure making some parts of the data unreadable. Disk Utility could not repair the errors. So I went to the Mac disk guru Disk Warrior which managed to recover most of my data from the Time Machine backup. Went through few more hoops before I could get back to Snow Leopard to the state where I exactly was. Then installed Lion which actually took only 30 mins. So the whole disaster had cost me a whole weekend, loss of many hours of sleep, a panic attack, loss of data which I feel every now and then and a blog post.

Now the funny part is I had been backing up on Time Machine for 2 years without realizing that my disk was actually broken. And this came to my attention at the worst possible time! You would think that an advanced tool like Time Machine would make sure your backup disk is intact when making a backup, but clearly it doesn’t. This I feel is a important feature that Apple needs to include in Time Machine. To my dismay I found quite a few users who had disk problems on their Time Machine backup disk during my research in getting out of this mess. I would urge all Time  Machine users to make sure to run a Verification with Disk Utility every time you run a backup. I know I will :-/

All in all even having gone through all this I still feel that Time Machine is a wonderful service to have on your Mac. Even having gone through all the trouble I was still able to get my MBP right back to where it was only because of Time Machine.

Image from :

The very basics of a Startup Idea

This topic is something I have been wanting to put down since of late. The idea of this post is to discuss the nature of two basic factors of any startup idea. The two factors being the problem and the solution (… yes im serious). I know what you are thinking.. its just way too basic that any business idea cannot exist without these two factors. True enough, but are they always basis of new product ideas? Something that you see quite often is startups that come up with products for a given space just because the space is “hot” OR because you can easily build a product to compete in the space OR even for fact that there is plenty of money to be made in a given space. And these products most of the time compliment the space than dominating them for they were not built for the right reasons.

So what am I really trying to say? I guess what I am trying to say is that, you will be in the best position to start something if you fit the below explained criteria. This at the same time doesn’t in any way mean that you have to fit this criteria to come up with a good startup idea.

The Problem : Feel the pain

For an idea to form the pain needs to be felt. The problem needs to be something that is experienced by you. At least it needs to be something you understand well and know how it feels to face this problem on a regular basis. This inherently gives you a detailed understanding of the pain and why the problem exists. When you understand a problem well, you generally would be connected to a community who understand this problem as well. This intern gives you the ability to explore different perspective of the same problem. Since you do understand the problem you would already know the available solutions to this problem if they do exist. And you would also know why these solutions might be less than ideal.

The Solution : Turn the pain into joy!

The solution for the problem should eliminate everything that is a pain and turn the whole process into pure joy. Now to do this you have to come up with the perfect solution that eliminates all the little difficulties and pains in the process. To desing such solution you must be that smart guy with the problem solving skills engraved deep into your genes. You are able to come up with a better solution than your competitors because one of the key reasons for you to start this idea itself is your competing product’s incompetence. As you develop your idea and concept you will again be in a better position since you are connected to people who are looking for the solution on a regular basis who will be able to give you constructive feedback and even commit a purchase!

All in all you will always have the upper hand in putting together a good startup idea, if you are dealing with something that you well understand and are well connected to. Following are some of the products/companies that I found to have built good products that solve the problem well and have good adoption in their respective markets.

String (My Gengo)

Solving a very specific problem that has almost no competition. The problem does exist but the problem is niche enough that a good solution has not been come up until this one. String from My Gengo is a tool that enables you manage your language bundles against it’s translations. When you have an application that is in multiple languages, its quite a hassle to manages text changes across all translations. The translation company My Gengo being the ideal people who understand the problem came up with the ideal solution.


A problem that has been solved many times but not so quite well. A former engineer at Adobe saw the problem and understood what was needed for him to get a simple wireframe drawn. Quickly put together a wire-framing tool that is making history because if it’s simplicity and completeness of solving the problem. The guy was facing the problem regularly and he sure was smart enough to get the job done right!


I tried not to bring in facebook! but you know what.. it fits the case so well. Zuckerburg saw the problem and felt it. Thats what made Facebook different than any other social networks. Being part of the problem helped Zuckerburg put in the right touches to get it to where it is today.

Would love to hear any thoughts about this topic in general? Please comment.

Mac App Store is just not going to work!

So I’m going to have to take back my fan boy tweet I made about Mac App store weeks back when it was announced. The whole idea of a Mac App Store sounded great having seen the iOS store and all other marketplaces that allow you to find apps that fit your requirement, much easily. However there comes a point when your requirement of an apps marketplace turns complex and not one provider is keeping up (at least for me). This is typically the case when it comes to anything new. Initially you are new to the idea and your requirements around the idea are not too complex. Over time when more implementation of the idea are experienced, your requirements start to grow. For any product to keep up they need to cater to these requirements.

However let me get to my disappointment of the moment. So today when I was beating myself up to get more work done over the weekend I realized that I needed a short break before I could go on. So I was wondering if I could play a game. Short after that thought I realized again that I didn’t have any games on my mac. So I went to the place where I could find something fast and get it over with – The Mac App Store. As soon as I went it to the games category, my eyes spotted the “Space Roads” game. Quick description on Space Roads – This was a 3D MS-DOS game that I had gone nuts over back when I was a kid where computers still had green screens and you’d be lucky to spot a full color screen. So I got excited and went right in to check it out. The screen shots pretty much looked the same and it was only $0.99. So next thing I know it was downloading to my mac. Few minutes after I opened it up to enjoy some serious old-school gaming and I was literally shocked by the quality and user experience. I couldn’t believe that not only someone couldn’t build a game to exceed the quality and experience of  a game built decades before, but they couldn’t even get anywhere closer to matching it. I was super pissed.. not at the guy who made the game but at Mac App store for listing it with a price tag on it. So I go on to the store again to find a way to cancel this purchase and guess what… you cant do it. Tried the apple site, still no option. Its not so much about the $0.99 but the feeling of getting screwed 🙂

Now to the topic of my post. Why won’t the Mac App Store work?

  1. No way to trial a App before buying. How can one pay for something that they have no idea of how it feels to be used. If trial is so hard to have, how difficult is it to have a video/screen-cast to demonstrate it.
  2. People have options when it comes to Mac applications. The whole problem with Apple is that they got carried away with the success of the iOS App Store. It was so much of a success not because of anything else but the first reason being users of iOS devices had no other choice. App Store was the only store iOS device users could get apps from. Then there is jail-breaking which is not something everyone can do or wants to do. So Apple applied the same model on Mac App Store and for their dismay the whole world didn’t come running wanting to sell their apps on the Mac App store. So it is not the ultimate place for mac apps (at least yet!)
  3. I can’t cancel a purchase! Yes its great that the process of buying a app is one click away but you would also want to undo it with not more than one. At minimal a policy for the provider to have a money back period would make things much better. But still I haven’t found a way to do this without contacting Apple.
  4. Mac apps are not so cheap as iOS Apps, at least when it comes to proper apps that get something useful done. So put together the previous 3 points and your decision of buying a app becomes relatively critical.

Image from:

Values of both worlds: Online and Desktop

Re-blogging a post I wrote on Creately Blog. If you didn’t already know Creately is a Collaborative online diagram software.

We have been focused during the past year in delivering Creately to you in various different forms. As you may already know, currently the most discussed topic here at Creately is the Creately Desktop edition. Yet another effort in getting Creately to all of you, who just NEED it on your desktop!

When building Creately Desktop, we have been careful to only add value to all the loved features and not to lose any of that coolness. What I’m trying to say is that, any tool online or locally installed, they all have their pros and cons. But Creately Desktop uniquely combines some of these key advantages of both worlds to give you the ultimate experience when it comes to working with your diagrams.

Advantages of Desktop tools

Everything is saved locally

Yup, all your work is in your hard drive and ready to be accessed anytime you want it. Its pretty much the same in an online tool but most people seem to like it this way =). So Creately Desktop makes sure to do this for you.

Online can be slow sometimes

When you are using an online tool, most of the time it needs to go half way around the world to make sure what you want is done and this can make your experience may be a bit slow. Creately Desktop has its brains and body on your local computer and works as fast as you do.

You don’t need internet

Hey its not always that you have internet and there may be times when you don’t have access to internet. So when you need to see that diagram you worked on last week, Creately Desktop can just pick it off your hard drive for you and you can continue doing your magic. Not only diagrams that you created but also diagrams created by someone else will be available on your hard drive when it is shared with you. This is possible since Creately Desktop makes sure to get all the work on your Creately Online account to your local hard drive whenever you have internet.

Advantages of Online tools

Working together (Sharing)

True that sharing with people becomes very easy when your work is on the cloud and anyone around the world can see it as you grant them access. Hey but Creately Desktop lets you do this just like an online tool. As your work is saved in your local hard drive, it is also transferred to your Creately Online account so that you can collaborate with anyone you wish. The experience is just like an online tool when it comes to sharing.


You want to embed your diagram or give a link to someone of your diagram so that they can just see it as it changes over time. Consider it done! You can just do this the same way you would do it on Creately Online. Just make sure you have internet =).


One of the coolest thing about an online tools is that you don’t need anything but a browser to access your work. So when you don’t have your computer next to you and you want to check out that diagram you drew couple of days back, all you are going to need is a browser when it comes to Creately. This is again possible because Creately Desktop always syncs your work to your Creately Online account. This way you can, at any time see or get crazy on your diagrams when you don’t have your computer around.

What do you think?

Like I said, we have been hard at work making sure you have the best diagraming experience not just in drawing but in everything else. Give it a try, and let us know what you think. Tell us what else we can do to make this experience worthwhile. Happy Diagramming!

Image from :

Banking with HSBC (Sri Lanka)

Writing about my banking matters on a publicly read blog may not sound like the greatest idea but still I felt I had to share this. Not to sound a HSBC fanboy, but they are by far the best banking experience I have had. This may be something specific to me and HSBC due to my requirements and interests when it comes to banking.

I used to bank with a series of local banks since the time I had any actual banking requirements. About several years back I started out with some services at HSBC due to a offer as I remember. Eventually I dropped off all dealings with other banks and stuck to HSBC on all my personal banking. Until the day I would have walked into a bank facility to get something done not more than ten times. Everything else just gets done between credit cards/ATM/Phone and Online banking. This clearly says something.

HSBC as I know it, had the focus (at least in Sri Lanka) of minimizing the cost on resources and automating as many services as possible. I think they have been very successful at not only achieving this focus but also growing their trust and quality in their services. This works perfectly well for a person like me. I like to get things done from where I am than having to go around all day long (on second thought thats something anyone would like). So online banking is my holy grail of all financial matters. From managing accounts, credit cards, loans, transfers to paying utility bills and anything else you can think of, all controlled from one screen. Anything beyond that mostly gets done through a phone call.

One of the important factors in any business is customer care. This is handled well at HSBC may it be in facility or over the phone. I never feel that I have to wast my time to get something done, mostly because of the way I am treated. I dont want to get started on all the offers and facilities that are provided with banking and credit cards. Its something definitely worth looking at if you are in Sri Lanka. There is also something new coming up every now and then that you can benefit out of.

If you were looking for the part where I share the details of my bank accounts and pin numbers, you are out of luck. All in all for me things just get done with HSBC and isn’t that what we all need. HSBC – Thanks for everything so far and dont screw up in future as well! 🙂

Image from: The Sunday Morning Herald

iPhone 4 in Malaysia

I’m glad I got the time to write a post after such a long time. It’s been very hectic since I got back from holiday. There was so many little things I wanted to blog about but never got the chance. However this is something I definitely want to share.

So we were at the Aviary at KL enjoying the wonderful collection of birds (not something you wanna miss if you go to KL) and one of my friends who had gone shopping in KL rings me up and gives me a shocking news. He is standing at a electronic store holding on to a iPhone 4 which is priced at RM 1100.00 (approx. USD350). Now I know this doesn’t sound right but at the meantime part of me wants to believe it is real. So I go on about inquiring further about the device to confirm the validity. As he speaks the form-factor, the behavior, applications and everything else seem to fit the criteria of a iPhone 4. So I had to see this. We decided to head there right after the Aviary.

So once we got out of the aviary we headed straight to the mall Kota Raya. On they way we were discussing of how many phones we should be buying and how we should be distributing them once we are back home. So we get to the mall which is a fairly cheap looking one filled with tiny kiosks selling only phones. So our friend takes us to the specific kiosk where there are plenty of phone on display and there it was the iPhone 4. Looked perfect, the exact form-factor, had the “Designed in California” print on it, had all the default iPhone apps, all working fine. Behavior looks normal and I was getting excited by the minute. I went on checking, except for the font being different I honestly couldn’t see any difference. The guy at the store confirmed that it was a original iPhone 4 more than once. So I was ready to buy 5 pieces for me and my friends and we wanted to pay by credit card.

While the guy was trying to figure out how to charge us since he didn’t have the credit card facility, and the meantime I wanted to check the firmware version of the iPhone. So I went into the “Settings” and then “General” and was looking for the “About” section and it didn’t seem to be there. I looked again wondering I was looking in the wrong place and no it wasn’t there. Then I went into the “App Store” app to see if that works and it didn’t, it loaded something else. All my excitement turned off like a spill of water in the fireplace. The store guy was trying to convince me saying that was because I didn’t have wifi on. But he was done fooling me by now cos I started to see the rest of the discrepancies. The icons inside the settings section were different and the screen was clearly not a Retina Display. The sim holder was very visible to the eye unlike in a real iPhone 4. So when I told the store guy that I was not going to buy it cos it particularly didn’t look legitimate, the guy almost started a fight with me making it a scene 😀

However all these added up to just one thing. We were on a wild goose chase! It should have been pretty obvious when it was too good to be true. A iPhone 4 cannot be probably even made for USD400. I kept wondering how many of the tourists may be getting played by this everyday. Later when I got back to the room I googled “iPhone 4 copy” and found tons of explanations and links of fake and copy products for much cheaper prices. There was even one saying “Why pay more when you can by for $85”.

Conclusion: When you are buying something for a unbelievably low price (not just iPhones) think twice.