# Program to compute the peremiter of a triangle given 3 defining points
# Matt Bishop, Apr. 27, 2009
# for ECS 10 Spring 2009
import math;
# this routine computes the Euclidean distance between two points
# parameters: x1, y1: x and y co-ordinates of the first point
# x2, y2: x and y co-ordinates of the second point
# returns: distance between (x1, y1) and (x2, y2) on 2-D plane
def dist(x1, y1, x2, y2):
return math.sqrt((x2-x1)**2 + (y2-y1)**2);
# this routine computes the perimeter of the triangle given 3 defining points
# parameters: x1, y1: x and y co-ordinates of the first point
# x2, y2: x and y co-ordinates of the second point
# x3, y3: x and y co-ordinates of the third point
# calls: dist
# returns: perimeter of triangle
def peri(x1, y1, x2, y2, x3, y3):
# compute the lengths of the three edges
d1 = dist(x1, y1, x2, y2);
d2 = dist(x2, y2, x3, y3);
d3 = dist(x3, y3, x1, y1);
# add them to get the perimeter
return d1 + d2 + d3;
# this routine generates a prompt to ask for a point
# parameters: what: which point (first, second, third) do you want?
# returns: the prompt as a string
def prompt(what):
# generate the prompt and return it
s = "Enter the " + what + " point's co-ordinates separated by a comma: ";
return s;
# now put it all together
# calls: prompt: generates user prompt
# peri: to compute perimeter
def main():
x1, y1 = input(prompt("first"));
x2, y2 = input(prompt("second"));
x3, y3 = input(prompt("third"));
print "The perimeter of the triangle with these points is ",
print peri(x1, y1, x2, y2, x3, y3);
main();