我知道这更像是一个算术问题,而不是纯粹的编程问题,但它与我目前试图解决的问题有关。另外,我不知道有什么好地方可以问数学或 JES 相关的话题。
我遇到的问题是,对于 uni 任务,我们打算使用 JES 来绘制不同的国旗。标志可以是任何大小,由用户确定。旗帜的难度根据旗帜上的星星图案和数量进行评级。
无论如何,我正在尝试绘制智利国旗,我遇到的唯一问题是如何计算星星的比例。
这是我用于缩放星星的功能:
def scaleStar(picture, scale):
# the starting X position is set to 0
sourceX = 0
# Calculates the scaled width and height of the picture as an integer
scaleWidth = int(getWidth(picture) / scale)
scaleHeight = int(getHeight(picture) / scale)
# Creates an empty picture with the scaled dimensions just calculated
scaledPicture = makeEmptyPicture(scaleWidth, scaleHeight)
# Begins the loop that goes through the entire picture and copies the
# pixel from the source to the target pixel
for scaleX in range(0, scaleWidth):
sourceY = 0
for scaleY in range(0, scaleHeight):
sourcePixel = getPixel(picture, int(sourceX), int(sourceY))
scaledPixel = getPixel(scaledPicture, scaleX, scaleY)
colour = getColor(sourcePixel)
setColor(scaledPixel, colour)
sourceY = sourceY + scale
sourceX = sourceX + scale
return scaledPicture
我知道星星是整个旗帜的 1/12 大小。因此,对于比例参数,我尝试将值设为 12。这适用于 300 宽 x 200 高的标志图片。但不适用于其他尺寸。我尝试了整个标志的 (height*width)/12 但这只返回了一个值错误。在过去的 3 天里,我一直在使用这段代码,每次我认为我有答案时,星星的大小都相差甚远。
如果这是一个愚蠢的问题,我真的很抱歉,但如果有人对我将如何解决这个问题有任何建议,我将非常感激。如果您需要更多信息来帮助,请不要犹豫:)