Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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[0],y[0]
x2,y2 = x[1],y[1]
x3,y3 = x[2],y[2]
x4,y4 = x[3],y[3]

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[0] and y[0] as x[4] and y[4] for simplicity):

import matplotlib.pyplot as plt

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

y = [8,-5,-5,8]
            
x = x + [x[0]]
y = y + [y[0]]
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])

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading