Lab assignment

A sample test file called lab4_test.py is provided to you. The file lab4.py accompanies this lab. This contains the code for the Point class. There is also starter code for a Segment class and a Triangle class. You should turn in the lab4.py file onto D2L.

The Segment class

A line segment can be defined by its endpoints. Therefore, one way to design a Segment class is
to define 2 instance variables, point1 and point2, both of which are Point objects. Write this
class to have the following methods:
· A constructor, which is passed two Point objects indicating the endpoints of the line
segment.
· __str__ and __repr__
length() takes 0 parameters and returns the length of the line segment, as a floating
point number. The length of a segment is √(x1 – x2)2 + (y1 + y2)2,where the
endpoings of the segment are (x1,y1) and (x2,y2).
· slope() takes 0 parameters and calculates the slope of the segment.
· midpoint() computes and returns the Point which is midway between the two
endpoints.

The Triangle class
A triangle can be represented in several different ways, but we will do so by storing 3 Point
objects as instance variables of a Triangle object. Each point represents one corner of the
triangle. Let’s call the points p1, p2, and p3. You must write the following methods:
·
A constructor, which is passed 3 points (p1, p2, and p3). These specify the 3 corners of the
triangle.
· __str__ and __repr__
· A method called is_triangle. This method should return True if p1, p2, and p3 define a
triangle, or False otherwise. Note that p1, p2, and p3 do not define a triangle if a straight
line can be drawn which connects the 3 points. For example:

A method called perimeter, which computes the perimeter of the triangle. For example:
>>> p1 = Point(1,1)
>>> p2 = Point(4,1)
>>> p3 = Point(4,5)
>>> t4 = Triangle(p1,p2,p3)
>>> t4.perimeter()
12.0
The perimeter of t3 is 12 because the distance between (1,1) and (4,1) is 3; the distance
between (4,1) and (4,5) is 4, and the distance between (4,5) and (1,1) is 5.
· A method called area, which computes the area of the triangle. There are several ways to
compute a triangle’s area, but we will use Heron’s formula: i. Define s to be ½ of the
triangle’s perimeter ii. The area of the triangle is

where a, b,and c are the lengths of the 3 sides of the triangle.

For example:
>>> t4.area()
6.0
For triangle t4, s = 6, (s-a) = 3, (s-b) is 2, and (s-c) is 1. Therefore, the area is √6 ∗ 3 ∗ 2 ∗ 1 =
6. Here is the output when I run lab4_test.py on my own sample solutions.
Is ((1,1),(2,3),(4,1)) a triangle?
True
Its perimeter is 8.06449510224598
Its area is 2.999999999999999
Is ((1.5,0),(2,3),(4,2)) a triangle?
True
Its perimeter is 8.479011361365323
Its area is 3.249999999999999
Is ((1,1),(2.0,1.5),(3,2)) a triangle?
False
Is ((1,1),(4,1),(4,5)) a triangle?
True
Its perimeter is 12.0
Its area is 6.0
Is ((1,0),(6,-4),(2,1)) a triangle?
True
Its perimeter is 14.220462037238793
Its area is 4.500000000000003

lab4test.py

from lab4complete import *

# Here is a test file.  You can run it using la4.py.

# it prints

##3 points

##

##(1,1)

##(6,1)

##(1,5)

##

##3 segments

##

##((1,1),(6,1))

##Segment(Point(1,1),Point(6,1))

##((1,1),(1,5))

##Segment(Point(1,1),Point(1,5))

##((6,1),(1,5))

##

##Segment lengths

##

##5.0

##4.0

##6.4031242374328485

##

##Segment slopes

##

##inf

##0.0

##-1.25

##

##Segment midpoints

##

##(3.5,1.0)

##(1.0,3.0)

##(3.5,3.0)

##

##Triangle(Point(1,1),Point(6,1),Point(1,5))

##((1,1),(6,1),(1,5))

##isTriangle True

##perimeter 15.403124237432849

##area 10.0

print(‘3 points\n’)

p1 = Point(1,1)

p2 = Point(6,1)

p3 = Point(1,5)

print(p1)

print(p2)

print(p3)

print(‘\n3 segments\n’)

seg12 = Segment(p1,p2)

seg13 = Segment(p1,p3)

seg23 = Segment(p2,p3)

print(seg12)

print(repr(seg12))

print(seg13)

print(repr(seg13))

print(seg23)

print(‘\nSegment lengths\n’)

print(seg12.length())

print(seg13.length())

print(seg23.length())

print(‘\nSegment slopes\n’)

print(seg12.slope())

print(seg13.slope())

print(seg23.slope())

print(‘\nSegment midpoints\n’)

print(seg12.midpoint())

print(seg13.midpoint())

print(seg23.midpoint())

t = Triangle(p1, p2, p3)

print(‘\n’ + repr(t))

print(str(t))

print(‘isTriangle ‘ + str(t.is_triangle()))

print(‘perimeter ‘ + str(t.perimeter()))

print(‘area ‘ + str(t.area()))

 lab4.py

#############################################

#

# Fill in the methods of the Segment and

# Triangle methods below.  In this code, you

# will probably wish to use the Point class.

#############################################

class Segment:

# make a Segment whose endpoints are the 2 points passed

# as parameters

def __init__(self, p1, p2):

pass

# however you would like to display a line segment as a string is fine

def __str__(self):

pass

# this must return ‘Segment(…)’ where inside the parenthese are

# the appropriate parameters that would make this Segment.

def __repr__(self):

pass

# The length of the Segment = srqrt((x1-x2)**2 + (y1-y2)**2)

def length(self):

pass

# slope of the line (x2-x1)/(y2-y1)

def slope(self):

pass

# midpoint of the line ((x2+x1)/2),(y2+y1)/2)

class Triangle:

# make a triangle whose corners are defined by the points p1, p2, and p3

def __init__(self, p1, p2, p3):

pass

# this method is complete.  You do not need to write or modify it.

def __str__(self):

# decide how you would like to display a Triangle as a string

pass

# this must reutrun ‘Traingle(…)’ where the items in

# the parentheses are the repr of the instance variable

# in the Triangle class.

def __repr__(self):

pass

defis_triangle(self):

return True         # change this

# return the perimeter of the triangle

def perimeter(self):

return 1            # change this

# return the area of the triangle.  Please see the lab write-up

# for the formula to compute the area.

def area(self):

return 1            # change this

# Here is the Point class from lecture.

# You will use it in the code you write

# below.

from math import sqrt

class Point:

# the parameter “self” refers to the Point object

# we will always give the name “self” to the

# first parameter of a method

def __init__(self, init_x, init_y):

self.x = init_x   # self.x is an instance variable

self.y = init_y   # self.y is an instance variable

# we can write the __str__ method however we want.

# It determines what the built-in Python str function

# returns for a Point object, such as

#

# >>> p = Point(2,3)

# >>>str(p)

# ‘(2,3)’

def __str__(self):

return ‘({},{})’.format(self.x, self.y)

# __repr__ determines what the built-in Python repr

# function returns for a Point object.  This must

# conform to the Python convention for what repr

# returns.

def __repr__(self):

return ‘Pointv2({},{})’.format(self.x, self.y)

defsetx(self, new_x):

self.x = new_x

defsety(self, new_y):

self.y = new_y

def get(self):

return (self.x, self.y) # returns a 2-tuple

defgetx(self):

returnself.x

defgety(self):

returnself.y

def move(self, dx, dy):

self.x += dx

self.y += dy

Solution:

lab4complete.py

#

# Fill in the methods of the Segment and

# Triangle methods below.  In this code, you

# will probably wish to use the Point class,

#############################################

from math import sqrt

class Segment:

# make a Segment whose endpoints are the 2 points passed

# as parameters

def __init__(self, p1, p2):

self.p1 = p1

self.p2 = p2

# however you would like to display a line segment as a string is fine

def __str__(self):

return ‘({} – {})’.format(self.p1, self.p2)

# this must return ‘Segment(…)’ where inside the parenthese are

# the appropriate parameters that would make this Segment.

def __repr__(self):

return ‘Segment({}, {})’.format(self.p1, self.p2)

# The length of the Segment = srqrt((x1-x2)**2 + (y1-y2)**2)

def length(self):

dx = (self.p1.getx() – self.p2.getx()) ** 2

dy = (self.p1.gety() – self.p2.gety()) ** 2

returnsqrt(dx + dy)

# slope of the line (x2-x1)/(y2-y1)

def slope(self):

dx = self.p2.getx() – self.p1.getx()

dy = self.p2.gety() – self.p1.gety()

if dx == 0:

return None

returndy / dx

# midpoint of the line ((x2+x1)/2),(y2+y1)/2)

def midpoint(self):

dx = self.p2.getx() + self.p1.getx()

dy = self.p2.gety() + self.p1.gety()

return Point(dx / 2, dy / 2)

class Triangle:

# make a triangle whose corners are defined by the points p1, p2, and p3

def __init__(self, p1, p2, p3):

self.p1 = p1

self.p2 = p2

self.p3 = p3

# this method is complete.  You do not need to write or modify it.

def __str__(self):

# decide how you would like to display a Triangle as a string

return ‘({}, {}, {})’.format(self.p1, self.p2, self.p3)

# this must reutrun ‘Traingle(…)’ where the items in

# the parentheses are the repr of the instance variable

# in the Triangle class.

def __repr__(self):

return ‘Triangle({}, {}, {})’.format(self.p1, self.p2, self.p3)

defis_triangle(self):

if self.p1.getx() == self.p2.getx() or self.p1.getx() == self.p3.getx():

return self.p2.getx() != self.p3.getx()

if self.p1.gety() == self.p2.gety() or self.p1.gety() == self.p3.gety():

return self.p2.gety() != self.p3.gety()

line1 = Segment(self.p1, self.p2)

line2 = Segment(self.p1, self.p3)

return line1.slope() != line2.slope() and line1.slope() != -line2.slope()

# return the perimeter of the triangle

def perimeter(self):

line1 = Segment(self.p1, self.p2)

line2 = Segment(self.p1, self.p3)

line3 = Segment(self.p2, self.p3)

a = line1.length()

b = line2.length()

c = line3.length()

return a + b + c

# return the area of the triangle.  Please see the lab write-up

# for the formula to compute the area.

def area(self):

line1 = Segment(self.p1, self.p2)

line2 = Segment(self.p1, self.p3)

line3 = Segment(self.p2, self.p3)

a = line1.length()

b = line2.length()

c = line3.length()

s = (a + b + c) / 2

returnsqrt(s * (s – a) * (s – b) * (s – c))

# Here is the Point class from lecture.

# You will use it in the code you write

# below.

from math import sqrt

class Point:

# the parameter “self” refers to the Point object

# we will always give the name “self” to the

# first parameter of a method

def __init__(self, init_x, init_y):

self.x = init_x   # self.x is an instance variable

self.y = init_y   # self.y is an instance variable

# we can write the __str__ method however we want.

# It determines what the built-in Python str function

# returns for a Point object, such as

#

# >>> p = Point(2,3)

# >>>str(p)

# ‘(2,3)’

def __str__(self):

return ‘({},{})’.format(self.x, self.y)

# __repr__ determines what the built-in Python repr

# function returns for a Point object.  This must

# conform to the Python convention for what repr

# returns.

def __repr__(self):

return ‘Pointv2({},{})’.format(self.x, self.y)

defsetx(self, new_x):

self.x = new_x

defsety(self, new_y):

self.y = new_y

def get(self):

return (self.x, self.y) # returns a 2-tuple

defgetx(self):

returnself.x

defgety(self):

returnself.y

def move(self, dx, dy):

self.x += dx

self.y += dy