Primitive types

Vortex2D contains a lot of primitive types. Proper using of them can minimize code size and efforts to resolve some tasks. Here described only members of Vortex.Drawing namespace which are related to Canvas2D.API. Most of them implement well known operations: per-component adding, subtraction, multiplication, geometry function etc. Their constructors has enough overloads even to initialize types from corresponding System.Drawing types. Reverse conversion is commonly available over ToXXX() methods. Some of types has static versions of some operation methods. They operate with references and usually provide better performance. Probably some abilities of primitive types are not consistent but it will be fixed with next releases.
  1. ColorU
  2. Vector2
  3. Rect
  4. Transform2D
  5. Vertex2D

ColorU (Vortex.Drawing.ColorU)

ColorU is primary color type of Vortex2D.NET. It is named in such way to avoid name collision with System.Drawing.Color type. ColorU uses 4 byte unsigned int color representation with alpha channel support. Each component is stored in 1 byte: blue, green, red, alpha. ColorU can be created from uint value, color components.

ColorU whiteOpaque = new ColorU(255, 255, 255); //from byte components
ColorU redHalfTransparent = new ColorU(128, 255, 0, 0);
ColorU whiteHalfTransparent = new ColorU(whiteOpaque , 0.5f);
ColorU greenTransparent = new ColorU(0x0000FF00);
ColorU yellowOpaque = new ColorU(System.Drawing.Color.Yellow);
Also there are a lot of predefined colors with user friendly names: ColorU.Red, ColorU.Blue, ColorU.Green and ~140 more... All of predefined colors are opaque (alpha is maximum).
There is a method to override alpha inline: MultiplyAlpha(float factor), it allows adjust alpha multiplying with value from 0 to 1.

ColorU greenHalfTransparent = ColorU.Green.MultiplyAlpha(0.5f);

There are four operations available with ColorU type: per-component adding with saturation, per-component subtraction with saturation, per-component multiplication (as floats 0..1), linear interpolation. First three can be accessed also with operators: +, -, *.

ColorU cyanOpaque = ColorU.Blue.Add(ColorU.Green); //or ColorU.Blue + ColorU.Green;
ColorU darkRedOpaque = ColorU.Red * ColorU.Gray.Transparent(); // or .Multiply()
ColorU violetTransparent = ColorU.White - ColorU.Green; // or .Subtract()
ColorU whiteHalfTransparent = ColorU.White.Lerp(ColorU.Black.MultiplyAlpha(0.5f), 0.0f);

Vector2 (Vortex.Drawing.Vector2)

Vector2 is two-dimensional vector/point object. It consists of X, Y components. It can be created from one or two floats and also from System.Drawing 2D types (Point, PointF, Size, SizeF). It is extends functionality of Vector2 from XNA

Vector2 vec;
vec = new Vector(10); // produces {10, 10}
vec = new Vector(10, 10);
vec = new Vector(new System.Drawing.PointF(10, 10));
...
There are available additional properties: Normalized which returns normalized copy of vector, SquaredMagnitude, Magnitude, Sum (sum of X + Y).
Vector has a rich set of per-component operations: Negate(), Round(), Floor(), Ceiling(), Min(Vector vec), Max(Vector vec), Average(Vector vec), Lerp(Vector vec, float factor). Distance(Vector vec), Dot(vector vec), Angle(Vector vec) return corresponding scalar values. Addition, subtraction, multiplication and division are available as methods and operators as well.
Also static versions are available for all operations.

Vector result = new Vector2(20, 20) + new Vector2(10, 10) * new Vector2(3, 3); //result = {50, 50}
Vector result2 = new Vector2(20, 20) + new Vector2(10, 10) * 3; //result2 = {50, 50}
Vector result3 = (new Vector2(20, 20) + new Vector2(10, 10) * 3) / 10 - 4 //result3 =  {1, 1}
Vector result5 = result.Negate(); //result5 = {-50, -50}
Vector result6 = new Vector2(10, 5).Min(new Vector2 (8, 8)); //result6 = {8, 5}
float result7 = new Vector2(1, 0).Distance(new Vector2(-1, 0); //result7 = 2
bool result8 = result3 == Vector.Unit; //result8 = true;

Rect (Vortex.Drawing.Rect)

Rect is custom implementation of rectangle object for Canvas2D.API. It differs from System.Drawing.RectangleF with second point storage form. RectangleF uses width and height to keep information about rectangle size parameters. Rect stores coordinates of right/bottom point, width and height are calculated. It could be not comfortable for someone and could be changed in next releases.

Rect has enough constructors:

Rect rect;
rect = new Rect(1, 1, 10, 10); // {1, 1, 10, 10}
rect = new Rect(Vector:Zero, new Vector(100, 100)); // {0, 0, 10, 10}
rect = new Rect(new System.Drawing.PointF(1, 1), new System.Drawing.SizeF(10, 10)); // {1, 1, 11, 11}
Also there are few helper factory methods: Rect Rect.FromBox(float left, float top, float height, float width) and Rect Rect.FromPoint(float centerX, float centerY, float width, float height).

rect = Rect.FromBox(1, 1, 10, 10); // {1, 1, 11, 11}
rect = Rect.FromPoint(1, 1, 10, 10); // {-4, -4, 6, 6}
Arithmetic per-component operations are also available. But you can add Rect with Vector and scalar float types only.

Rect result = new Rect(1, 1, 10, 10) + new Vector(5, 7); // {5, 8, 15, 18}
Rect result2 = new Rect(1, 1, 10, 10) * 4; // {4, 4, 40, 40}
...
Build-in methods Contains and Intersects could be very useful for collision detection etc.

bool result = new Rect(0, 0, 10, 10).Contains(5, 5); // result = true
bool result2 = new Rect(6, 6, 10, 10).Contains(5, 5); // result2 = false
bool result3 = new Rect(10, 10, 20, 20).Intersects(new Rectangle(0, 15, 50, 25); // result3 = true

Transform2D (Vortex.Drawing.Transform2D)

Implementation of 3x2 matrix which represents affine transformations. See compiled doc reference to get more info.

Vertex2D (Vortex.Drawing.Vertex2D)

Structure which represents vertex used by Canvas API to draw geometry and sprites.

Last edited Dec 20, 2010 at 6:40 PM by AlexKhomich, version 17

Comments

No comments yet.