Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to calculate the center of a rectangle in Python

1 Answer

0 votes
class Point:
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y

class Rectangle:
    def __init__(self, top_left: Point, bottom_right: Point):
        self.top_left = top_left
        self.bottom_right = bottom_right

def get_center(rect: Rectangle) -> Point:
    center_x = (rect.top_left.x + rect.bottom_right.x) / 2.0
    center_y = (rect.top_left.y + rect.bottom_right.y) / 2.0
    return Point(center_x, center_y)


rect = Rectangle(Point(10.0, 20.0), Point(110.0, 70.0))
center = get_center(rect)

print(f"Center of the rectangle: ({center.x:.2f}, {center.y:.2f})")



'''
run:

Center of the rectangle: (60.00, 45.00)

'''

 



answered Jun 22, 2025 by avibootz

Related questions

...