Overview
The Rectangle primitive represents a closed rectangular path in 3D space. It is defined by a center point, width (along the X-axis), and breadth (along the Z-axis). The rectangle lies in the XZ plane with Y as the normal direction.
Use Cases:
Creating rectangular shapes and boundaries
Base profiles for extrusion
Floor plans and section cuts
Offset operations for creating concentric rectangles
Constructor
let rectangle = OGRectangle :: new ( "rect-1" . to_string ());
const rectangle = new Rectangle ( options ?: IRectangleOptions );
Rust Constructor Parameters
Unique identifier for the rectangle instance
TypeScript Constructor Parameters
Configuration options for the rectangle
Optional unique identifier (auto-generated if not provided)
Center point of the rectangle
Width of the rectangle along the X-axis
Breadth of the rectangle along the Z-axis
Rectangle color as a hexadecimal number (e.g., 0x00ff00 for green)
Methods
set_config
rectangle . set_config (
center : Vector3 ,
width : f64 ,
breadth : f64
);
Updates the rectangle’s geometric parameters. rectangle . setConfig ( options : IRectangleOptions );
Updates the rectangle configuration and regenerates geometry.
generate_geometry
rectangle . generate_geometry ();
// Called automatically by setConfig
Generates four vertices forming the rectangle and creates edges connecting them. The geometry includes a face for the closed rectangle.
get_geometry_serialized
let geometry : String = rectangle . get_geometry_serialized ();
// Used internally for rendering
Returns serialized vertex buffer data including the closing vertex (first vertex repeated).
get_offset_serialized
let offset_data = rectangle . get_offset_serialized (
distance : f64 ,
acute_threshold_degrees : f64 ,
bevel : bool
);
const result = rectangle . getOffset (
distance : number ,
acuteThresholdDegrees : number = 35.0 ,
bevel : boolean = true
): IRectangleOffsetResult ;
Generates an offset rectangle at the specified distance. Positive distances create larger rectangles, negative distances create smaller ones.
Distance to offset the rectangle (positive = outward, negative = inward)
Angle threshold for beveling acute corners
Enable beveling at acute angles
get_brep_serialized
let brep : String = rectangle . get_brep_serialized ();
const brep = rectangle . getBrep ();
Returns the complete BREP representation including vertices, edges, and face.
getConfig
const config = rectangle . getConfig ();
Returns the current rectangle configuration.
discardGeometry
rectangle . discardGeometry ();
Disposes of the current geometry.
Properties
Unique identifier for the rectangle instance
Center point of the rectangle (default: origin)
Width along the X-axis (default: 1.0)
Breadth along the Z-axis (default: 1.0)
Internal BREP representation with 4 vertices, 4 edges, and 1 face
Rectangle color (TypeScript only, settable)
Code Examples
Creating a Basic Rectangle
use opengeometry :: primitives :: OGRectangle ;
use openmaths :: Vector3 ;
let mut rect = OGRectangle :: new ( "rect-1" . to_string ());
rect . set_config (
Vector3 :: new ( 0.0 , 0.0 , 0.0 ), // center
10.0 , // width
5.0 // breadth
);
rect . generate_geometry ();
import { Rectangle , Vector3 } from "opengeometry" ;
const rect = new Rectangle ({
center: new Vector3 ( 0 , 0 , 0 ),
width: 10 ,
breadth: 5 ,
color: 0x00ff00
});
Creating a Square
const square = new Rectangle ({
center: new Vector3 ( 0 , 0 , 0 ),
width: 8 ,
breadth: 8 , // Equal to width
color: 0xff0000
});
Generating Offset Rectangles
// Generate an outward offset rectangle
let offset_data = rect . get_offset_serialized ( 2.0 , 35.0 , true );
let result : OffsetResult = serde_json :: from_str ( & offset_data ) . unwrap ();
// The result contains the offset rectangle's points
for point in result . points {
println! ( "Point: ({}, {}, {})" , point . x, point . y, point . z);
}
// Outward offset (larger rectangle)
const outerRect = rect . getOffset ( 2.0 , 35.0 , true );
// Inward offset (smaller rectangle)
const innerRect = rect . getOffset ( - 1.5 , 35.0 , true );
console . log ( outerRect . points ); // Vector3[]
console . log ( outerRect . beveledVertexIndices ); // number[]
console . log ( outerRect . isClosed ); // true for rectangles
Creating Concentric Rectangles
const baseRect = new Rectangle ({
center: new Vector3 ( 0 , 0 , 0 ),
width: 20 ,
breadth: 15 ,
color: 0x0000ff
});
// Create multiple offset rectangles
const offsets = [ 2 , 4 , 6 , 8 ];
const concentricRects = offsets . map ( distance =>
baseRect . getOffset ( - distance )
);
Updating Rectangle Dimensions
rect . setConfig ({
center: new Vector3 ( 5 , 0 , 5 ),
width: 15 ,
breadth: 10 ,
color: 0xffff00
});
// Change color
rect . color = 0xff00ff ;
Getting Rectangle Vertices
// Get the four corner points
let points = rect . get_raw_points ();
// Points are ordered: bottom-left, bottom-right, top-right, top-left
const brep = rect . getBrep ();
const vertices = brep . vertices ;
// vertices contains the 4 corner points
Live Demo
Rectangle Demo Try the Rectangle primitive in the browser
See Also
Polygon Complex 2D shapes with holes
Polyline Connected line segments
Line Straight line segments