扩展和重载 MS 和点类型

发布于 2024-10-09 13:13:07 字数 1039 浏览 2 评论 0原文

我是否可以创建自己的点和向量类型来重载它们?为什么这不起作用?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2024-10-16 13:13:07

我真的不明白你想用这一行实现什么:

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文