【问题标题】:PlotBars procedure in Pascal for Bubble Sort algorithm用于冒泡排序算法的 Pascal 中的 PlotBars 过程
【发布时间】:2016-03-30 08:28:37
【问题描述】:

我目前正在使用 Pascal 编写一个程序,我想在该程序中实现冒泡排序算法并使用 Bars 直观地显示它。我已经成功编写了 BubbleSort 过程,但我被困在 PlotBars 过程(为 BubbleSort 过程绘制条形的过程)。现在,当我运行程序时,它会在右侧显示数字面板,然后单击“排序!”按钮它只显示一个栏,如果我一直按排序按钮,它会降低栏的高度。我附在我的代码 sn-p 和我的输出下方,并且我附在我希望我的输出看起来像的下方。任何帮助将不胜感激。谢谢

问候, 瓦利德

PltoBar 代码

procedure PlotBars(var data: array of Integer);
var
  i: Integer;
  yAxis: Integer;
  newWidth: Single;
  newHeight: Single;
  roundNewWidth: Integer;
  roundNewHeight: Integer;
begin
  yAxis := 600; //Screenheight is 600
  newWidth := ((ScreenWidth() - PanelWidth('NumberPanel')) / 25); // There are 25 index in array
  for i:= 0 to High(data) do
  begin
    newHeight := data[i] - ScreenHeight();
    roundNewWidth := Round(newWidth);
    roundNewHeight := Round(newHeight);
    ClearScreen();
    FillRectangle(ColorRed, i, yAxis, roundNewWidth, roundNewHeight);
  end;
end;  

What my Output looks like

What i want my Output to look like

【问题讨论】:

  • 代码应该作为文本插入问题,而不是图像。另外,您使用什么版本的 Pascal?
  • 您应该提供一个最小、完整和可验证的示例 (stackoverflow.com/help/mcve)。您的部分代码无法编译,因为没有包含许多变量和函数。

标签: pascal


【解决方案1】:

您在绘制每个条之前清除 for i 循环内的屏幕,因此只有最后一个条存在。在循环之前调用ClearScreen()(在循环之后调用DrawInterface()RefreshScreen())。

【讨论】:

  • 好的,我做到了,但屏幕上仍然只出现一个栏。我希望我的输出看起来像我上面附加的图片。不应该有更多的 FillRectangle 过程吗?
【解决方案2】:

您的问题是如何绘制矩形。 我假设您的程序 FillRectangle 调用 FillRect。 FillRect从左上角开始绘制,需要4个坐标]1

所以你的代码必须看起来像这样:

procedure PlotBars(var data: array of Integer);
var
  i: Integer;
  yAxis: Integer;
  newWidth: Single;
  newHeight: Single;
  roundNewWidth: Integer;
  roundNewHeight: Integer;
begin
  yAxis := 600; //Screenheight is 600
  newWidth := ((ScreenWidth() - PanelWidth('NumberPanel')) / High(data)); // changed from fixed value, since there could be more than 25!
  roundNewWidth := Round(newWidth);

  for i:= 0 to High(data) do
  begin
    newHeight := ScreenHeight() - data[i];
    roundNewHeight := Round(newHeight);
    FillRectangle(ColorRed, i*roundNewWidth, yAxis, i*roundNewWidth+roundNewWidth, roundnewHeight);
  end;
end;

【讨论】:

    猜你喜欢
    • 2016-03-19
    • 1970-01-01
    • 2018-08-11
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2013-11-02
    • 2013-05-17
    相关资源
    最近更新 更多