Archive for January, 2009
AS3 unit conversion util class
Posted by Dennis in Actionscript 3, Util on January 27th, 2009
As I was developing an application, I needed to convert degrees celcius to fahrenheit and vice versa,
as well as mph to kph and vice versa.
I figured the best way to implement this was in a util class because these calculations aren’t likely to change any time soon
After some googling for a class like this, i didn’t find something alike.
So that’s why I decided to write my own and of course share it with you. I tried to put in the most commonly used conversions.
This class enables you to convert:
- miles to kilometers, kilometers to miles
- celsius to fahrenheit, fahrenheit to celsius
- meters to feet, feet to meters
- … and many more
This is the result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /** * ... * @author Dennis Jaamann * @version 1.0 * Util Class that converts units from one given unit to another * * Copyright (c) 2009 Dennis Jaamann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.dj.util { public class UnitConverter { /** * Length */ public static function kilometersToMiles(kilometers:Number):Number{ return kilometers * 0.621371192; } public static function kilometersToNauticalMiles(kilometers:Number):Number{ return kilometers * 0.539956803; } public static function milesToKilometers(miles:Number):Number{ return miles * 1.609344; } public static function milesToNauticalMiles(miles:Number):Number{ return miles * 0.868976242; } public static function nauticalMilesToKilometers(nauticalMiles:Number):Number{ return nauticalMiles * 1.85200; } public static function nauticalMilesToMiles(nauticalMiles:Number):Number{ return nauticalMiles * 1.15077945; } public static function metersToFeet(meters:Number):Number{ return meters * 3.2808399; } public static function feetToMeters(feet:Number):Number{ return feet * 0.3048; } public static function metersToYards(meters:Number):Number{ return meters * 1.0936133; } public static function yardsToMeters(yards:Number):Number{ return yards * 0.9144; } public static function centimetersToInches(centimeters:Number):Number{ return centimeters * 0.393700787; } public static function inchesToCentimeters(inches:Number):Number{ return inches * 2.54; } /** * Temperature */ public static function fahrenheitToCelsius(degrees:Number):Number{ return (degrees - 32) / 1.8; } public static function fahrenheitToKelvin(degrees:Number):Number{ return (degrees + 459.67) / 1.8; } public static function celsiusToFahrenheit(degrees:Number):Number{ return (degrees * 1.8) + 32; } public static function celsiusToKelvin(degrees:Number):Number{ return degrees + 273.15; } public static function kelvinToCelsius(degrees:Number):Number{ return degrees - 273.15; } public static function kelvinToFahrenheit(degrees:Number):Number{ return (degrees * 1.8) - 459.67; } /** * Weight */ public static function kilogramsToPounds(kilograms:Number):Number{ return kilograms * 2.20462262; } public static function kilogramsToStone(kilograms:Number):Number{ return kilograms * 0.157473044; } public static function poundsToKilograms(pounds:Number):Number{ return pounds * 0.45359237; } public static function poundsToStone(pounds:Number):Number{ return pounds * 0.0714285714; } public static function stoneToPounds(stone:Number):Number{ return stone * 14; } public static function stoneToKilograms(stone:Number):Number{ return stone * 6.35029318; } public static function gramsToOunces(grams:Number):Number{ return grams * 0.0352739619; } public static function ouncesToGrams(ounces:Number):Number{ return ounces * 28.3495231; } /** * Speed */ public static function kilometersPerHourToKnots(kilometersPerHour:Number):Number{ return kilometersPerHour * 0.539956803; } public static function kilometersPerHourToMilesPerHour(kilometersPerHour:Number):Number{ return kilometersPerHour * 0.621371192; } public static function milesPerHourToKnots(milesPerHour:Number):Number{ return milesPerHour * 0.868976242; } public static function milesPerHourToKilometersPerHour(milesPerHour:Number):Number{ return milesPerHour * 1.609344; } public static function knotsToKilometersPerHour(knots:Number):Number{ return knots * 1.85200; } public static function knotsToMilesPerHour(knots:Number):Number{ return knots * 1.15077945; } /** * Volume */ public static function litersToGallons(liters:Number):Number{ return liters * 0.264172052; } public static function gallonsToLiters(gallons:Number):Number{ return gallons * 3.78541178; } public static function cubicFeetToCubicMeters(cubicFeet:Number):Number{ return cubicFeet * 0.0283168466; } public static function cubicMetersToCubicFeet(cubicMeters:Number):Number{ return cubicMeters * 35.3146667; } /** * Power */ public static function kilowattsToHorsepower(kilowatts:Number):Number{ return kilowatts * 1.34102209; } public static function horsepowerToKilowatts(horsepower:Number):Number{ return horsepower * 0.745699872; } } } |
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 | package { public class SomeClass { public function SomeClass() { var kph:Number = UnitConverter.milesPerHourToKilometersPerHour(10); trace("10 miles per hour = " + kph + " kilometers per hour"); } } } |
Copy text or download source: unitconverter (533)
Flash Tracer & FF3
Since I’ve been more busy with flash lately, I almost forgot about flash tracer.
This is a nifty tool I usually use when developing flex. It opens an output panel in your firefox browser window, displaying everything that is outputted by flash / flex in your browser.
This is cool because then you don’t have to shift to your flex IDE’s console panel to see obvious runtime errors (or traces :p)
This can be time saving and just plain handy.
I admit, a logger is a much better way to debug an application, but hey the occasional trace can sometimes do the trick when time is scarce and solutions are needed fast.
As I was saying, I did more flash development lately and didn’t have the need to use this lovely tool.
In the meanwhile, a Firefox update was released. Result => the add-on that firefox has ready for users in the add-on pages is outdated.
Today, my colleague Bart Claessens pointed out that it could be found Here
Happy tracing
My First Post
Posted by Dennis in Random stuff on January 19th, 2009
Hello and welcome to my brand new blog.
As a flash / flex actionscript developer @ Boulevart Belgium,
I come across loads of cool and crazy stuff, as well as the usual problems involved with everyday actionscripting.
Because of the nature of my job, being a developer and all for some time now, I decided it was time for me to get myself out there.
In the community that is
Why?
Just because the actionscript community already saved me tons of times when in dire need of code and workarounds.
I figured that these people are giving me the time they lost fixing bugs, making workarounds, code examples, …
I felt that maybe if I could save somebody else some time, give him the deciding tip or lead to good solution, it was my duty to do so.
With all this said and done, I now own my first blog and will start making actionscript / flash / flex posts and off course other stuff that I run into every day.
