[Tutor] python question
Amir Fawzi
amirfaw at hotmail.com
Fri Oct 25 10:13:57 CEST 2013
Hi!
I am trying to do an exercise abt classes, but I find it really hard even after reading. I have tryed to start it at the bottom of this document. I hope someone can help me. I have to submit the question by sunday.
Exercise 7.4. Make a class for straight lines.
Make a class Line whose constructor takes two points p1 and p2 (2-
tuples or 2-lists) as input. The line goes through these two points (see
function line in Chapter 3.1.7 for the relevant formula of the line). A
value(x) method computes a value on the line at the point x. Here is
a demo in an interactive session:
>>> from Line import Line
>>> line = Line((0,-1), (2,4))
>>> print line.value(0.5), line.value(0), line.value(1)
0.25 -1.0 1.5
My answer:
class Line:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def value(self, x):
p1 = (y1 - y0)/float(x1 -x0) # Formula referred to in chapter 3.1.7 in the book
p2 = (y0 - a*x0) # return <value of the line at point x>
def line (x0, y0, x1, y1):
p1 = (x0 - x1)
p2 = (y0 - y1)
return p1, p2
Kindest Regards
A.F.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131025/c2e00cff/attachment.html>
More information about the Tutor
mailing list