Skip to main content

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());

Rust Constructor Parameters

id
String
required
Unique identifier for the rectangle instance

TypeScript Constructor Parameters

options
IRectangleOptions
Configuration options for the rectangle
options.ogid
string
Optional unique identifier (auto-generated if not provided)
options.center
Vector3
required
Center point of the rectangle
options.width
number
required
Width of the rectangle along the X-axis
options.breadth
number
required
Breadth of the rectangle along the Z-axis
options.color
number
required
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.

generate_geometry

rectangle.generate_geometry();
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();
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
);
Generates an offset rectangle at the specified distance. Positive distances create larger rectangles, negative distances create smaller ones.
distance
number
required
Distance to offset the rectangle (positive = outward, negative = inward)
acuteThresholdDegrees
number
default:"35.0"
Angle threshold for beveling acute corners
bevel
boolean
default:"true"
Enable beveling at acute angles

get_brep_serialized

let brep: String = rectangle.get_brep_serialized();
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

id
String
Unique identifier for the rectangle instance
center
Vector3
Center point of the rectangle (default: origin)
width
f64
Width along the X-axis (default: 1.0)
breadth
f64
Breadth along the Z-axis (default: 1.0)
brep
Brep
Internal BREP representation with 4 vertices, 4 edges, and 1 face
color
number
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();

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);
}

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

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
Last modified on March 14, 2026