---
name: "Rect"
section: "3g"
platform: "linux"
package: "libxg-dev"
version: "4.3-14"
release: "Debian 3.0 (Woody)"
platforms: ["linux"]
url: "https://new.carta.tech/add"
---

# Rect(3g)

arithmetic on points and rectangles

Part of [add(3g)](/add/3g.md).

## NAME

add, sub, mul, divpt, raddp, rsubp, rmul, rdiv, rshift, inset, rcanon, eqpt, eqrect, ptinrect, rectinrect, rectXrect, rectclip, Dx, Dy, Pt, Rect, Rpt - arithmetic on points and rectangles

## SYNOPSIS

```
#include <libc.h>
#include <libg.h>
```

**Point	add(Point p, Point q)**

**Point	sub(Point p, Point q)**

**Point	mul(Point p, int a)**

**Point	divpt(Point p, int a)**

**Rectangle	raddp(Rectangle r, Point p)**

**Rectangle	rsubp(Rectangle r, Point p)**

**Rectangle	rmul(Rectangle r, int a)**

**Rectangle	rdiv(Rectangle r, int a)**

**Rectangle	rshift(Rectangle r, int a)**

**Rectangle	inset(Rectangle r, int n)**

**Rectangle	rcanon(Rectangle r)**

**int	eqpt(Point p, Point q)**

**int	eqrect(Rectangle r, Rectangle s)**

**int	ptinrect(Point p, Rectangle r)**

**int	rectinrect(Rectangle r, Rectangle s)**

**int	rectXrect(Rectangle r, Rectangle s)**

**int	rectclip(Rectangle \*rp, Rectangle b)**

**int	Dx(Rectangle r);**

**int	Dy(Rectangle r);**

**Point	Pt(int x, int y)**

**Rectangle	Rect(int x0, int y0, int x1, int y1)**

**Rectangle	Rpt(Point p, Point q)**

## DESCRIPTION

The functions *Pt*, *Rect* and *Rpt* construct geometrical data types from their components. These are implemented as functions.  (Under Plan 9 these are implemented as macros.)

*Add* returns the Point sum of its arguments: **Pt(***p***.x+***q***.x,** *p***.y+***q***.y).** *Sub* returns the Point difference of its arguments: **Pt(***p***.x-***q***.x,** *p***.y-***q***.y).** *Mul* returns the Point **Pt(***p***.x\****a***,** *p***.y\****a***).** *Divpt* returns the Point **Pt(***p***.x/***a***,** *p***.y/***a***).**

*Raddp* returns the Rectangle **Rect(add(***r***.min,** *p***),** **add(***r***.max,** *p***));** *rsubp* returns the Rectangle **Rpt(sub(***r***.min,** *p***),** **sub(***r***.max,** *p***)).** *Rmul* returns the Rectangle **Rpt(mul(***r***.min,***a***),** **mul(***r***.max,***a***));** *Rdiv* returns the Rectangle **Rpt(div(***r***.min,***a***),** **div(***r***.max,***a***)).**

*Rshift* returns the rectangle *r* with all coordinates either left-shifted or right-shifted by *a*, depending on whether *a* is positive or negative, respectively.

*Inset* returns the Rectangle **Rect(***r***.min.x+***n***,** *r***.min.y+***n***,** *r***.max.x-***n***,** *r***.max.y-***n***)***.*

*Rcanon* returns a rectangle with the same extent as *r*, canonicalized so that **min.x** \<= **max.x**, and **min.y** \<= **max.y**.

*Eqpt* compares its argument Points and returns 0 if unequal, 1 if equal. *Eqrect* does the same for its argument Rectangles.

*Ptinrect* returns 1 if *p* is a point within *r*, and 0 otherwise.

*Rectinrect* returns 1 if all the pixels in *r* are also in *s*, and 0 otherwise.

*RectXrect* returns 1 if *r* and *s* share any point, and 0 otherwise.

*Rectclip* clips in place the Rectangle pointed to by *rp* so that it is completely contained within *b*. The return value is 1 if any part of \**rp* is within *b*. Otherwise, the return value is 0 and \**rp* is unchanged.

The functions *Dx* and *Dy* give the width (delta x) and height (delta y) of a Rectangle. They are implemented as macros.

## SEE ALSO

[*graphics*(3g)](/graphics/3g.md).
