Last active
July 16, 2021 14:53
-
-
Save minte9/ed6c6cac3defb70f06cf13ed6fcada76 to your computer and use it in GitHub Desktop.
Python / Class / Attributes - https://www.minte9.com/python/class-attributes-1286
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a function named distance_between_points() | |
# that takes 2 Point as argument | |
# and return the distance between them | |
import math | |
class Point: """ a 2D point """ | |
a = Point() | |
a.x = 0.0 | |
a.y = 0.0 | |
b = Point() | |
b.x = 3.0 | |
b.y = 4.0 | |
# SOLUTION | |
def distance_between_points(p1, p2): | |
dx = (p2.x - p1.x)**2 | |
dy = (p2.y - p1.y)**2 | |
return math.sqrt(dx + dy) | |
print(distance_between_points(a, b)) # 5.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment