Description

Test a shape to check if it’s a true rectangle.

Author and license

Script by MLAV.LAND, licensed under the GNU GPL 3 License.

Code

import rhinoscriptsyntax as rs
 
def is_rectangle():
    # Select a closed curve composed of 4 points (a quad)
    curve = rs.GetObject("Select a closed curve composed of 4 points", rs.filter.curve)
    
    if not curve:
        print("No valid curve selected.")
        return
 
    # Check if the curve is a closed curve composed of exactly 4 points
    if not rs.IsPolyline(curve) or len(rs.PolylineVertices(curve)) != 5:
        print("The selected curve is not a valid quad.")
        return
 
    # Get the four corner points of the quad
    points = rs.PolylineVertices(curve)
    
    # Calculate the diagonal lengths
    diagonal1 = rs.Distance(points[0], points[2])
    diagonal2 = rs.Distance(points[1], points[3])
    
    # Check if the diagonal lengths are approximately equal
    tolerance = 0.01  # Adjust the tolerance as needed
    if abs(diagonal1 - diagonal2) < tolerance:
        print("The selected shape is a true rectangle.")
    else:
        print("The selected shape is a random quad.")
 
if __name__ == "__main__":
    is_rectangle()