rectangle - Create rectangle with sharp or curved corners - MATLAB (original) (raw)

Create rectangle with sharp or curved corners

Syntax

Description

rectangle('Position',[pos](#f66-515573-pos)) creates a rectangle in 2-D coordinates. Specify pos as a four-element vector of the form [x y w h] in data units. The x and y elements determine the location and the w and h elements determine the size. The function plots into the current axes without clearing existing content from the axes.

example

rectangle('Position',[pos](#f66-515573-pos),'Curvature',[cur](#f66-515573-cur)) adds curvature to the sides of the rectangle. For different curvatures along the horizontal and vertical sides, specify cur as a two-element vector of the form [horizontal vertical]. For the same length of curvature along all sides, specify cur as a scalar value. Specify values between 0 (no curvature) and 1 (maximum curvature). Use [1 1] to create an ellipse or circle.

example

rectangle(___,[Name,Value](#namevaluepairarguments)) specifies rectangle properties using one or more name-value pair arguments. For example, 'FaceColor','red' specifies a red fill color. You can specify rectangle properties with any of the input argument combinations in the previous syntaxes.

example

rectangle([ax](#f66-515573-ax),___) creates the rectangle in the axes specified by ax instead of in the current axes (gca). The option ax can precede any of the input argument combinations in the previous syntaxes.

`r` = rectangle(___) returns the rectangle object. Use r to modify properties of the rectangle after it is created. For a list of properties, see Rectangle Properties. You can return an output argument with any of the previous syntaxes.

example

Examples

collapse all

Draw a rectangle that has a lower left corner at the point (1,2). Make the width of the rectangle 5 units and the height 6 units. Then, change the axis limits.

rectangle('Position',[1 2 5 6]) axis([0 10 0 10])

Figure contains an axes object. The axes object contains an object of type rectangle.

Draw a rectangle that has a lower left corner at (0,0) and an upper right corner at (2,4). Create the rectangle with curved corners by specifying the curvature as the scalar value 0.2. For data units of equal length along both the _x_-axis and _y_-axis, use axis equal.

figure rectangle('Position',[0 0 2 4],'Curvature',0.2) axis equal

Figure contains an axes object. The axes object contains an object of type rectangle.

Add a second rectangle that has the shortest side completely curved by specifying the curvature as the scalar value 1.

rectangle('Position',[3 0 2 4],'Curvature',1)

Figure contains an axes object. The axes object contains 2 objects of type rectangle.

Add a third rectangle and use different curvatures along the horizontal and vertical sides. Use a horizontal curvature of 0.5 to make 50 percent of each horizontal side curved. Use a vertical curvature of 1 to make both vertical sides completely curved.

rectangle('Position',[6 0 2 4],'Curvature',[0.5,1])

Figure contains an axes object. The axes object contains 3 objects of type rectangle.

Draw a circle by setting the Curvature property to [1 1]. Draw the circle so that it fills the rectangular area between the points (2,4) and (4,6). The Position property defines the smallest rectangle that contains the circle.

pos = [2 4 2 2]; rectangle('Position',pos,'Curvature',[1 1]) axis equal

Figure contains an axes object. The axes object contains an object of type rectangle.

Draw a red rectangle using the same position values to show how the circle fills the rectangular area.

rectangle('Position',pos,'EdgeColor','r')

Figure contains an axes object. The axes object contains 2 objects of type rectangle.

Draw a rectangle and specify the fill color, outline color, and outline width.

rectangle('Position',[1,2,5,10],'FaceColor',[0 .5 .5],'EdgeColor','b',... 'LineWidth',3)

Figure contains an axes object. The axes object contains an object of type rectangle.

Add a rectangle to the current axes and return the rectangle object, r.

r = rectangle('Position',[0 0 1 1]')

Figure contains an axes object. The axes object contains an object of type rectangle.

r = Rectangle with properties:

FaceColor: 'none'
EdgeColor: [0.1294 0.1294 0.1294]
LineWidth: 0.5000
LineStyle: '-'
Curvature: [0 0]
 Position: [0 0 1 1]

Show all properties

Modify the rectangle after creation using r. Change the fill color, outline color, and outline width.

r.FaceColor = [0 .5 .5]; r.EdgeColor = 'b'; r.LineWidth = 3;

Figure contains an axes object. The axes object contains an object of type rectangle.

Input Arguments

collapse all

Size and location of the rectangle, specified as a four-element vector of the form [x y w h]. The x and y elements define the coordinate for the lower left corner of the rectangle. The w and h elements define the dimensions of the rectangle.

All values are in data units.

Example: rectangle('Position',[0.5 0.5 0.3 0.4])

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Amount of horizontal and vertical curvature, specified as a two-element vector or a scalar value. Use this property to vary the shape of the rectangle from rectangular to ellipsoidal.

The horizontal curvature is the fraction of the width that is curved along the top and bottom edges. The vertical curvature is the fraction of the height that is curved along the left and right edges.

Example: [0.5 0.6]

Example: 0.75

Data Types: double

Axes object. If you do not specify an axes object, then rectangle uses the current axes.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: rectangle('Position',[0 0 1 1],'EdgeColor','k','FaceColor',[0 .5 .5])

Outline color, specified as an RGB triplet, a hexadecimal color code, a color name, or a short name. The default RGB triplet value of [0 0 0] corresponds to black.

For a custom color, specify an RGB triplet or a hexadecimal color code.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and the hexadecimal color codes.

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"red" "r" [1 0 0] "#FF0000" Sample of the color red
"green" "g" [0 1 0] "#00FF00" Sample of the color green
"blue" "b" [0 0 1] "#0000FF" Sample of the color blue
"cyan" "c" [0 1 1] "#00FFFF" Sample of the color cyan
"magenta" "m" [1 0 1] "#FF00FF" Sample of the color magenta
"yellow" "y" [1 1 0] "#FFFF00" Sample of the color yellow
"black" "k" [0 0 0] "#000000" Sample of the color black
"white" "w" [1 1 1] "#FFFFFF" Sample of the color white
"none" Not applicable Not applicable Not applicable No color

This table lists the default color palettes for plots in the light and dark themes.

Palette Palette Colors
"gem" — Light theme default_Before R2025a: Most plots use these colors by default._ Sample of the "gem" color palette
"glow" — Dark theme default Sample of the "glow" color palette

You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.

RGB = orderedcolors("gem"); H = rgb2hex(RGB);

Before R2023b: Get the RGB triplets using RGB = get(groot,"FactoryAxesColorOrder").

Before R2024a: Get the hexadecimal color codes using H = compose("#%02X%02X%02X",round(RGB*255)).

Example: 'blue'

Example: [0 0 1]

Example: '#0000FF'

Line width, specified as a positive value in point units.

Example: 0.75

Version History

Introduced before R2006a