将随机数转换为 XY 坐标以进行绘图

发布于 2024-08-18 08:58:27 字数 704 浏览 6 评论 0原文

我的教授给我们布置了一项作业,要求我们使用线性和搜索来测试运行时间和搜索大小的差异。二进制算法,数据将被绘制成图表。

我有搜索方法将运行时 &数组大小作为 ArrayList 中的点,然后发送到 GraphResults 类进行绘图。我需要先将这些数据点转换为 xy 坐标。搜索大小是 x 轴,运行时间是 y 轴

由于搜索大小固定为 128 的倍数,并且只有 8 个大小,我使用 switch 来计算 x 值,但我正在寻找一种更有效的方法将运行时间转换为坐标。

现在,我正在使用 5 个嵌套条件,如下所示:

if (y<=1000) {
    if (y<= 500) { 
        if (y<= 250) {
            newy= yaxis-32; }//equals to 250ms category
        else {
            newy= yaxis-(32*2); }//500ms category
   } 
else if (y<=750) {
    newy= yaxis-(32*3);} //750ms category
else {
    newy= yaxis-(32*4);} //1000ms category
} //end of the 1000ms tests

现在,超过 5000 毫秒的数字需要 7 次测试。有没有更有效的方法根据数字大小分配数字?

My professor gave us an assignment to test the difference in runtimes and search sizes using linear & binary algorithms, and the data is to be graphed.

I have the search methods put the runtime & array sizes as Points in an ArrayList, which is then sent to the GraphResults class for plotting. I need to convert those data points into xy coordinates before. The search size is the x-axis and the runtime is the y axis

As the search sizes are fixed as a multiple of 128 and there are only 8 sizes, I used switch for calculating the x value, but am looking for a more efficient way to convert the runtimes into coordinates.

Right now, I'm using nested conditionals with 5 like this:

if (y<=1000) {
    if (y<= 500) { 
        if (y<= 250) {
            newy= yaxis-32; }//equals to 250ms category
        else {
            newy= yaxis-(32*2); }//500ms category
   } 
else if (y<=750) {
    newy= yaxis-(32*3);} //750ms category
else {
    newy= yaxis-(32*4);} //1000ms category
} //end of the 1000ms tests

Right now, the numbers that are over 5000ms require 7 tests. Is there a more efficient way to assign a number based on a number size?

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

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

发布评论

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

评论(3

天赋异禀 2024-08-25 08:58:27

当您尝试确定测量范围时,可以将金额除以范围大小,然后计算要在图表中显示的数字。

顺便说一句,在您的代码中,您犯了一个逻辑错误,如果值为 y <= 1000,则第一个条件计算为 true,第二个条件为 y <= 750 > 永远不会被评估。

而且,值范围越高,图表点就越低。是这样的吗? (1000 -> ymax - 128 while 1 -> ymax - 32)

另外,如果您想将值与不均匀的范围进行比较,您还可以执行类似数组查找的操作(伪代码):

int[] ranges = new int { 50, 500, 5000, 50000 };

for (int n = 0; n < ranges.length && value > ranges[n]; n++) {
}

int range = n;
int newy = yaxis - range * 32;

请注意,输出 -范围索引充当大于数组中最大值的值的范围。

As you are trying to determine the range of your measurement, you can divide the amount by the range size, followed by calculating the number you want to show in the graph.

Btw, in your code, you made a logic error, if the value is y <= 1000 the first condition evaluates to true, and the second for y <= 750 will never be evaluated.

Also it seems that the higher the value range, the lower your graph point. Is that as intended? (1000 -> ymax - 128 while 1 -> ymax - 32)

As an aside, if you want to compare values to uneven ranges, you can also do something like an array lookup (pseudo code):

int[] ranges = new int { 50, 500, 5000, 50000 };

for (int n = 0; n < ranges.length && value > ranges[n]; n++) {
}

int range = n;
int newy = yaxis - range * 32;

Note that the out-of-range index acts as the range found for a value that is bigger than the biggest value in your array.

柠栀 2024-08-25 08:58:27

newy = yaxis - 32 * ((y/250)% 8); 怎么样?

How about newy = yaxis - 32 * ((y/250)% 8);?

粉红×色少女 2024-08-25 08:58:27

我会将您的代码重新格式化为更像这样的内容:

newy = yaxis - 32 * ((y-1)/250 + 1);

这样,您将计算乘数而不是手动选择它。

I would reformat your code to something more like this:

newy = yaxis - 32 * ((y-1)/250 + 1);

This way, you're calculating the multiplier rather than choosing it manually.

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