【问题标题】:initializing a 100 point array初始化一个 100 点数组
【发布时间】:2012-09-03 14:42:18
【问题描述】:

引用其他帖子:

致电Graphics.FillPolygon()。您将需要刷子而不是钢笔,并且您必须将您的点放入点 array Point[].

来自 MSDN 的示例代码是这样的:

// Create solid brush.
SolidBrush^ blueBrush = gcnew SolidBrush( Color::Blue );  

// Create points that define polygon.  
Point point1 = Point(50,50);  
Point point2 = Point(100,25);  
Point point3 = Point(200,5);       
Point point4 = Point(250,50);  
Point point5 = Point(300,100);  
Point point6 = Point(350,200);  
Point point7 = Point(250,250);  
array<Point>^ curvePoints = {point1,point2,point3,point4,point5,point6,point7}; 

这太糟糕了!我必须输入一百个等距点!

在屏幕上绘制多边形。

e->Graphics->FillPolygon( blueBrush, curvePoints );

我尝试了很多东西:

array<Point,2>^ aPoints;
//Points tabPoints[10][10];//= new Points[10][10];
Points = gcnew array<Point,2>(10,10);
//init des tableaux 

for (int i = 0;i<10;i++)
{
    for(int j =0;j<10;j++)
    {
    //tabPoints[i][j].pX =i*10;
    //tabPoints[i][j].pY = j * 10;
    // = new Points(i*10,j*10);
    aPoints[i,j]= new Point(i*20,j*20);
    }
}  

它们都不起作用!

【问题讨论】:

  • 问题是? ...
  • 真的吗? 如何在 C++/CLI 中创建 100 点数组?
  • 去掉循环内的new。你想要一个Point 值,而不是指向一个值的指针。 (MSDN 的那部分是对的)
  • 哦,显示你的错误信息,不要只说“这都不起作用”。
  • @BenVoigt 请将其作为答案发布,以便他将其标记为已接受。

标签: arrays c++-cli managed


【解决方案1】:

二维数组不一定是您想要的,但您只需稍作改动即可使当前代码正常工作:

  • 摆脱循环内的new。您需要一个 Point 值,而不是指向某个值的指针。

MSDN 已经做好了这部分。

【讨论】:

    【解决方案2】:

    您现在得到的不是 100 点阵列,而是 10x10 二维阵列。试试gcnew array&lt;Point&gt;(100),你就可以把它传递给FillPolygon。

    【讨论】:

    • @Ben Voigt 的答案是正确的,谢谢。事实证明我根本不需要 FillPolygon。一个二维数组就可以了,数组中的每个点都有填充椭圆。我仍在学习如何在这个论坛中四处走动:我如何将他的答案标记为正确的?
    • 他没有发布答案,而是发表了评论。惯例似乎是要求他将其发布为答案,我刚刚这样做了。
    猜你喜欢
    • 2016-12-17
    • 2011-07-30
    • 2011-05-30
    • 1970-01-01
    • 2012-11-08
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多