# CSC240, Homework 2, Fall 2015
# TODO 0: your name here
import math
import raster
import sys
my_blue = (64, 153, 255)
class Point:
# the Point class has member variables:
# img: the image the point belongs to
# x,y: the x and y coordinates of the point
# color: a 3-tuple representing the color of the point
def __init__(self, img, x, y, color):
self.img = img
self.x = x
self.y = y
self.color = color
# draw the point on your image
def draw(self):
self.img.setPixel(self.x, self.y, self.color)
def __str__(self):
return str(self.x) + ' ' + str(self.y)
class Line:
# the Line class has member variables:
# img: the image the point belongs to
# p1, p2: the two end-points of the line, of class Point
# color: a 3-tuple representing the color of the line
def __init__(self, img, p1, p2, color):
self.img = img
self.p1 = p1;
self.p2 = p2;
self.color = color
self.points = [] # a list of all the Points on the line
# round in case we were given non-integer inputs
x1 = round(self.p1.x)
y1 = round(self.p1.y)
x2 = round(self.p2.x)
y2 = round(self.p2.y)
# vertical line
if x1 == x2:
for y in range(min(y1,y2),max(y1,y2)+1):
self.points.append(Point(self.img, x1, y, self.color))
return
# non-vertial line, define the slope
m = float(y2-y1)/(x2-x1)
# if the y-values are changing more slowly than x, loop over x
if abs(m) <= 1:
# start at (x1,y1) if x1 is smaller than x2, and (x2,y2) otherwise
if x1 <= x2:
y = y1
else:
y = y2
for x in range(min(x1,x2),max(x1,x2)+1):
self.points.append(Point(self.img, x, round(y), self.color))
y += m
# if the x-values are changing more slowly than y, loop over y
else:
# start at (x1,y1) if y1 is smaller than y2, and (x2,y2) otherwise
if y1 <= y2:
x = x1
else:
x = x2
for y in range(min(y1,y2),max(y1,y2)+1):
self.points.append(Point(self.img, round(x), y, self.color))
x += 1/m
# now our draw method is very simple!
def draw(self):
for p in self.points:
p.draw()
class RegularPolygon:
# n is the number of sides
# c is the center point of the polygon, i.e. c = [x,y]
# r is the "radius" of the polygon, so if the polygon were
# circumscribed by a circle, that circle would have radius r
# color: a 3-tuple representing the color of the polygon
def __init__(self, img, n, c, r, color):
self.img = img
self.n = n
self.c = c
self.r = r
self.color = color
self.points = []
# TODO 1: complete the constructor to create a list of points
# on the polygon. Hint: use the Line class
# after assembling the points, sort them by y-coordinate
self.points = sorted(self.points, key=lambda p: p.y)
# now the draw method is the same as for Line
def draw(self):
for p in self.points:
p.draw()
def fill(self):
# TODO 2: implement the fill method, using the sweep method
# from class. To get started: how can you use self.points to
# get the min and max y values?
# Hint: again use the Line class as you are sweeping through
# the y-values
pass
# this is where we can draw our lines to make an image
def make_image(filename, width, height):
img = raster.Raster(width, height)
# example polygon
poly = RegularPolygon(img, 6, [200, 200], 75, my_blue)
poly.draw()
poly.fill()
img.display()
img.save_PPM(filename, "TODO 3: comment")
if __name__ == '__main__':
if len(sys.argv) != 4:
print('\n\tcommandline should be of the form:\n\t\tpython hw1.py filename width height\n\twhere filename is hw2_your_name (.ppm will be added), and \n\twidth and height are positive integers representing the size of the image\n')
else:
make_image(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))