Rectangle origin on center

Hello.

This is easy. I didn’t post code because it is simple, ‘how to draw a rectangle about the center of a point rather than from the corner’.

Thank you in advance.

@brobes05,

below is an example function which you can feed with a valid plane while providing the width and height of the rectangle. The center is the origin of the plane.

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
    
def DoSomething():
    plane = rs.WorldXYPlane()
    width, height = 20.0, 10.0
    
    polyline = AddRectangleFromCenter(plane, width, height)
    if polyline:
        curve = polyline.ToNurbsCurve()
        scriptcontext.doc.Objects.AddCurve(curve)
        scriptcontext.doc.Views.Redraw()
    
def AddRectangleFromCenter(plane, width, height):
    p0 = plane.PointAt( width * 0.5,  height * 0.5 )
    p1 = plane.PointAt(-width * 0.5,  height * 0.5 )
    p2 = plane.PointAt(-width * 0.5, -height * 0.5 )
    p3 = plane.PointAt( width * 0.5, -height * 0.5 )
    
    polyline = Rhino.Geometry.Polyline([p0, p1, p2, p3, p0])
    if isinstance(polyline, Rhino.Geometry.Polyline):
        if polyline.IsValid: 
            return polyline
    
DoSomething()

c.

Thank you @Clement .

This works and is helpful and could be applied to a grid; however, I wonder if there is a simpler method without drawing polylines. In GH, I simply ‘construct domain’ and divide the x and y inputs by ‘x/2’ and ‘-x/2’ respectively.

I have been trying to add this line of thinking to python, but it might not work.

Thanks!

@brobes05, yes indeed, you can simplify the function further and draw a Rectangle3d instead of a polyline using just two points divided by 2:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
    
def DoSomething():
    plane = rs.WorldXYPlane()
    width, height = 20.0, 10.0
    
    rc = AddRectangleFromCenter(plane, width, height)
    if rc:
        curve = rc.ToNurbsCurve()
        scriptcontext.doc.Objects.AddCurve(curve)
        scriptcontext.doc.Views.Redraw()
    
def AddRectangleFromCenter(plane, width, height):
    a = plane.PointAt(-width * 0.5, -height * 0.5 )
    b = plane.PointAt( width * 0.5,  height * 0.5 )
    rectangle = Rhino.Geometry.Rectangle3d(plane, a, b)
    if isinstance(rectangle, Rhino.Geometry.Rectangle3d):
        if rectangle.IsValid: 
            return rectangle
    
DoSomething()

The reason why i prefer the first function is the rectangle.IsValid part. It seems that a Rectangle3d is valid, even if the width or height is zero.

c.