nick34992
4/30/2017 - 6:55 PM

Python: Class, Math: Calculate Hypotenuse

Python: Class, Math: Calculate Hypotenuse

import math

class Point:
    """Set the X/Y coordinates of a point."""

    def __init__(self, x=0, y=0):
        """Initialize location of new point based on X/Y coordinates.
        Otherwise, the point defaults to origin."""
        self.relocate(x, y)

    def relocate(self, x, y):
        """Relocate point on 2D plane. New X/Y values passed as parameters.
        `p1.relocate(7,0)`"""
        self.x = x
        self.y = y

    def reset(self):
        """Reset point to (0,0).
        `p1.reset()`"""
        self.relocate(0, 0)

    def calc_hypotenuse(self, other):
        """Calculate distance from 1st point to 2nd point.
        2nd point passed as parameter.

        Pythagorean Theorem applied to calculate diagonal distance
        between two points. Distance returned as a float value."""
        return math.sqrt(
            (self.x - other.x)**2 + (self.y - other.y)**2
        )