# find centerline of the longest side of a rectangle python

I am trying to put together a simple code without any python libraries.

the aim is to find the centerline of a rectangle (for the longest side specifically).

This is my code so far. (it is looking for the center coordinates).

I want to be able to fine the centerline nonmatter the orientation of the rectangle. how do you recommend I do it. I’m open to any advice.

``````import matplotlib.pyplot as plt

x = [4,4,-4,-4]

y = [8,-5,-5,8]

x1,y1 = x,y
x2,y2 = x,y
x3,y3 = x,y
x4,y4 = x,y

ch_y1 = y1-y2
ch_y2 = y1-y4
ch_x1 = x1-x2
ch_x2 = x1-x4

l1 = ((x1-x2)**2+(y1-y2)**2)**0.5
l4 = ((x1-x4)**2+(y1-y4)**2)**0.5
l2 = ((x2-x3)**2+(y2-y3)**2)**0.5
l3 = ((x3-x4)**2+(y3-y4)**2)**0.5

if l2 > l1:
if abs(1-l1/l3) < 0.3 :
if x1>x2:
x11 = x1 - l2/2

if x1<x2:
x11 = x1 + l2/2

if x1 == x2:
x11 = x1

if y1>y2:
y11 = y1 - l2/2

if y1<y2:
y11 = y1 + l2/2

if y1 == y2:
y11 = y1

if x2>x3:
x12 = x2 - l3/2

if x2<x3:
x12 = x2 + l3/2

if x2 == x3:
x12 = x2

if y2>y3:
y12 = y2 - l3/2

if y2<y3:
y12 = y2 + l3/2

if y2 == y3:
y12 = y2

if l1 >l2:

x1 = x1
x2 = x3
x3 = x4
y1 = y1
y2 = y3
y3 = y4

if abs(1-l2/l4) < 0.3 :

if x1>x2:
x11 = x1 - l2/2

if x1<x2:
x11 = x1 + l2/2

if x1 == x2:
x11 = x1

if y1>y2:
y11 = y1 - l2/2

if y1<y2:
y11 = y1 + l2/2

if y1 == y2:
y11 = y1

if x2>x3:
x12 = x2 - l4/2

if x2<x3:
x12 = x2 + l4/2

if x2 == x3:
x12 = x2

if y2>y3:
y12 = y2 - l4/2

if y2<y3:
y12 = y2 + l4/2

if y2 == y3:
y12 = y2

plt.plot(x,y)

plt.plot([x11,x12],[y11,y12])
``````

### >Solution :

That is way too complicated. You can just draw the lines going through the center of the segments like so (note that I added a copy of `x` and `y` as `x` and `y` for simplicity):

``````import matplotlib.pyplot as plt

x = [4,4,-4,-4]

y = [8,-5,-5,8]

x = x + [x]
y = y + [y]
plt.plot(x,y)

for i in range(2):
plt.plot([(x[i]+x[i+1])/2, (x[i+2]+x[i+3])/2], [(y[i]+y[i+1])/2, (y[i+2]+y[i+3])/2])
`````` 