Algorithms: misc
From wikinotes
Horizontal to Vertical Array
/* ( result: ) --> to --> ( result: ) 0, 1 , 2, 0, 4, 8, 3, 4 , 5, 1, 5, 9, 6, 7 , 8, 2, 6, 10, 9, 10, 11, 3, 7, 11, */ int $colSize = 3; int $rowSize = 4; string $value[] = { "0" , "1" , "2", "3" , "4" , "5", "6" , "7" , "8", "9" , "10", "11" }; // Convert horizontal array to vertical array // int $count=0; string $newValue[]; clear($newValue); for ($i=0; $i<$colSize; $i++) { // for each column for ($ii=0; $ii<$rowSize; $ii++) { // for each row $columnTop = ($ii * $colSize); // (row * numCol) = the top of each column $index = ($columnTop + $i); // (top of column) + currentColumn = column, then down $newValue[$index] = $value[$count]; print ( $index +"--"+ $count +"\n"); $count++; } } // print newGraph // int $count = 0; // ( result: ) for ($i=0; $i<size($newValue); $i++) { // 0, 4, 8, if ($count < $colSize) { // 1, 5, 9, print ($newValue[$i] + ", "); // 2, 6, 10, $count++; // 3, 7, 11, } else { print "\n"; print ($newValue[$i] +", "); $count = 1; } }
scale video within bounding box
self.width = 100 ## the height/width of the bounding self.height = 150 # box that the video is contained in. avg_width = 80 ## the height/width of the video we want avg_height = 120 # to occupy the maximum amount of space within the bounding-box logger.debug('calculating height scaled based on width') width_scaled_height = ( self.width * avg_width ) / avg_height logger.debug('calculating width scaled based on height') height_scaled_width = ( self.height * avg_height ) / avg_width if self.width > self.height: logger.debug('bounding box is wider than tall') if width_scaled_height <= self.height: logger.debug('width_scaled_height >= self.height') self.video_width = self.width self.video_height = width_scaled_height else: logger.debug('width_scaled_height < self.height') self.video_height = self.height self.video_width = height_scaled_width else: logger.debug('bounding box is taller than wide') if height_scaled_width <= self.width: logger.debug('height_scaled_width >= self.width') self.video_height = self.height self.video_width = height_scaled_width else: logger.debug('height_scaled_width < self.width') self.video_width = self.width self.video_height = width_scaled_height