Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute the centroid of a rectangle in python

I want to compute the centroid of rectangle, The coordinates of rectangle are as follows:

co_ord = (601, 1006,604, 1009)  ## (xmin,ymin,xmax,ymax)

Can someone point to an easy way. Thanks

like image 488
Sami Avatar asked Dec 01 '22 12:12

Sami


2 Answers

The centroid of a rectangle with opposite corners (x1, y1) and (x2, y2) lies at the center of that rectangle ((x1+x2)/2, (y1+y2)/2)

like image 196
Patrick Haugh Avatar answered Dec 10 '22 10:12

Patrick Haugh


First, I am assuming that by saying centroid, you mean center. Next, I assume that the coord tuple is in the format: (x, y, width, height). In that case, it would be done this way:

coord = (601, 1006, 604, 1009)
centerCoord = (coord[0]+(coord[2]/2), coord[1]+(coord[3]/2))

where centerCoord would be the coordinates of the center in the format (x, y).

like image 35
Leonid Avatar answered Dec 10 '22 11:12

Leonid