BLACK = (0,0,0)
WHITE = (255,255,255)
#
# By given point and square size returns True, if pixel should be black
# or false, if should be white.
#
# Remember, that first square in left bottom corner is black, therefore
# we need to compute it from the start.
#
def isBlackSquare(x, y, squareSize):
resultX = x / squareSize
resultY = y / squareSize
return (resultX + resultY) % 2 == 0
#
# Returns distance between two points
#
def getDistance(center, point):
diffX = center[0] - point[0]
diffY = center[1] - point[1]
return int(math.sqrt(diffX**2 + diffY**2))
#
# Draws first picture in homework of problem C
#
def drawEffect1(size, squareCount):
im = Image.new("RGB", (size, size), "white")
radius = size / 4
squareSize = size / squareCount
i = 0
while i < size:
j = 0
while j < size:
isBlack = isBlackSquare(i, j, squareSize)
# Getting distance between point and center
distance = getDistance((size / 2, size / 2), (i,j))
level = distance / radius
if level % 2 == 1:
if isBlack == True:
isBlack = False
else:
isBlack = True
if isBlack:
im.putpixel((i, size - j - 1), BLACK)
j += 1
i += 1
im.save("effect1.bmp")
#
# If is pixel inside of square in picture, returns True, else returns false.
#
def isInsideSquare(point, squareSize):
halfSquareSize = squareSize / 2
startSquareValue = halfSquareSize
endSquareValue = halfSquareSize * 3
return startSquareValue <= point[0] and startSquareValue <= point[1] and endSquareValue >= point[0] and endSquareValue >= point[1]
#
# Draws second picture in homework of problem C
#
def drawEffect2(size, changeColorCount, squareCount):
im = Image.new("RGB", (size, size), "white")
level = size / changeColorCount
step = 255 / level
# Two because we have square on two sides
squareSize = size / squareCount
i = 0
while i < size:
j = 0
while j < size:
# Getting distance between point and center
distance = getDistance((size / 2, size / 2), (i,j))
levelIndex = distance / level
stepIndex = (distance % level) * step
isInside = isInsideSquare((i,j), squareSize)
if isInside == False:
levelIndex += 2
# If is levelIndex zero, then it has black color
color = BLACK
# If it has value 1, it is going from dark to white
if levelIndex % 4 == 1:
color = (stepIndex, stepIndex, stepIndex)
# If is two, it is white
if levelIndex % 4 == 2:
color = WHITE
# And in the end it is going from white to black
if levelIndex % 4 == 3:
stepIndex = 255 - stepIndex
color = (stepIndex, stepIndex, stepIndex)
im.putpixel((i,j), color)
j += 1
i += 1
im.save("effect2.bmp")
def isUnderSin(point, halfSize):
x = point[0]
y = point[1]
value = math.sin(math.radians(x)) * halfSize + halfSize
return value < y
def drawEffect3(size, squareCount):
im = Image.new("RGB", (size, size), "white")
squareSize = size / squareCount
halfSize = float(size) / 2
i = 0
while i < size:
j = 0
while j < size:
isBlack = isBlackSquare(i, j, squareSize)
isUnder = isUnderSin((i, j), halfSize)
if not isUnder:
isBlack = not isBlack
if isBlack:
im.putpixel((i, size - j - 1), BLACK)
j += 1
i += 1
im.save("effect3.bmp")
from myPixelLib import *
from PIL import Image
import math
drawEffect1(500, 10)
drawEffect2(500, 30, 2)
drawEffect3(500, 10)