为了账号安全,请及时绑定邮箱和手机立即绑定

如何编写在三种颜色之间进行选择以生成特定图像的条件语句

如何编写在三种颜色之间进行选择以生成特定图像的条件语句

慕虎7371278 2023-12-13 14:29:06
我正在为我在大学修读的计算机科学课程做一个实验室。问题指出,“让每个返回颜色的 int 表示形式。还让每个接受四个 int 参数:列索引、行索引、图像宽度和图像高度。最终,您将使用这些参数在颜色之间进行仲裁”。具体来说,对于我无法弄清楚的方法,问题是“在方法 boxColor 中,编写一个条件语句,在三种颜色之间进行选择以生成此图像”。这是图像:图像很明显,我的教授在这里期望的是分配的另一个问题的快速示例。问题指出,“在方法 stripesColor 中,编写一个条件语句,在三种颜色之间进行选择以生成此图像:”图片:图片这是我为完成任务而编写的代码: public static int stripesColor(int column, int row, int imWidth, int imHeight) {      if (column < (imHeight / 3)) {         return Color.red.getRGB();      } else if ((column < 2 * (imHeight / 3))) {         return Color.pink.getRGB();      } else {         return Color.orange.getRGB();    }  }我还应该提到,为了清楚起见,教授为我们提供了一个类,它将使用我们编写的方法来生成图像,我们只负责进行计算以获得他提供的图片。我尝试找到整个正方形 (512px x 512px) 的对角线,并将较小的正方形放在总大小的 1/5 内部,但我不确定我所做的是否是错误的,或者是否有另一个这样做的方法。如有任何帮助,我们将不胜感激,谢谢。
查看完整描述

2 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

我建议将其分解为较小的问题并单独处理。首先只关注一个案例,也许是最简单的一个,然后再解决其他案例。


就在我的脑海里,别让我太在意数学……


public static int stripesColor(int column, int row, int imWidth, int imHeight) {

    // Just worry about the square in the center for now.


    // If pixel is not in left/bottom or top/right quarters:

    if (imWidth / 4 < column < (imWidth * 3)/4) && 

    (imHeight / 4 < row < (imHeight * 3)/4) {


        return Color.hotpink.getRGB();


    } else if {

        // We know that any pixel in the center square is already taken care of, 

        // so the logic for the rest can completely ignore that.

        // It can be written as though the square isn't in the image at all.

    } else {

        // Last remaining color

    }

}

我也不知道内部正方形的暗淡是总大小的 1/2 还是 3/5;我在这里假设为 1/2,但最终这并不重要。希望这可以帮助您摆脱困境。


如果条件内的数学if看起来很糟糕,您始终可以为这些值初始化单独的变量,然后条件将更加清晰(并且基本上是自记录的)。


查看完整回答
反对 回复 2023-12-13
?
偶然的你

TA贡献1841条经验 获得超3个赞

这与其说是一个编码问题,不如说是一个如何构建组合逻辑的问题。

让我们拆开盒子。该盒子基本上由三部分组成 - 左上半部分的黄色三角形,右下半部分的青色三角形,以及覆盖在顶部的洋红色正方形。

好的。让我们看第一部分——我们如何定义图像的左上半部分?如果我们将方形图像像素视为图形,则分割黄色和青色的中心线就是从原点 (0,0) 到左上角 (imWidth, imHeight) 的线。这条线的斜率为 1,形式为 y=x。因此,左上角的像素是列 <= 行的任何位置。

因此,当column <= row时,我们将返回值设置为黄色整数值。

对于左下角,我们选择相反的比较,因此当列 > 行时,我们将返回值设置为青色整数值。

现在为了处理覆盖,我们想要找到像素位于该中心区域内的情况。假设我们希望图像占据中间的 80%,那么我们需要将缓冲区设置为总大小的 10%。所以我们检查的是是否 (imWidth * (0.1) < row ) && (row < imWidth * (1-0.1)) && (imHeight * (0.1) < column) && (column < imHeight * (1-0.1) ))

public static int boxColor(int column, int row, int imWidth, int imHeight) {

    final int UPPER_LEFT_COLOR = 0; // Set to upper-left color.

    final int LOWER_RIGHT_COLOR = 128; // Set to lower-right color.

    final int CENTER_SQUARE_COLOR = 255; // Set to center color.

    final double MARGIN_AMOUNT = 0.1; // Set to buffer percentage


    int return_color = 0; //Initialize the return value to something.

    // First set the return value based on the midline split.

    if (column <= row) {

        return_color = UPPER_LEFT_COLOR;

    } else {

        return_color = LOWER_RIGHT_COLOR;

    }

    // Test for the overlay and reset the return value.

    if ((imWidth * (MARGIN_AMOUNT) < row ) && (row < imWidth * (1-MARGIN_AMOUNT)) && (imHeight * (MARGIN_AMOUNT) < column) && (column < imHeight * (1-MARGIN_AMOUNT))) {

        return_color = CENTER_SQUARE_COLOR;

    }

    // Return the finally determined value.

    return return_color;

}


查看完整回答
反对 回复 2023-12-13
  • 2 回答
  • 0 关注
  • 50 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信