Расширение и перегрузка MS и типы точек - PullRequest
0 голосов
/ 30 декабря 2010

Нужно ли мне создавать свои собственные типы точек и векторов для их перегрузки?Почему это не работает?

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;**
        }
    }

1 Ответ

0 голосов
/ 30 декабря 2010

Я не совсем понимаю, чего вы пытаетесь достичь с помощью этой строки:

Point P11 = **P1 * jF;** 

Если вы попытаетесь включить его, используйте функцию Math.Pow.

Обновление У вас должно быть внутреннее поле в структуре, которое представляет ваши значения, тогда реализация оператора очень проста.

Что касается IFormattable, я не сделалЧтобы действительно проверить, что я пишу, я просто скопировал код из здесь , чтобы дать вам идею:

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
}
...