i am trying to calculate cartesian distance between two points using classes

import random
from math import sqrt

class Point:
    def __init__(self, name='anonym_point',x=0,y=0,z=0):
        self.name = name
        self.x = x
        self.y = y
        self.z = z

    def coord(self):
        return (self.x, self.y, self.z)

    def __repr__(self):
        return ("{} has coordinate {}  {}  {}".format(self.name, self.x, self.y, self.z))

def makepoint(namepoint):
    return Point(namepoint, random.uniform(0,100), random.uniform(0,100), random.uniform(0,100))

def distance_between_points(p1,p2):
    dist = sqrt((p2.x-p1.x)**2 + (p2.y-p1.y)**2 + (p2.z-p1.z)**2)
    print("distance between point ",p1.name," and the point ",p2.name,"  : ",dist)

point1 = makepoint("p1")
point2 = makepoint("p2")


Distance = distance_between_points(point1,point2)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top