Wednesday, April 15, 2009

Point Struct

 

Ok System.drawing contains a point struct it has the basic functionality,

Can be initialized by a int32, a point ,a size.When you initialize with a int 32 the doublewords(yes your int) hi word is x and lo word is y

ex: if you want a point (12,12) 786444 is the magic number as you know 12’s hex code is C  000C000C is the double word.

   1: private static int HIWORD(int n)
   2:       {
   3:           return ((n >> 0x10) & 0xffff);
   4:       }
   5:  
   6:       private static int LOWORD(int n)
   7:       {
   8:           return (n & 0xffff);
   9:       }

Also it is interesting microsoft does not allow us to add or substract points, i think it is forgotten or somethink like that.Because you can add points, basicly a point can be thought as a vector whose starting point is the origin, so as you can add 2 vectors you can add points you can add points and sizes and can convert sizes to point so we must have the ability to add points.Because Micosoft defined the point as struct you can’t inherit a new class too.

Here is the source of the point which i added implicit convervsion to system.drawing.point and added add operator to add 2 points.

   1: namespace SystemX.DrawingX
   2: {
   3:     using System;
   4:     using System.Drawing;
   5:     using System.ComponentModel;
   6:     using System.Globalization;
   7:     using System.Runtime.InteropServices;
   8:  
   9:     [Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true), TypeConverter(typeof(PointConverter))]
  10:     public struct Point
  11:     {
  12:         public static readonly Point Empty;
  13:         private int x;
  14:         private int y;
  15:         public Point(int x, int y)
  16:         {
  17:             this.x = x;
  18:             this.y = y;
  19:         }
  20:  
  21:         public Point(Size sz)
  22:         {
  23:             this.x = sz.Width;
  24:             this.y = sz.Height;
  25:         }
  26:  
  27:         public Point(int dw)
  28:         {
  29:             this.x = (short) LOWORD(dw);
  30:             this.y = (short) HIWORD(dw);
  31:         }
  32:  
  33:         [Browsable(false)]
  34:         public bool IsEmpty
  35:         {
  36:             get
  37:             {
  38:                 return ((this.x == 0) && (this.y == 0));
  39:             }
  40:         }
  41:         public int X
  42:         {
  43:             get
  44:             {
  45:                 return this.x;
  46:             }
  47:             set
  48:             {
  49:                 this.x = value;
  50:             }
  51:         }
  52:         public int Y
  53:         {
  54:             get
  55:             {
  56:                 return this.y;
  57:             }
  58:             set
  59:             {
  60:                 this.y = value;
  61:             }
  62:         }
  63:         public static implicit operator PointF(Point p)
  64:         {
  65:             return new PointF((float) p.X, (float) p.Y);
  66:         }
  67:  
  68:         public static implicit operator System.Drawing.Point(Point p)
  69:         {
  70:             return new System.Drawing.Point(p.X, p.Y);
  71:         }
  72:  
  73:         public static explicit operator Size(Point p)
  74:         {
  75:             return new Size(p.X, p.Y);
  76:         }
  77:  
  78:         public static Point operator +(Point pt, Size sz)
  79:         {
  80:             return Add(pt, sz);
  81:         }
  82:         public static Point operator +(Point pt, Point pt2)
  83:         {
  84:             return Add(pt, pt2);
  85:         }
  86:  
  87:         public static Point operator -(Point pt, Point pt2)
  88:         {
  89:             return Subtract(pt, pt2);
  90:         }
  91:  
  92:         public static Point operator -(Point pt, Size sz)
  93:         {
  94:             return Subtract(pt, sz);
  95:         }
  96:  
  97:         public static bool operator ==(Point left, Point right)
  98:         {
  99:             return ((left.X == right.X) && (left.Y == right.Y));
 100:         }
 101:  
 102:         public static bool operator !=(Point left, Point right)
 103:         {
 104:             return !(left == right);
 105:         }
 106:  
 107:         public static Point Add(Point pt, Size sz)
 108:         {
 109:             return new Point(pt.X + sz.Width, pt.Y + sz.Height);
 110:         }
 111:  
 112:         public static Point Add(Point pt, Point pt2)
 113:         {
 114:             return new Point(pt.X + pt2.X, pt.Y + pt2.Y);
 115:         }
 116:  
 117:         public static Point Subtract(Point pt, Point pt2)
 118:         {
 119:             return new Point(pt.X - pt2.X, pt.Y - pt2.Y);
 120:         }
 121:  
 122:         public static Point Subtract(Point pt, Size sz)
 123:         {
 124:             return new Point(pt.X - sz.Width, pt.Y - sz.Height);
 125:         }
 126:  
 127:         public static Point Ceiling(PointF value)
 128:         {
 129:             return new Point((int) Math.Ceiling((double) value.X), (int) Math.Ceiling((double) value.Y));
 130:         }
 131:  
 132:         public static Point Truncate(PointF value)
 133:         {
 134:             return new Point((int) value.X, (int) value.Y);
 135:         }
 136:  
 137:         public static Point Round(PointF value)
 138:         {
 139:             return new Point((int) Math.Round((double) value.X), (int) Math.Round((double) value.Y));
 140:         }
 141:  
 142:         public override bool Equals(object obj)
 143:         {
 144:             if (!(obj is Point))
 145:             {
 146:                 return false;
 147:             }
 148:             Point point = (Point) obj;
 149:             return ((point.X == this.X) && (point.Y == this.Y));
 150:         }
 151:  
 152:         public override int GetHashCode()
 153:         {
 154:             return (this.x ^ this.y);
 155:         }
 156:  
 157:         public void Offset(int dx, int dy)
 158:         {
 159:             this.X += dx;
 160:             this.Y += dy;
 161:         }
 162:  
 163:         public void Offset(Point p)
 164:         {
 165:             this.Offset(p.X, p.Y);
 166:         }
 167:  
 168:         public override string ToString()
 169:         {
 170:             return ("{X=" + this.X.ToString(CultureInfo.CurrentCulture) + ",Y=" + this.Y.ToString(CultureInfo.CurrentCulture) + "}");
 171:         }
 172:  
 173:         private static int HIWORD(int n)
 174:         {
 175:             return ((n >> 0x10) & 0xffff);
 176:         }
 177:  
 178:         private static int LOWORD(int n)
 179:         {
 180:             return (n & 0xffff);
 181:         }
 182:  
 183:         static Point()
 184:         {
 185:             Empty = new Point();
 186:         }
 187:     }
 188: }
 189:  

No comments: