Homework:
Create a class Point.
Attributes: x, y
String representation: (x, y)
Methods:
translate(self, moveX, moveY): adds moveX to x and moveY to y
dist(self, p) where p is another Point: calculates distance between two points
Create two points. Find the distance. Translate one point 4 units left and 3 units up. Find the new distance.
Recourses:
Class slides: https://docs.google.com/presentation/d/1yG4Tdux4eBxCvBTNu840gISgyg2C-nmjkbZOk4PKczc/edit?usp=sharing
Class code: https://replit.com/join/biwwaovqdx-shrav816
Contact Info:
shravyassathish@gmail.com (mentor)
kingericwu@gmail.com (TA)
felixguo41@gmail.com (TA)
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def ToString(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"
def translate(self, movex, movey):
self.x = self.x + movex
self.y = self.y + movey
def dist(self, p):
return math.sqrt((p.x - self.x)**2 + (p.y - self.y)**2)
p1 = Point(3, 5)
p1.translate(4, 10)
p2 = Point(2,3)
dist = p1.dist(p2)
print(f"dist from {p1.ToString()} to {p2.ToString()} is {dist}")
import math;
class Point:
def __init__(self, x, y):
self.x = x;
self.y = y;
def toString(self):
return "("+ str(self.x) + "," + str(self.y) + ")";
def translate(self, moveX, moveY):
self.x = self.x + moveX;
self.y = self.y + moveY;
def dist(self, p):
a = self.x - p.x;
b = self.y - p.y;
c = math.sqrt(pow(a,2) + pow(b,2))
return c;
p1 = Point(2,3);
p2 = Point(5,7)
d1 = p1.dist(p2)
print(d1)
d2 = p2.dist(p1)
print(d2)
p2.translate(-4, 3)
d3 = p1.dist(p2)
print(d3)