Mechanics math

Wednesday, March 17th, 2010

Here so I don’t have to search the internet to look for this stuff.

I’m working on some physics things for my swarm AI… tricky since I haven’t done any of this for ehm… 15 years or so.


Second law of Newton

F = m * a
a = F/m
m = F/a

F is force in newtons
m is mass in kilograms
a is acceleration in meters per second

Make a safe url

Wednesday, March 17th, 2010

This is a function that checks a string and makes it work nicely for friendly urls

  1. // Make sure a url string is nicely formatted
  2. function makeSafeUrl($myUrl, $allowSpace=0, $allowCase=0){
  3.         $sSafe = 'abcdefghijklmnopqrstuvwxyz1234567890-_';
  4.         $disallowed = array();
  5.         $disallowed['c'] = 'ç';
  6.         $disallowed['n'] = 'ñ';
  7.         $disallowed['y'] = 'ýÿ';
  8.         $disallowed['e'] = 'èéêë';
  9.         $disallowed['a'] = 'àáâãäå';
  10.         $disallowed['o'] = 'ðóòôõöø';
  11.         $disallowed['u'] = 'ùúûü';
  12.         $disallowed['i'] = 'ìíîï';
  13.  
  14.         if(!$allowSpace) $disallowed['-'] = ' ';
  15.         if($allowSpace) $sSafe .= ' ';
  16.  
  17.         if($allowCase){
  18.                 $sSafe .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  19.                 $disallowed['C'] = 'Ç';
  20.                 $disallowed['N'] = 'Ñ';
  21.                 $disallowed['Y'] = 'ÝŸ';
  22.                 $disallowed['E'] = 'ÈÉÊË';
  23.                 $disallowed['A'] = 'ÀÁÂÃÄÅ';
  24.                 $disallowed['O'] = 'ÐÓÒÔÕÖØ';
  25.                 $disallowed['U'] = 'ÙÚÛÜ';
  26.                 $disallowed['I'] = 'ÌÍÎÏ';
  27.         }else{
  28.                 $myUrl = strtolower($myUrl);
  29.         }
  30.  
  31.         $newString = array();
  32.  
  33.         for($i = 0; $i $var){
  34.                                 if(stristr($var, $thisChar)){
  35.                                         $newString[$i] = $key;
  36.                                 }
  37.                         }
  38.                 }
  39.         }
  40.         return implode('', $newString);
  41. }

Display clean php code for copying

 // Make sure a url string is nicely formatted
function makeSafeUrl($myUrl, $allowSpace=0, $allowCase=0){
	$sSafe = 'abcdefghijklmnopqrstuvwxyz1234567890-_';
	$disallowed = array();
	$disallowed['c'] = 'ç';
	$disallowed['n'] = 'ñ';
	$disallowed['y'] = 'ýÿ';
	$disallowed['e'] = 'èéêë';
	$disallowed['a'] = 'àáâãäå';
	$disallowed['o'] = 'ðóòôõöø';
	$disallowed['u'] = 'ùúûü';
	$disallowed['i'] = 'ìíîï';

	if(!$allowSpace) $disallowed['-'] = ' ';
	if($allowSpace) $sSafe .= ' ';

	if($allowCase){
		$sSafe .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$disallowed['C'] = 'Ç';
		$disallowed['N'] = 'Ñ';
		$disallowed['Y'] = 'ÝŸ';
		$disallowed['E'] = 'ÈÉÊË';
		$disallowed['A'] = 'ÀÁÂÃÄÅ';
		$disallowed['O'] = 'ÐÓÒÔÕÖØ';
		$disallowed['U'] = 'ÙÚÛÜ';
		$disallowed['I'] = 'ÌÍÎÏ';
	}else{
		$myUrl = strtolower($myUrl);
	}

	$newString = array();

	for($i = 0; $i $var){
				if(stristr($var, $thisChar)){
					$newString[$i] = $key;
				}
			}
		}
	}
	return implode('', $newString);
}

List of all countries

Wednesday, March 17th, 2010

I found it helpfull to have this around from time to time.

Afghanistan, Albania, Algeria, American Samoa, Andorra, Angola, Anguilla, Antigua and Barbuda, Argentina, Armenia, Aruba, Australia, Austria, Azerbaijan, Bahamas, Bahrain, Bangladesh, Barbados, Belarus, Belgium, Belize, Benin, Bermuda, Bhutan, Bolivia, Bosnia, Botswana, Brazil, British Virgin Islands, Brunei, Bulgaria, Burkina Faso, Burundi, Cambodia, Cameroon, Canada, Cape Verde, Cayman Islands, Central African Republic, Chad, Chile, China, Colombia, Comoros, Costa Rica, Cote d’Ivoire, Croatia, Cuba, Cyprus, Cyprus, Czech Republic, Democratic Republic of the Congo, Denmark, Djibouti, Dominica, Dominican Republic, East Timor, Ecuador, Egypt, Egypt, El Salvador, Equatorial Guinea, Eritrea, Estonia, Ethiopia, Falkland Islands, Fiji, Finland, France, French Guiana, French Polynesia, Gabon, Gambia, Georgia, Germany, Ghana, Gibraltar, Greece, Greenland, Grenada, Guadeloupe, Guam, Guatemala, Guinea, Guinea-Bissau, Guyana, Haiti, Honduras, Hungary, Iceland, India, Indonesia, Iran, Iraq, Ireland, Israel, Italy, Jamaica, Japan, Jordan, Kazakhstan, Kenya, Kiribati, Kuwait, Kyrgyzstan, Laos, Latvia, Lebanon, Lesotho, Liberia, Libya, Liechtenstein, Lithuania, Luxembourg, Macau, Macedonia, Madagascar, Malawi, Malaysia, Maldives, Mali, Malta, Marshall Islands, Martinique, Mauritania, Mauritius, Mexico, Mexico, Micronesia, Moldova, Monaco, Mongolia, Montenegro, Montserrat, Morocco, Mozambique, Myanmar, Namibia, Nauru, Nepal, Netherlands, Netherlands Antilles, New Caledonia, New Zealand, Nicaragua, Niger, Nigeria, North Korea, Northern Mariana Islands, Norway, Oman, Pakistan, Palau, Palestine, Panama, Papua New Guinea, Paraguay, Peru, Philippines, Pitcairn Islands, Poland, Portugal, Puerto Rico, Qatar, Republic of the Congo, Romania, Russia, Rwanda, San Marino, Sao Tome and Principe, Saudi Arabia, Senegal, Serbia, Seychelles, Sierra Leone, Singapore, Slovakia, Slovenia, Solomon Islands, Somalia, South Africa, South Korea, Spain, Sri Lanka, St. Kitts and Nevis, St. Lucia, St. Vincent and the Grenadines, Sudan, Suriname, Swaziland, Sweden, Switzerland, Syria, Taiwan, Tajikistan, Tanzania, Thailand, Tibet, Togo, Tonga, Trinidad and Tobago, Tunisia, Turkey, Turkey, Turkey, Turkmenistan, Turks and Caicos Islands, Tuvalu, U.S. Virgin Islands, Uganda, Ukraine, United Arab Emirates, United Kingdom, United States, Uruguay, Uzbekistan, Vanuatu, Venezuela, Vietnam, Western Sahara, Western Samoa, Yemen, Zambia, Zimbabwe

JQuery geshi

Wednesday, March 17th, 2010

Code highlighting using ajax with jquery and geshi

  1. $(document).ready(function(){
  2.  
  3.         // Initialise highlighting
  4.         if($('pre').length){
  5.                 $('pre').each(function(){
  6.                         highLight($(this));
  7.                 });
  8.         }
  9.  
  10. });
  11.  
  12. // Highlight item
  13. function highLight(thisItem){
  14.         snippet  = thisItem.html();
  15.         snippet = snippet.replace(/&/ig, escape('&'));
  16.         snippet = snippet.replace(/&lt;/ig, '<');
  17.         snippet = snippet.replace(/&gt;/ig, '>');
  18.         language = thisItem.attr('class');
  19.         $.ajax({
  20.                 type: "POST",
  21.                 url: '/scripts/highlight.php',
  22.                 data: 'language='+language+'&snippet='+snippet,
  23.                 dataType: "html",
  24.                 success: function(msg){
  25.                         if (msg){
  26.                                 msg = msg.replace('&amp;', '&');
  27.                                 thisItem.after(msg);
  28.                                 thisItem.hide();
  29.                         }
  30.                 }
  31.         });
  32. }

Display clean javascript code for copying

$(document).ready(function(){

	// Initialise highlighting
	if($('pre').length){
		$('pre').each(function(){
			highLight($(this));
		});
	}

});

// Highlight item
function highLight(thisItem){
	snippet  = thisItem.html();
	snippet = snippet.replace(/&amp;amp;/ig, escape('&'));
	snippet = snippet.replace(/&lt;/ig, '<');
	snippet = snippet.replace(/&gt;/ig, '>');
	language = thisItem.attr('class');
	$.ajax({
		type: "POST",
		url: '/scripts/highlight.php',
		data: 'language='+language+'&snippet='+snippet,
		dataType: "html",
		success: function(msg){
			if (msg){
				msg = msg.replace('&amp;', '&');
				thisItem.after(msg);
				thisItem.hide();
			}
		}
	});
}

Make a safe url

Wednesday, March 17th, 2010

This is a function that makes a string url safe.
Nice for creating friendly urls that are actually friendly.

  1. // Function for making sure text only uses url safe symbols
  2. function makeSafe(thisText, allowSpace){
  3.         var w = "!@#$%^&*()+=[]\\\';,./{}|\":<>?";
  4.         var s = 'abcdefghijklmnopqrstuvwxyz0123456789-_';
  5.         var x = new Array('àáâãäå', 'ç', 'èéêë', 'ìíîï', 'ñ', 'ðóòôõöø', 'ùúûü', 'ýÿ');
  6.         var r = new Array('a', 'c', 'e', 'i', 'n', 'o', 'u', 'y');
  7.  
  8.         if(allowSpace){
  9.                 s = s + ' ';
  10.         }
  11.  
  12.         thisText = thisText.toLowerCase();
  13.         var newText = new Array();
  14.  
  15.         for (i = 0; i < thisText.length; i++){
  16.                 thisChar = thisText.charAt(i);
  17.                 if(w.indexOf(thisChar) == -1){
  18.                         if(s.match(''+thisChar+'')){
  19.                                 newText[i] = thisChar;
  20.                         }else{
  21.                                 for (j = 0; j < x.length; j++){
  22.                                         if(x[j].match(thisChar)){
  23.                                                 newText[i] = r[j];
  24.                                         }
  25.                                 }
  26.                         }
  27.                 }
  28.         }
  29.  
  30.         return newText.join('');
  31. }

Display clean javascript code for copying

// Function for making sure text only uses url safe symbols
function makeSafe(thisText, allowSpace){
	var w = "!@#$%^&*()+=[]\\\';,./{}|\":<>?";
	var s = 'abcdefghijklmnopqrstuvwxyz0123456789-_';
	var x = new Array('àáâãäå', 'ç', 'èéêë', 'ìíîï', 'ñ', 'ðóòôõöø', 'ùúûü', 'ýÿ');
	var r = new Array('a', 'c', 'e', 'i', 'n', 'o', 'u', 'y');

	if(allowSpace){
		s = s + ' ';
	}

	thisText = thisText.toLowerCase();
	var newText = new Array();

	for (i = 0; i < thisText.length; i++){
		thisChar = thisText.charAt(i);
		if(w.indexOf(thisChar) == -1){
			if(s.match(''+thisChar+'')){
				newText[i] = thisChar;
			}else{
				for (j = 0; j < x.length; j++){
					if(x[j].match(thisChar)){
						newText[i] = r[j];
					}
				}
			}
		}
	}

	return newText.join('');
}

Find root folder

Wednesday, March 17th, 2010

I use this tiny snippet that uses jquery to find the root folder of my website.
It assumes jquery.js is included in the page.
It’s usefull for finding out where to post ajax stuff to (so you have a complete root).

For this website it would look in the header of the html for:
<script type="text/javascript" src="http://www.macouno.com/js/jquery.js"></script>

  1. // Get the source directory of this script
  2. root = $('script[@src$=jquery.js]').attr('src').replace('js/jquery.js', '');
  3. if(!location.href.match('www')){
  4.         root = root.replace('www.', '');
  5. }

Display clean javascript code for copying

// Get the source directory of this script
root = $('script[@src$=jquery.js]').attr('src').replace('js/jquery.js', '');
if(!location.href.match('www')){
	root = root.replace('www.', '');
}

Add to tinymce

Wednesday, March 17th, 2010

Because I will forget unless I save a copy here.
This is a very basic function for adding a string to tinymce (I use it to add images that are listed elsewhere on a page).

  1. // Adding content to the end of the tinymce editor
  2. function addToTiny(newContent){
  3.         var inst = tinyMCE;
  4.         inst.execCommand('mceFocus',false,'content');
  5.         inst.execCommand('mceInsertContent', true, newContent);
  6. }

Display clean javascript code for copying

// Adding content to the end of the tinymce editor
function addToTiny(newContent){
	var inst = tinyMCE;
	inst.execCommand('mceFocus',false,'content');
	inst.execCommand('mceInsertContent', true, newContent);
}

Is valid page

Wednesday, March 17th, 2010

A little function I use to double check the items in a friendly url to see if they only use valid characters.

  1. // Check if the page name is correct
  2. function isValidPage($page){
  3.         $safe = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '_', '.');
  4.         $pageCheck = preg_split('//', strtolower($page), -1, PREG_SPLIT_NO_EMPTY);
  5.  
  6.         // Make sure that the page name has only safe variables
  7.         foreach($pageCheck as $key => $val){
  8.                 if(!in_array($val, $safe)) return false;
  9.         }
  10.         return true;
  11. }

Display clean php code for copying

// Check if the page name is correct
function isValidPage($page){
	$safe = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '_', '.');
	$pageCheck = preg_split('//', strtolower($page), -1, PREG_SPLIT_NO_EMPTY);

	// Make sure that the page name has only safe variables
	foreach($pageCheck as $key => $val){
		if(!in_array($val, $safe)) return false;
	}
	return true;
}

Is date

Wednesday, March 17th, 2010

See if a string is actually a date (used for friendly urls mostly)

  1. // See if a string from the url is an actual date
  2. function isDate($var){
  3.         $time = strtotime(str_replace('-', ' ', $var));
  4.         if($time){
  5.                 if(strlen($var) == 8){
  6.                         return Array('month', $var, $time);
  7.                 }elseif(strlen($var) == 11){
  8.                         return Array('day', $var, $time);
  9.                 }
  10.         }
  11.         return false;
  12. }

Display clean php code for copying

// See if a string from the url is an actual date
function isDate($var){
	$time = strtotime(str_replace('-', ' ', $var));
	if($time){
		if(strlen($var) == 8){
			return Array('month', $var, $time);
		}elseif(strlen($var) == 11){
			return Array('day', $var, $time);
		}
	}
	return false;
}

Html symbols

Wednesday, March 17th, 2010
Character Entity Decimal Hex Rendering in Your Browser
Entity Decimal Hex
Latin small f with hook = function = florin &fnof; &#402; &#x192; ƒ ƒ ƒ
Greek capital letter alpha &Alpha; &#913; &#x391; Α Α Α
Greek capital letter beta &Beta; &#914; &#x392; Β Β Β
Greek capital letter gamma &Gamma; &#915; &#x393; Γ Γ Γ
Greek capital letter delta &Delta; &#916; &#x394; Δ Δ Δ
Greek capital letter epsilon &Epsilon; &#917; &#x395; Ε Ε Ε
Greek capital letter zeta &Zeta; &#918; &#x396; Ζ Ζ Ζ
Greek capital letter eta &Eta; &#919; &#x397; Η Η Η
Greek capital letter theta &Theta; &#920; &#x398; Θ Θ Θ
Greek capital letter iota &Iota; &#921; &#x399; Ι Ι Ι
Greek capital letter kappa &Kappa; &#922; &#x39A; Κ Κ Κ
Greek capital letter lambda &Lambda; &#923; &#x39B; Λ Λ Λ
Greek capital letter mu &Mu; &#924; &#x39C; Μ Μ Μ
Greek capital letter nu &Nu; &#925; &#x39D; Ν Ν Ν
Greek capital letter xi &Xi; &#926; &#x39E; Ξ Ξ Ξ
Greek capital letter omicron &Omicron; &#927; &#x39F; Ο Ο Ο
Greek capital letter pi &Pi; &#928; &#x3A0; Π Π Π
Greek capital letter rho &Rho; &#929; &#x3A1; Ρ Ρ Ρ
Greek capital letter sigma &Sigma; &#931; &#x3A3; Σ Σ Σ
Greek capital letter tau &Tau; &#932; &#x3A4; Τ Τ Τ
Greek capital letter upsilon &Upsilon; &#933; &#x3A5; Υ Υ Υ
Greek capital letter phi &Phi; &#934; &#x3A6; Φ Φ Φ
Greek capital letter chi &Chi; &#935; &#x3A7; Χ Χ Χ
Greek capital letter psi &Psi; &#936; &#x3A8; Ψ Ψ Ψ
Greek capital letter omega &Omega; &#937; &#x3A9; Ω Ω Ω
Greek small letter alpha &alpha; &#945; &#x3B1; α α α
Greek small letter beta &beta; &#946; &#x3B2; β β β
Greek small letter gamma &gamma; &#947; &#x3B3; γ γ γ
Greek small letter delta &delta; &#948; &#x3B4; δ δ δ
Greek small letter epsilon &epsilon; &#949; &#x3B5; ε ε ε
Greek small letter zeta &zeta; &#950; &#x3B6; ζ ζ ζ
Greek small letter eta &eta; &#951; &#x3B7; η η η
Greek small letter theta &theta; &#952; &#x3B8; θ θ θ
Greek small letter iota &iota; &#953; &#x3B9; ι ι ι
Greek small letter kappa &kappa; &#954; &#x3BA; κ κ κ
Greek small letter lambda &lambda; &#955; &#x3BB; λ λ λ
Greek small letter mu &mu; &#956; &#x3BC; μ μ μ
Greek small letter nu &nu; &#957; &#x3BD; ν ν ν
Greek small letter xi &xi; &#958; &#x3BE; ξ ξ ξ
Greek small letter omicron &omicron; &#959; &#x3BF; ο ο ο
Greek small letter pi &pi; &#960; &#x3C0; π π π
Greek small letter rho &rho; &#961; &#x3C1; ρ ρ ρ
Greek small letter final sigma &sigmaf; &#962; &#x3C2; ς ς ς
Greek small letter sigma &sigma; &#963; &#x3C3; σ σ σ
Greek small letter tau &tau; &#964; &#x3C4; τ τ τ
Greek small letter upsilon &upsilon; &#965; &#x3C5; υ υ υ
Greek small letter phi &phi; &#966; &#x3C6; φ φ φ
Greek small letter chi &chi; &#967; &#x3C7; χ χ χ
Greek small letter psi &psi; &#968; &#x3C8; ψ ψ ψ
Greek small letter omega &omega; &#969; &#x3C9; ω ω ω
Greek small letter theta symbol &thetasym; &#977; &#x3D1; ϑ ϑ ϑ
Greek upsilon with hook symbol &upsih; &#978; &#x3D2; ϒ ϒ ϒ
Greek pi symbol &piv; &#982; &#x3D6; ϖ ϖ ϖ
bullet = black small circle &bull; &#8226; &#x2022;
horizontal ellipsis = three dot leader &hellip; &#8230; &#x2026;
prime = minutes = feet &prime; &#8242; &#x2032;
double prime = seconds = inches &Prime; &#8243; &#x2033;
overline = spacing overscore &oline; &#8254; &#x203E;
fraction slash &frasl; &#8260; &#x2044;
script capital P = power set = Weierstrass p &weierp; &#8472; &#x2118;
blackletter capital I = imaginary part &image; &#8465; &#x2111;
blackletter capital R = real part symbol &real; &#8476; &#x211C;
trade mark sign &trade; &#8482; &#x2122;
alef symbol = first transfinite cardinal &alefsym; &#8501; &#x2135;
leftwards arrow &larr; &#8592; &#x2190;
upwards arrow &uarr; &#8593; &#x2191;
rightwards arrow &rarr; &#8594; &#x2192;
downwards arrow &darr; &#8595; &#x2193;
left right arrow &harr; &#8596; &#x2194;
downwards arrow with corner leftwards = carriage return &crarr; &#8629; &#x21B5;
leftwards double arrow &lArr; &#8656; &#x21D0;
upwards double arrow &uArr; &#8657; &#x21D1;
rightwards double arrow &rArr; &#8658; &#x21D2;
downwards double arrow &dArr; &#8659; &#x21D3;
left right double arrow &hArr; &#8660; &#x21D4;
for all &forall; &#8704; &#x2200;
partial differential &part; &#8706; &#x2202;
there exists &exist; &#8707; &#x2203;
empty set = null set = diameter &empty; &#8709; &#x2205;
nabla = backward difference &nabla; &#8711; &#x2207;
element of &isin; &#8712; &#x2208;
not an element of &notin; &#8713; &#x2209;
contains as member &ni; &#8715; &#x220B;
n-ary product = product sign &prod; &#8719; &#x220F;
n-ary sumation &sum; &#8721; &#x2211;
minus sign &minus; &#8722; &#x2212;
asterisk operator &lowast; &#8727; &#x2217;
square root = radical sign &radic; &#8730; &#x221A;
proportional to &prop; &#8733; &#x221D;
infinity &infin; &#8734; &#x221E;
angle &ang; &#8736; &#x2220;
logical and = wedge &and; &#8743; &#x2227;
logical or = vee &or; &#8744; &#x2228;
intersection = cap &cap; &#8745; &#x2229;
union = cup &cup; &#8746; &#x222A;
integral &int; &#8747; &#x222B;
therefore &there4; &#8756; &#x2234;
tilde operator = varies with = similar to &sim; &#8764; &#x223C;
approximately equal to &cong; &#8773; &#x2245;
almost equal to = asymptotic to &asymp; &#8776; &#x2248;
not equal to &ne; &#8800; &#x2260;
identical to &equiv; &#8801; &#x2261;
less-than or equal to &le; &#8804; &#x2264;
greater-than or equal to &ge; &#8805; &#x2265;
subset of &sub; &#8834; &#x2282;
superset of &sup; &#8835; &#x2283;
not a subset of &nsub; &#8836; &#x2284;
subset of or equal to &sube; &#8838; &#x2286;
superset of or equal to &supe; &#8839; &#x2287;
circled plus = direct sum &oplus; &#8853; &#x2295;
circled times = vector product &otimes; &#8855; &#x2297;
up tack = orthogonal to = perpendicular &perp; &#8869; &#x22A5;
dot operator &sdot; &#8901; &#x22C5;
left ceiling = APL upstile &lceil; &#8968; &#x2308;
right ceiling &rceil; &#8969; &#x2309;
left floor = APL downstile &lfloor; &#8970; &#x230A;
right floor &rfloor; &#8971; &#x230B;
left-pointing angle bracket = bra &lang; &#9001; &#x2329;
right-pointing angle bracket = ket &rang; &#9002; &#x232A;
lozenge &loz; &#9674; &#x25CA;
black spade suit &spades; &#9824; &#x2660;
black club suit = shamrock &clubs; &#9827; &#x2663;
black heart suit = valentine &hearts; &#9829; &#x2665;
black diamond suit &diams; &#9830; &#x2666;