Tuesday, September 13, 2005

Sleeping with the enemy [1991]

A great film, fine idea and well played out. A clever woman escaping the clutches of her tyrant husband.

Well worth another watch if it appears on tv.

http://www.imdb.com/title/tt0102945/

Monday, September 12, 2005

Vertical label component in c#

Some code for a vertical label in c#

Developed this for a project, but don't need it. Translated it from a Vb.net example on the web, and extended it a little

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Whatever
{
///


/// VerticalLabel is a component that displays a label, but instead of the
/// text being horizontal it is displayed vertically.
///
/// This is achieved by drawing using the specified Font and overriding
/// the OnPaint method to draw with the font rotated.
///
/// Windows designer support is also included, the default text and alignment
/// can be set in the designer
///

///
public class VerticalLabel : System.Windows.Forms.Control
{

///
/// internal variable for the current label text
///

private string labelText;

///
/// internal variable for the alignment of the vertical text
///

private System.Drawing.ContentAlignment labelTextAlign;

#region default constructor, destructor and initialisation code
public VerticalLabel()
{
//base.New();
InitializeComponent();
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!((components == null)))
{
components.Dispose();
}
}
base.Dispose(disposing);
}



private System.ComponentModel.Container components;

[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.Size = new System.Drawing.Size(24, 100);
}

#endregion

///
/// Override the onPaint method to draw a string vertically on the screen
///

/// default PaintEventArgs parameter
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
float sngControlWidth;
float sngControlHeight;
float sngTransformX;
float sngTransformY;
Color labelColor = this.BackColor;
Pen labelBorderPen = new Pen(labelColor, 0);
SolidBrush labelBackColorBrush = new SolidBrush(labelColor);
SolidBrush labelForeColorBrush = new SolidBrush(base.ForeColor);
base.OnPaint(e);
sngControlWidth = this.Size.Width;
sngControlHeight = this.Size.Height;
e.Graphics.DrawRectangle(labelBorderPen, 0, 0, sngControlWidth, sngControlHeight);
e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, sngControlWidth, sngControlHeight);
sngTransformX = 0;
sngTransformY = sngControlHeight;
e.Graphics.TranslateTransform(sngTransformX, sngTransformY);
e.Graphics.RotateTransform(270);

#region figure out offset to achieve the desired alignment
//default to left alignment
float leftOffset = 0;


//handle center alignment
if(this.labelTextAlign == System.Drawing.ContentAlignment.BottomCenter
this.labelTextAlign == System.Drawing.ContentAlignment.MiddleCenter
this.labelTextAlign == System.Drawing.ContentAlignment.TopCenter )
{
System.Drawing.SizeF sf = e.Graphics.MeasureString(this.labelText, Font);
leftOffset = (this.Size.Height - sf.Width) / 2;
}
//handle right alignment
if(this.labelTextAlign == System.Drawing.ContentAlignment.BottomRight
this.labelTextAlign == System.Drawing.ContentAlignment.MiddleRight
this.labelTextAlign == System.Drawing.ContentAlignment.TopRight )
{
System.Drawing.SizeF sf = e.Graphics.MeasureString(this.labelText, Font);
leftOffset = this.Size.Height - sf.Width;
}
#endregion

e.Graphics.DrawString(labelText, Font, labelForeColorBrush, leftOffset, 0);
}


///
/// Invalidate on resize event
///

///
protected override void OnResize(EventArgs e)
{
Invalidate();
base.OnResize (e);
}



#region windows form designer support
///
/// Windows designer Text setting
///

[Category("Verticallabel"), Description("Text is displayed vertiaclly in container")]
public override string Text
{
get
{
return labelText;
}
set
{
labelText = value;
Invalidate();
}
}
[Category("Verticallabel"), Description("Text Alignment")]
public System.Drawing.ContentAlignment TextAlign
{
get
{
return labelTextAlign;
}
set
{
labelTextAlign = value;
Invalidate();
}
}



#endregion



}
}

Tuesday, September 06, 2005

Variance calculation in c#

I found this algorithm a little tricky to get right, so I thought I should post it.

It calculates the variance for a set of input data using the formula Excel uses

private double CalculateVariance(double[] input)
{
double sumOfSquares = 0.0;
double total = 0.0;
foreach(double d in input)
{
total+=d;
sumOfSquares += Math.Pow(d, 2);
}
int n = input.Length;
return ((n * sumOfSquares) - Math.Pow(total, 2)) / (n * (n - 1));
}