Development
Japanese
Personal

# Free Code, Freely Given

Solutions to common problems, tricky code I learned a lesson on, and other code that may be of use to someone, somewhere, sometime.

## UNIX Auto-Organization

I'm always amazed at how useful this tiny little script has been to me over the past two years. Originally built to help me organize large collections of data and files on my QNAP-TS210 Pro, it will create an A through Z folder structure in the current directory and move all files that begin with each letter into its respective folder. I really should expand it to do further sub-foldering such that the "A" folder would contain "Aa" through "Az".

```for letter in 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
do
mkdir \$letter; mv \$letter*.* ./\$letter
done```

## iPhone Scale and Rotate

The iPhone SDK does not include code to detect a two-finger zoom/rotate (like when you zoom in on a photo or webpage). Here's what I've figured out, based loosely on others' examples, some basic geometry, and some sweat.

```- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

//Code for rotation came from smart maths at
//http://forums.macrumors.com/archive/index.php/t-508042.html
//which led me to http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
//which I then simplified down to a cached "rotation angle" and looked at angle of rotation
//for the current point vs. previous point
if ([touches count] == 2) {
//NSLog(@"multitouch called!");
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGPoint previousPoint1 = [touch1 previousLocationInView:nil];
CGPoint previousPoint2 = [touch2 previousLocationInView:nil];
CGFloat previousAngle = atan2 (previousPoint2.y - previousPoint1.y, previousPoint2.x - previousPoint1.x);

CGPoint currentPoint1 = [touch1 locationInView:nil];
CGPoint currentPoint2 = [touch2 locationInView:nil];
CGFloat currentAngle = atan2 (currentPoint2.y - currentPoint1.y, currentPoint2.x - currentPoint1.x);
CGFloat angleChange = currentAngle - previousAngle;
CGFloat totalAngle = transformRotationAngle + angleChange;

CGFloat previousDistance = sqrt(pow((previousPoint2.y - previousPoint1.y), 2.0) + pow((previousPoint2.x - previousPoint1.x), 2.0));
CGFloat currentDistance = sqrt(pow((currentPoint2.y - currentPoint1.y), 2.0) + pow((currentPoint2.x - currentPoint1.x), 2.0));
CGFloat distanceChange = currentDistance - previousDistance;
CGFloat scaleChange = distanceChange/100;
CGFloat totalScale = transformScale + scaleChange;

CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(totalAngle);
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(totalScale, totalScale);
CGAffineTransform fullTransform = CGAffineTransformConcat(rotateTransform, scaleTransform);
self.transform = fullTransform;

transformRotationAngle = totalAngle;
transformScale = totalScale;
}
}
```