开发者

Extend and Overload MS and Point Types

开发者 https://www.devze.com 2023-02-01 13:06 出处:网络
Do I have make my own Point and Vector types to overload them ? Why does this not work ? namespace System . windows

Do I have make my own Point and Vector types to overload them ? Why does this not work ?

namespace System . windows
{
public partial struct Point : IFormattable
{
    public static Point operator * ( Point P , double D )
    {
        Point Po = new Point ( );
        return Po;
    }
}
}

namespace SilverlightApplication36
{
public partial class MainPage : UserControl
{

    public static void ShrinkingRectangle ( WriteableBitmap wBM , int x1 , int y1 , int x2 , int y2 , Color C )
    {
        wBM . DrawRectangle ( x1 , y1 , x2 , y2 , Colors . Red );
        Point Center = Mean ( x1 , y1 , x2 , y2 );
        wBM . SetPixel ( Center , Colors.Blue , 3 );
        Point P1 = new Point ( x1 , y1 );
        Point P2 = new Point ( x1 , y2 );
        Point P3 = new Point ( x1 , y2 );
        Point P4 = new Point ( x2 , y1 );
        const int Steps = 10;
        for ( int i = 0 ; i < Steps ; i++ )
        {
            double iF = (double)(i+1) / (double)Steps;
            double jF = ( 1.0 - iF );
            Point P11 = **P1 *开发者_如何学运维 jF;**
        }
    }


I don't really understand what you're trying to achieve with this line:

Point P11 = **P1 * jF;** 

If you try to power it then use the Math.Pow function.

Update You should have an internal field in the structure that represents your values, then implementing the operator is very easy.

As for the IFormattable, I didn't really test what I am writing, I just copied the code from here to give you the idea:

public partial struct Point : IFormattable
{
  private double x;
  public double X
  {
    get { return x; }
  }

  private double y;
  public double Y
  {
    get { return y; }
  }

  public static Point operator *(Point point, double value)
  {
    return new Point(point.X * value, point.y * value);
  }

  public Point(double x, double y)
  {
    this.x = x;
    this.y = y;
  }

  #region IFormattable Members

  public string ToString(string format, IFormatProvider formatProvider)
  {
    if (format == null) format = "H"; //hyphenized


    if (formatProvider != null)
    {
      ICustomFormatter formatter = 
        (ICustomFormatter)formatProvider.GetFormat(this.GetType());

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

    switch (format)
    {
      case "X:
        return string.Format("{0}x{1}", X, Y);
      case "C":
        return string.Format("{0}, {1}", X, Y);
      case "H":
      default:
        return string.Format("{0}-{1}", X, Y); 
    }                                          
  }

  #endregion
}
0

精彩评论

暂无评论...
验证码 换一张
取 消