Jython picture scaling algorithm

1.9k Views Asked by At

I understand this is more of an arithmetic question than purely a programming question but it relates to a problem I'm trying to solve at the moment. Also, I don't know any good places to ask math or JES related topics.

The problem I am having is that, for a uni assignment, we are meant to use JES to draw different national flags. The flags can be any size, which is determined by the user. The flags are rated by difficulty by the patterns and numbers of stars on the flag.

Anyway, I'm trying to draw the Chilean flag and the only problem I've run into is how to calculate what the scale of the star should be.

Here is the function I have for scaling the star:

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

I know that the star is 1/12 the size of the entire flag. So for the scale parameter I tried having the value 12. This worked for a flag picture with 300 width by 200 height. But not for other sizes. I tried (height*width)/12 of the entire flag but this only returned a value error. I've been playing with this code for the past 3 days and every time I think I have the answer, the size of the star is way off.

I'm really sorry if this a dumb question but if anyone has any advice on how I would go about solving this problem, I'd be extremely grateful. If you need more info to help, don't hesitate to ask :)

0

There are 0 best solutions below