Project Euler

by Administrator 26. April 2007 15:26

Someone turned me on to Project Euler last night and I have to admit its quite a gem.  From the site:

"Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems."

Each problem should take less than a minute to solve once the solution is implemented; my newer computer gives me a much needed handicap.  This is excellent for me since Math is an area where I've not been very successful at self study.  I didn't register yet but I solved #1 and #2 as fast as I could type and then began over-engineering my solution to #3 by attempting to implement a Sieve of Atkin in C#.

Tags:

Consoles need remote desktop

by Administrator 26. April 2007 15:13

With consoles getting better at becoming livingroom media hubs and all-around entertainment devices I think remote desktop functionality should be considered in the future.  As I sit in my home office today working, it would be nice if I could force my console to wake on LAN and check for firmware updates or download content from my laptop, or see what other users are online for a quick game of Motorstorm over lunch.  What do you think?

Tags:

Designer[0]

by Administrator 24. April 2007 17:28

Hosting a Designer Surface in Windows Forms Part Zero

Note that my 1st article in this series begins with Zero because I am partial to c-family languages.  Yes, I'm hilarious.  My apologies to those with smaller screen sizes, but the screenshots need to be readable.

The first step we take is to create  UserControl that will contain the entire UI representation of the designer surface.  You could put all of this on a Form but in my case It'd be handy to have several of these open at once using a tab page interface.  On the UserControl I place a Panel with AutoScroll=true and a PropertyGrid.  The PropertyGrid should look familiar as its essentially what you've come to know and love from VS2005.  I also add another UserControl I’ve created called DefaultToolboxService.  This control has a Checkbox and a ListBox control on it.  So far I’ve got something like this:

 

As you can tell my initial goal is simple: I want to select an item in my ListBox and have it be automagically created when I click on my designer area. The next step will be to start tying in some of the plumbing to allow this to happen.  This is a good place to introduce the notion of Designer Services.

Designer Services

From the class library documentation:

Services are a foundation of the .NET Framework design-time architecture. Services provide design-time objects access to specific features and methods implemented by a service object that provides a service or services.  Let’s look at a few lines of code from the constructor of the TemplateDesignerControl:

        public TemplateDesignerControl()

        {

            InitializeComponent();

            _serviceContainer = new ServiceContainer();

            _serviceContainer.AddService(typeof(INameCreationService), new DefaultNameCreationService());

            _serviceContainer.AddService(typeof(IUIService), new DefaultUIService());

            _designerHost = new DefaultDesignerHost(_serviceContainer);

 

            _serviceContainer.AddService(typeof(IToolboxService), _toolboxSvc);

            _toolboxSvc.DesignPanel = _viewHostPnl;

            LoadToolboxItems();

ServiceContainer is a framework class that’s already implemented for me.  Obviously this instance is going to hold all the Services I need for my designer.  INameCreationService and IUIService we’ll come back to in the next article so for now we’ll look at DefaultToolBoxService.  DefaultToolBoxService was instantiated in InitializeComponent() because it is also a UserControl as mentioned above.  Now might be a good time to start modeling these relationships:

We’ll continue to flesh this diagram out as we move along.

Obviously the declaration for DefaultToolboxService, then, is...

public class DefaultToolBoxService : UserControl, IToolboxService

...because it is both a visual Control and an implementation of an important Service class.  In my case, these do not need to vary independently but the implementations could be separate.  If you wish, you can review (http://msdn2.microsoft.com/en-us/library/system.drawing.design.itoolboxservice(vs.80).aspx) the IToolBoxService interface before moving on.  The implementation is straightforward: Add the types of items you’d like to create to the ListBox and implement the IToolboxService accordingly.  The design-time environment, which in our case is also the run-time environment, can query what item is selected and attempt to create an instance of the appropriate class.  Adding items is accomplished like so:

        protected void LoadToolboxItems()

        {

            ToolboxItem labelItem = new ToolboxItem(typeof(CarSpot.TemplateDesigner.Controls.CustomLabel));

            labelItem.DisplayName = "Text";

            _toolboxSvc.AddToolboxItem(labelItem);

Running the code so far you would see a Toolbox, a Panel with nothing in it, and a PropertyGrid with no selected object.  In the next article we’ll start to look at our IDesignerHost implementation and the sequence of events that occur when I want to see a designer view of my “designed document” with a CarSpot.TemplateDesigner.Controls.CustomLabel on it.

Tags:

More designer stuff coming

by Administrator 20. April 2007 20:12

I just installed my favorite free UML tool, StarUML, on my new laptop, and started working on some UML models for the designer surface.  This weekend you can expect some more posts on this effort.

Tags:

Forcing specifc shareholder votes on Executive pay is Immoral

by Administrator 20. April 2007 18:16

http://news.yahoo.com/s/ap/20070420/ap_on_go_co/congress_executive_pay

From the article:

 "The House voted Friday to give shareholders at public corporations a voice in executive pay packages that typically equal 500 times the salaries of workers at those companies. "

Last I heard, shareholders already have a Voice in what goes on in public corporations in the form of the various voting for corporate officials that is mandated by the SEC.  You elect board members and such that you feel represent your interests.  Oddly enough this is also our system of goverment.  I do think both executive pay and our public servants' performance and subsequent patterns of perpetual  re-election are disconnected from reality.  We keep putting people in public office who sell us down the river and we manage to forget this and put the same Senator/Governor/House Rep back in office the next time we have a chance.  Many executives pay are out of touch with their company performance: when an airline is going under and people's pensions are tossed overboard I don't see any reason to authorize "emergency bonuses" for key executives.  I understand the notion that you don't want these high level powerplayers who drove the company into the ground to suddenly quit but something about this doesn't sit right.  Many executives absolutely deserve what they have and I often point out Bill Gates or Warren Buffet as examples of leaders who are, well, leading.

The specifics of the bill are not in the AP article, but I wonder what this is actually going to accomplish?  Will I be forced to stop signing Proxy documents and go to shareholder meetings or cast remote ballots?  Direct Democracy, anyone?  The government is already placing a number of insane and costly requirements on public companies, we don't need any more nonsense.

We have all the tools we need to improve our nation: freely available information, access to our own bank accounts, and the right to vote are some of the more important ones.  We just repeatedly choose not to use these tools.

Tags:

Image Overlay 2

by Administrator 19. April 2007 18:11

I took my sample and did it with a much cooler source image, namely the Tesla Roadster, which is absolutely the coolest car I can think of.

 

Tags:

Image Overlay

by Administrator 19. April 2007 17:35

We have a cool feature in our backend data hub we call Image Overlay.  Basically given images of a vehicle we can do all sorts of transformations including overlaying text ("Buy this car!"), overlaying images ("GM Certified logo") scaling and matte-ing (shrinking the input image down to have a single color margin to draw text in) and support for various geometric primitives.  Our implementation is one of the few in our industry and is probably far more complete, flexible, and better-er than the few competing implementations.  Our backend uses some PERL stuff for this and I wondered if a .NET engine would be as fast or faster and how it would scale, given that the claim has been made that C# is 1:1 performance with C++ and despite my love of C# I found this claim questionable.  So, supposing I have an input image which is assuredly not a car:

And a tiny screenshot of my most anticipated upcoming PS3 game, LAIR:

And I want to do a few operations in realtime in .NET:

  • create a Matte
  • scale the source image
  • Add some text
  • import the Dragon screenshot from LAIR
  • Time the operation

Here is the source code for entering some parameters, which could obviously come from some other source:

<div>

<h3>Source Image:</h3>

<asp:HyperLink ID="_exampleLnk" runat="server" Target="_blank" NavigateUrl="~/sample.jpg">

View source image

</asp:HyperLink>

<br />

Matte and overlay some text:

<asp:TextBox ID="_overlayTxt" runat="server" Text="go MSFT!"></asp:TextBox>

<br />

Import Img X <asp:TextBox runat="server" ID="_xTxt" Width="2em" Text="100"></asp:TextBox>

Import Img Y <asp:TextBox runat="server" ID="_yTxt" Width="2em" Text="100"></asp:TextBox>

<asp:Button ID="_submitBtn" runat="server" Text="Lay it over" OnClick="_submitBtn_Click" />

<br />

<asp:Label ID="_resultTimeLbl" runat="server"></asp:Label>

<br />

<asp:Image ID="_resultImg" runat="server" />

</div>

... and the code behind....

protected void _submitBtn_Click(object sender, EventArgs e)
{
string getQuery = "Image.aspx?overlay={0}&x={1}&y={2}";
int x;
int y;
int.TryParse(_xTxt.Text, out x);
int.TryParse(_yTxt.Text, out y);
_resultImg.ImageUrl = string.Format(getQuery, _overlayTxt.Text, x, y);
}

... and the result...

So, on a Windows Server 2003 running ASP.Net 2.0, how much code does it take to generate this?  Surprisingly little:

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

bool high = true;

HighResTimer hpt = new HighResTimer();
hpt.Start();
System.Drawing.Imaging.ImageCodecInfo codecInfo;
System.Drawing.Imaging.EncoderParameters parms;

System.Drawing.Imaging.ImageCodecInfo[] infoz = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
codecInfo = infoz[1];
parms = new System.Drawing.Imaging.EncoderParameters(1);
parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

string txt = Request["overlay"];
int x = int.Parse(Request["x"]);
int y = int.Parse(Request["y"]);
Response.ContentType = codecInfo.MimeType;
System.Drawing.Image newImage = new Bitmap(640, 480);
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(Server.MapPath("~/sample.jpg"));
System.Drawing.Image lairImage = System.Drawing.Image.FromFile(Server.MapPath("~/lair.jpg"));
Graphics g = Graphics.FromImage(newImage);

//Interpolation mode, smoothing mode, CompositingQuality,PixellOffsetMode,etc.
if (high)
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
}

try
{
//Matte color
g.Clear(Color.LightGray);

//Overlay font
Font textFont = new Font("Verdana", 9.0f, FontStyle.Bold | FontStyle.Underline);
Brush fontBrush = Brushes.Black;
//Scales the image down to matte it
g.DrawImage(srcImage, new RectangleF(0, 0, 600, 440));
g.DrawImage(lairImage, new RectangleF(x, y, 150, 84));
g.DrawString(txt, textFont, fontBrush, new PointF(10, 441));
g.DrawString("Damon Certified used o-scope", textFont, Brushes.Red, new PointF(430, 370));
hpt.Stop();
g.DrawString("RenderTime=" + hpt.Duration.ToString("n4") + "seconds", textFont, fontBrush, new PointF(10, 455));

//Stream
newImage.Save(Response.OutputStream, codecInfo, parms);
}
finally
{
if (null != srcImage)
{
srcImage.Dispose();
}

if (null != lairImage)
{
lairImage.Dispose();
}

if (null != g)
{
g.Dispose();
}

if (null != parms) // Seems to want to save in BMP anyway with this code in
{
parms.Dispose();
}
}
}

And I'd say 6/100th of a second on my very slow server (its an Emachine's POS we confiscated from someone after a pr0n escapade) is pretty good performance.  I have one bug in that it does not always save as a JPEG like I told it to but I'm sure a little research would clear that up.  So there you have it, a simple bit of image watermarking code in C#.

 

 

Tags:

Wave power gets smarter with GA

by Administrator 16. April 2007 16:46

http://feeds.treehugger.com/~r/treehuggersite/~3/109498577/wave_energy_gets_smarter.php

From the article:

"...Computer scientists at the University of Edinburgh are using clever software to improve the efficiency of a wave-powered generator. The machine consists of four tubes connected by hydraulic rams, which generate up to 750 kilowatts when extended and compressed by the movement of the water. When the speed and frequency of the waves changes, the machine doesn't adapt particularly well, and this is what the researchers are focusing on. By using genetic algorithms which model evolution and natural selection..."

I've done some reading about GA but have trouble thinking of problems to apply the science to, no doubt due to my incredibly shallow understanding of GA.  This is for sure an area of future research.

Tags:

Overdue Plea for Help

by Administrator 12. April 2007 21:53

I am hiring into my department @ CarSpot.com in downtown Milwaukee. 

Right now I am hoping for one Summer intern and one full time person based on the work that is going on.  We are doing cool stuff and growing.  As we staff up we are hoping to have some Google-eque policies such as spending part of your time on fun research projects.  I'm setting up a Quake server tomorrow as well.  We are not a .com company, however.

Do you...

  • Have .NET skills in C#
  • Understand design patterns
  • Understand UML
  • Enjoy drinking beer (we are upstairs from the Milwaukee Ale House)
  • Like an ultra-flexible work schedule
  • Like a small company environment
  • Multi-task well (we all wear many hats in a small company)
  • Like working with NUnit, CruiseControl, NCover
  • Have interest in developing cutting edge stuff that is far ahead of its time?

Ok, I had to throw the last one in.  There is room for help in Windows Forms, Compact Framework, ASP.net web services, ASP.net web forms, some win32/C++  dev, and probably some things I'm forgetting.  Forward your interest to damon.payne@carspot.com

 

Tags:

New Laptop

by Administrator 11. April 2007 18:26

I got a new laptop yesterday: Dell inspiron, 17" screen, core 2 duo 2.13ghz, 2gb ram@667mhz, 7200rpm serial ata drive, more 3d card than one needs unless one is playing Quake4.  Hmm, sounds like we need to install Quake4 at work for Team Building.  My previous laptop was on its 4th year of service, its 2nd round of ram and 2nd hard drive, with the 2nd one looking like it was on its way out due to more and more bad sectors being found by chkdsk.

Its amazing how fast Outlook 2007, Visual Studio 2005, etc. run with the fastest hardware Dell would currently sell me. 

 

Tags:

About the author

Damon Payne is a Microsoft MVP specializing in Smart Client solution architecture. 

INETA Community Speakers Program

Month List

Page List

flickr photostream