English 中文(简体)
发现甲壳物体的概率和面积? (C#)
原标题:Finding Perimeter and area of a Rectangle object? (C#)
  • 时间:2009-12-02 07:55:41
  •  标签:
  • c#
  • oop

我知道,寻找试采区的方法只是很长的一段时期, *的公式是2(宽)+2(宽)。 我的问题是,找到由其他物体构成的试金物体的地区和周边的最有效方式是什么?

<My Code Snippet:

class Rectangle
{

public Line left { get; set; }
public Line top { get; set; }
public Line right { get; set; }
public Line bottom { get; set; }

public Rectangle() : this(new Line(new Point(), new Point())) { }

public Rectangle(Line diaganol)
{
    Point beginningDiagonalPoint = diaganol.startPoint;
    Point endingDiagonalPoint = diaganol.endPoint;

    int begXC = beginningDiagonalPoint.xCoord;
    int begYC = beginningDiagonalPoint.yCoord;

    int endXC = endingDiagonalPoint.xCoord;
    int endYC = endingDiagonalPoint.yCoord;



    Point rightSideEnd = new Point(endXC, begYC);
    Point leftSideEnd = new Point(begXC, endYC);


    right = new Line(endingDiagonalPoint, rightSideEnd);
    left = new Line(beginningDiagonalPoint, leftSideEnd);
    top = new Line(leftSideEnd, endingDiagonalPoint);
    bottom = new Line(rightSideEnd, beginningDiagonalPoint);
}
}

我想写出两种方法,一种是计算该地区,另一种是划定周边,我应如何用物体处理?

我知道,我可以把 x子——先是 co子,最后是 co——花了很长的时间,但是否有另一种和(或)更好的方法用物体做?

<><>>> 感谢!

最佳回答

页: 1

页: 1

......假定点数按原样X和方向排列(另加附每个轴数,如abs(endXC- begXC)

如果可能叫区和环绕区,你可能会想计算和安放;将这些地区储存在地下。

问题回答

试金星区永远是:

area = width * height

周边国家总有:

perimeter = width * 2 + height * 2

不管你如何到达宽度或高度,情况总是一样。

你的班子 施工人<>/em>界定了一条 rec,但由于线上没有保护,外部代码可能会将其改变为平行图、其他四边或只是脱节线。

你回到正确的轨道上来,虽然--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

你们不需要计算“区域”或“地方”参数的线,你只需要最初的分点。 事实上,你根本不需要离开/站立/右边/底部,因此,最好让它们成为神圣的、只读的财产。

Area = Math.Abs( (Point1.X - Point2.X) * (Point1.Y - Point2.Y) )

Perimeter = (Math.Abs(Point1.X - Point2.X) + Math.Abs(Point1.Y - Point2.Y)) * 2

你的周边将是四条线路长度的总和。 假设线标有或能够有很长的方法。

同样,该地区是权利期限的产物,或是左边或底层。

如果你看上你的班子,那么,你在班子的构造中浪费的时间比你计算该地区和周边的损失要多。 为什么你储存线路? 要点更方便:

class Rectangle
{

public Point bottomLeft { get; set; }
public Point topRight { get; set; }
public int? area;
public int? perimeter;

public Rectangle() : this(new Line(new Point(), new Point())) { }

public Rectangle(Line diaganol)
{
    bottomLeft = diagonal.StartPoint;
    topRight = diagonal.EndPoint;
}

public int Area()
{
  if (area == null)
  {
     area = Math.Abs(diagonal.StartPoint.X - diagonal.EndPoint.X) *
            Math.Abs(diagonal.StartPoint.Y - diagonal.EndPoint.Y);
  } 
  return (int)area;
}
public int Perimeter()
{
  if (perimeter == null)
  {
     perimeter = Math.Abs(diagonal.StartPoint.X - diagonal.EndPoint.X) * 2 +
            Math.Abs(diagonal.StartPoint.Y - diagonal.EndPoint.Y) * 2;
  } 
  return (int)perimeter;
}
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...