Monday, January 16, 2012

Monday 1-16-12

namespace VertexDemo
{
//format a type with ToString()
struct Vertex3d : IFormattable, IEquatable, IComparable
{
private int _id;

private double _x;
private double _y;
private double _z;

public int Id
{
get
{
return _id;
}

set
{
_id = value;
}
}

public double X
{
get { return _x; }
set { _x = value; }
}

public double Y
{
get { return _y; }
set { _y = value; }
}

public double Z
{
get { return _z; }
set { _x = value; }
}

public Vertex3d(double x, double y, double z)
{
this._x = x;
this._y = y;
this._z = z;

_id = 0;

}

public int CompareTo(Vertex3d other)
{
if (_id < other._id)
return -1;
if (_id == other._id)
return 0;
return 1;
}

public double this[int index]
{
get
{
switch (index)
{
case 0: return _x;
case 1: return _y;
case 2: return _z;
default: throw new ArgumentOutOfRangeException("index", "Only indexes 0-2 valid!");
}
}
set
{
switch (index)
{
case 0: _x = value; break;
case 1: _y = value; break;
case 2: _z = value; break;
default: throw new ArgumentOutOfRangeException("index", "Only indexes 0-2 valid!");
}
}

}

public double this[string dimension]
{
get
{
switch (dimension)
{
case "x":
case "X": return _x;
case "y":
case "Y": return _y;
case "z":
case "Z": return _z;
default: throw new ArgumentOutOfRangeException("dimension", "Only dimensions X, Y, and Z are valid!");
}
}
set
{
switch (dimension)
{
case "x":
case "X": _x = value; break;
case "y":
case "Y": _y = value; break;
case "z":
case "Z": _z = value; break;
default: throw new ArgumentOutOfRangeException("dimension", "Only dimensions X, Y, and Z are valid!");
}
}
}

public string ToString(string format, IFormatProvider formatProvider)
{
//"G" is .Net's standard for general formatting
//all times should support it
if (format == null) format = "G";

//is the user providing their own format provider?
if (formatProvider != null)
{
ICustomFormatter formatter = formatProvider.GetFormat(this.GetType()) as ICustomFormatter;

if (formatter != null)
{
return formatter.Format(format, this, formatProvider);
}
}

//formatting is up to us, so let'd do it
if (format == "G")
{
return string.Format("({0}, {1}, {2})", X, Y, _z);
}

StringBuilder sb = new StringBuilder();
int sourceIndex = 0;

while (sourceIndex < format.Length)
{
switch (format[sourceIndex])
{
case 'X':
sb.Append(X.ToString());
break;
case 'Y':
sb.Append(Y.ToString());
break;
case 'Z':
sb.Append(X.ToString());
break;
default:
sb.Append(format[sourceIndex]);
break;
}
sourceIndex++;
}
return sb.ToString();
}

public override bool Equals(object obj)
{
if (obj == null)
return false;
if (obj.GetType() != this.GetType())
return false;
return Equals((Vertex3d)obj);
}

public bool Equals(Vertex3d other)
{
return this._x == other._x
&& this._y == other._y
&& this._z == other._z;
}
}


class TypeFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter)) return this;
return Thread.CurrentThread.CurrentCulture.GetFormat(formatType);
}

public string Format(string format, object arg, IFormatProvider formatProvider)
{
string value;
IFormattable formattable = arg as IFormattable;
if (formattable == null)
{
value = arg.ToString();
}
else
{
value = formattable.ToString(format, formatProvider);
}
return string.Format("Type: {0}, Value: {1}", arg.GetType(), value);
}
}

class Program
{
static void Main(string[] args)
{
Vertex3d v = new Vertex3d(1.0, 2.0, 3.0);
Vertex3d v2 = new Vertex3d(4.0, 5.0, 6.0);
Vertex3d v3 = new Vertex3d(1.0, 2.0, 3.0);
TypeFormatter formatter = new TypeFormatter();
StringBuilder sb = new StringBuilder();
sb.AppendFormat(formatter, "{0:(X Y)}; {1:[X, Y, Z]}", v, v2);
Console.WriteLine(sb.ToString());
Console.WriteLine("v == v3 : {0}", v.Equals(v3));
Console.WriteLine("v == v2: {0}", v.Equals(v2));

Vertex3d vNew = new Vertex3d(1, 2, 3);
Console.WriteLine(vNew[0]);
Console.WriteLine(vNew["Z"]);
Console.WriteLine(vNew["y"]);
vNew.Id = 15;


}
}
}

No comments:

Post a Comment