スキップしてメイン コンテンツに移動

投稿

ActionScript 3.0: Dictionary Utility Methods (Delete All Entries, Count All Entries, etc.))

public class DictionaryUtils { public function DictionaryUtils() { } public static function forEachBySortedValue(dict:Dictionary, compare:Function, callback:Function):void { var v:Vector.<Object> = extractValues(dict); v.sort(compare); for each(var value:Object in v) { callback(value); } } public static function forEachBySortedKey(dict:Dictionary, compare:Function, callback:Function):void { var v:Vector.<Object> = extractKeys(dict); v.sort(compare); for each(var key:Object in v) { callback(key, dict[key]); } } public static function extractValues(dict:Dictionary):Vector.<Object> { var v:Vector.<Object> = new Vector.<Object>(); for each(var value:Object in dict) { v.push(value); } return v; } public static function extractKeys(dict:Dictionary):Vector.<Object> { var v:Vector.<Object> = new Vector.<Object>(); for (var key:Object in dict) { v.push(key); } return v; } pu

ActionScript 3.0: Digging Directory Recursively And Process Each File

This class aims to parse directory and do something for each file. You should just override proper methods, then call process method like this. new DirectoryDigger().process("c:/temp"); package utils.file { import flash.filesystem.File; public class DirectoryDigger { public function DirectoryDigger() { } public function process(path:String):Object { var file:File = new File(path); preProcess(file); processBody(file); return postProcess(file); } protected function processBody(file:File):void { if (file.isDirectory) { preProcessDir(file); var files : Array = file.getDirectoryListing(); for (var i : int = 0; i < files.length; i++) { processBody(files[i] as File); } postProcessDir(file);

C#: Capture Screen

Here is the code for capturing current screen in given Rectangle region. public static Bitmap CaptureFromScreen(Rectangle rc) { var bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(bmp)) { g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy); } return bmp; } You can give something like below as a Rectangle argument // full screen area Screen.PrimaryScreen.Bounds; // 640 x 480 rectamgle from left side corner new Rectangle(0, 0, 640, 480); // working area Screen.PrimaryScreen.WorkingArea; The following links are helpful. http://homepage1.nifty.com/yasunari/VB/VB2005/DrawToScreen.htm : Sorry this page is written in Japanese... http://www.bobpowell.net/capture.htm http://msdn.microsoft.com/en-us/library/dd144871(VS.85).aspx http://msdn.microsoft.com/en-us/library/dd162920(VS.85).aspx

Solving Unimodal Function by Brent's Method

Introduction I faced a mathematical problem for solving a root from a unimodal function like quadratic function. Of course we can solve the root if the function is mono decreasing or mono increasing in the target range. However if the function is in potentially there are 2 solutions or no solutions in the target range. I know this article is not perfect yet. I will keep improving this article :) Main Requirements There are mainly 2 requirements when solving the root. Minimize the function call as much as possible: because the function needs heavy calculation Robustness against oscillating: Maybe the below figure helps your understanding. If we solve the root strictly, there can be a lot of solutions in the range because of the small oscillating. However we should choose reasonable accrual solution from candidate points. Solution I have tried Newton method, but it didn’t work well because of the above bullet point 2. I have also tried fitting 3 dimensional function

C#: Extract A Href Links from Html Text

// I know 1, 2, 3 is bad grouping name X( private readonly static Regex LINK_REGEX = new Regex( @"<a\s+[^>]*href\s*=\s*(?:(?<3>'|"")(?<1>[^\3>]*?)\3|(?<1>[^\s>]+))[^>]*>(?<2>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled ); public static void ExtractLinks(string text, ICollection<string> links) { LINK_REGEX.ApplyAllMatched(text, (m) => links.Add(m.Groups[1].Value)); } Helper method which processes matched string for each. public static void ApplyAllMatched(this Regex regex, string text, Action apply) { for (var m = regex.Match(text); m.Success; ) { apply(m); m = m.NextMatch(); } }

C#: Save Image as JPEG or PNG format

Just a simple code tips for saving image as JPEG or PNG format in C#. Save image as JPEG with Quality parameter public static readonly ImageCodecInfo JPEG_CODEC = GetEncoderInfo("image/jpeg"); public static void SaveImageAsJpeg(this Image src, string fileName, int quality) { var eps = new EncoderParameters(1); var ep = new EncoderParameter(Encoder.Quality, quality); eps.Param[0] = ep; src.Save(fileName.ChangeExtension("jpg"), JPEG_CODEC, eps); } public static ImageCodecInfo GetEncoderInfo(string mineType) { return ImageCodecInfo.GetImageEncoders().First(enc => enc.MimeType == mineType); } Save image as PNG public static void SaveImageAsPng(this Image src, string fileName) { src.Save(fileName.ChangeExtension("png"), ImageFormat.Png); }

C#: Resize Image

// Default parameters are almost the highest quality setting. public static Bitmap Resize(this Image src, int w, int h, SmoothingMode smoothingMode = SmoothingMode.AntiAlias, InterpolationMode interpolationMode = InterpolationMode.HighQualityBicubic, PixelOffsetMode pixelOffsetMode = PixelOffsetMode.HighQuality) { var newImage = new Bitmap(w, h); using (var gr = Graphics.FromImage(newImage)) { gr.SmoothingMode = smoothingMode; gr.InterpolationMode = interpolationMode; gr.PixelOffsetMode = pixelOffsetMode; gr.DrawImage(src, 0, 0, w, h); } return newImage; } // resize image based on given percentage public static Bitmap ResizedByPercentage(this Image src, double percent) { var rW = (int)Math.Round(src.Width * percent, 0); var rH = (int)Math.Round(src.Height * percent, 0); return src.Resize(rW, rH); } // resize image limited in given w, h parameter public static Bitmap ResizeImageIn(this Image src, int w, i