Category: programming

Find Minimum Distance Between List of Points

This is a neat little call I whipped up that returns the two x and y points from a list which have the shortest distance. It also returns the distance between those two points.

Pretty weird solution right? Seems to work though!

Flatten Data in R by Column Values

Not sure if this will help anyone, but I recently wrote a script that will let you flatten/widen a data set based on a dynamically set column name.

Animal Instances Instances Instances Instances
Puppy 47 77 97 25
Puppy 26 6 14 98
Kitten 38 50 5 73
Puppy 83 61 43 4
Kitten 78 30 57 8

…would turn into this

V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
Puppy 47 77 97 25 26 6 14 98 83 61 43 4
Kitten 38 50 5 73 78 30 57 8

Breaks a vector into defined segment lengths

Another gist written for stack overflow. This breaks up a vector defined lengths and accounts for the remainder size. Great for splitting up a vector for saving into different files. It is a loop, but any performance issues should be minimal and increases readability/extendability.

Find Patterns in R Vector

Wrote this while providing a solution on Stack Overflow. Figured it might actually come in handy so I saved it. Not the most elegant, and not vectorized, but it might be useful to others as well.

Dynamically Set Accordion Navigation

One of my friends who works for a fairly large company (loooooots of internal links) needed a way to structure the left navigation in such a way that the links would take you to the page, but the list element/clickable element would keep you on the page and expand/contract the navigation. There weren’t any good solutions around that he could find, so I decided to help him out.

This solution is not only an accordion for the menu, but dynamically adds indicators to what list elements are a dropdown and since it makes the list element handle the navigation menu, any element in the list element (including ::before content) will let you expand or collapse the menu system. Clicking on a link will take you to the page, and cancel any accordion effects.

See the Pen VYraBx by Matt Sandy (@MattSandy) on CodePen.

Hopefully this helps someone else out there as well.

Threaded PHP Download

I wrote this code for my bandcamp download project which has since been retired. It breaks up a download into different chunks and saves them to chunk.part1, chunk.part2, etc. I was pretty happy since it sped up my downloads a lot.

Return a Date Range Array in PHP

I think this code might come in handy for me as well as other programmers. It lets you pass two dates and returns an array containing the range of dates between them. I had found code previously, but there was a glitch and so I decided to just write my own.

Numeric equivalent of an Excel column name

I wrote this little php function to see how small I could make the code. I think I did pretty well and figured I would post it here as to not forget in case I needed to actually use it some day. There might be a better way, but I had some fun with this.

function b($b){$l=strlen($b);while($i<$l){$c+=(base_convert($b[$i],36,10)-9)*pow(26,$l-1-$i);$i++;}return $c;}

Use PHP to Display EXIF Data from Flickr

Speacial Note: If you want this in a format you can copy/paste click here.

I recently added EXIF data to my photo pages in my portfolio with information like Camera used and the ISO setting. I decided it would be good to share this with the PHP community in case anyone was curious or is having trouble. I don’t work with XML too often, so there might be a more efficient way of doing this.

This is just a function for displaying the particular EXIF data. You should look at the output in case you want to change the information from clean to raw for particular things, or add data for checking if there is a clean and if not then return the raw data.

function grabExif($id)
	$url		= "";
	$url		.= "$id";
	$content 	= file_get_contents("$url");
	$xml 		= new SimpleXMLElement($content);
	//echo $content;
	foreach ($xml->photo->exif as $exifData)
		foreach($exifData->attributes() as $a => $b) {
			//echo $a,'="',$b,"\"\n";
				$model[count($model)] = (string) $exifData->raw;
			else if(($b=="Make")&&($a=="label"))
				$make[count($make)] = (string) $exifData->raw;
			else if(($b=="ISO Speed")&&($a=="label"))
				$ISO[count($ISO)] = (string) $exifData->raw;
			else if(($b=="Aperture")&&($a=="label"))
				$aperture[count($aperture)] = (string) $exifData->clean;
			else if(($b=="Exposure")&&($a=="label"))
				$shutterSpeed[count($shutterSpeed)] = (string) $exifData->raw;
		return "Taken with the " . $model[0] . " at " . $aperture[0] . " with an exposure time of " . $shutterSpeed[0] . "s using ISO" . $ISO[0];
		return "No exif data provided";

Just an FYI, getting a syntax highlighter running in WordPress properly can get dicey. I went with Dean’s Code Highlighter and added

$content = htmlspecialchars_decode($content);

to line 126 of deans_code_highlighter.php under

function ch_the_content_filter($content)

in order to display the ampersands properly. Sometimes WordPress thinks it knows best when it comes to formatting posts. I may update this post later, but this should be enough to go on for all of you PHP/Flickr developers out there.