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

我正在为我在大学修读的计算机科学课程做一个实验室。问题指出,“让每个返回颜色的 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 内部,但我不确定我所做的是否是错误的,或者是否有另一个这样做的方法。


如有任何帮助,我们将不胜感激,谢谢。


慕虎7371278
浏览 55回答 2
2回答

DIEA

我建议将其分解为较小的问题并单独处理。首先只关注一个案例,也许是最简单的一个,然后再解决其他案例。就在我的脑海里,别让我太在意数学……public static int stripesColor(int column, int row, int imWidth, int imHeight) {&nbsp; &nbsp; // Just worry about the square in the center for now.&nbsp; &nbsp; // If pixel is not in left/bottom or top/right quarters:&nbsp; &nbsp; if (imWidth / 4 < column < (imWidth * 3)/4) &&&nbsp;&nbsp; &nbsp; (imHeight / 4 < row < (imHeight * 3)/4) {&nbsp; &nbsp; &nbsp; &nbsp; return Color.hotpink.getRGB();&nbsp; &nbsp; } else if {&nbsp; &nbsp; &nbsp; &nbsp; // We know that any pixel in the center square is already taken care of,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // so the logic for the rest can completely ignore that.&nbsp; &nbsp; &nbsp; &nbsp; // It can be written as though the square isn't in the image at all.&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // Last remaining color&nbsp; &nbsp; }}我也不知道内部正方形的暗淡是总大小的 1/2 还是 3/5;我在这里假设为 1/2,但最终这并不重要。希望这可以帮助您摆脱困境。如果条件内的数学if看起来很糟糕,您始终可以为这些值初始化单独的变量,然后条件将更加清晰(并且基本上是自记录的)。

偶然的你

这与其说是一个编码问题,不如说是一个如何构建组合逻辑的问题。让我们拆开盒子。该盒子基本上由三部分组成 - 左上半部分的黄色三角形,右下半部分的青色三角形,以及覆盖在顶部的洋红色正方形。好的。让我们看第一部分——我们如何定义图像的左上半部分?如果我们将方形图像像素视为图形,则分割黄色和青色的中心线就是从原点 (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) {&nbsp; &nbsp; final int UPPER_LEFT_COLOR = 0; // Set to upper-left color.&nbsp; &nbsp; final int LOWER_RIGHT_COLOR = 128; // Set to lower-right color.&nbsp; &nbsp; final int CENTER_SQUARE_COLOR = 255; // Set to center color.&nbsp; &nbsp; final double MARGIN_AMOUNT = 0.1; // Set to buffer percentage&nbsp; &nbsp; int return_color = 0; //Initialize the return value to something.&nbsp; &nbsp; // First set the return value based on the midline split.&nbsp; &nbsp; if (column <= row) {&nbsp; &nbsp; &nbsp; &nbsp; return_color = UPPER_LEFT_COLOR;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return_color = LOWER_RIGHT_COLOR;&nbsp; &nbsp; }&nbsp; &nbsp; // Test for the overlay and reset the return value.&nbsp; &nbsp; if ((imWidth * (MARGIN_AMOUNT) < row ) && (row < imWidth * (1-MARGIN_AMOUNT)) && (imHeight * (MARGIN_AMOUNT) < column) && (column < imHeight * (1-MARGIN_AMOUNT))) {&nbsp; &nbsp; &nbsp; &nbsp; return_color = CENTER_SQUARE_COLOR;&nbsp; &nbsp; }&nbsp; &nbsp; // Return the finally determined value.&nbsp; &nbsp; return return_color;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java