This page documents library functions that render polygons.
n-Sided Regular Polygon
dpolygon(n,pcentre,r,d) Generate vertices of an n-sided regular polygon.
Arguments
| Argument | Description | Type | Constraints | Notes |
|---|
| n | number of sides | Z+ | 3≤n (required) | |
| pcentre | centre | (R,R) | | |
| r | radius of the polygon | R | r=0 (required) 0<r | “Radius” refers to the distance from the centre of the polygon to any vertex. |
| d | direction (rotation) | R | 0≤d<2π | |
Return
| Value | Description | Type | Constraints | Notes |
|---|
| ω | polygon vertices | [(R,R),...] | | Use polygon(ω) to render the polygon. |
Usage
f_{nrange}left(s_{start}, s_{stop}, n\right)=left[left(s_{start}+ileft(rac{s_{stop}-s_{start}}{n-1}\right)\right)operatorname{for}i=left[0...left(n-1\right)\right]\right]
d_{polygon}left(n, p_{centre}, r, d\right)=left[left(p_{centre}.x+rcos\theta, p_{centre}.y+rsin\theta\right)operatorname{for}\theta=f_{nrange}left(d, d+2pi, n+1\right)\right]
operatorname{polygon}left(d_{polygon}left(6, left(0, 0\right), 4, rac{pi}{6}\right)\right)
Implementation
dpolygon(n,pcentre,r,d)=[(pcentre.x+rcosθ, pcentre.y+rsinθ)forθ=fnrange(d, d+2π, n+1)] Dependencies
- fnrange()
Rectangle
drect(pcentre,x,y) Draw a rectangle at centre pcentre with dimensions x×y.
Arguments
| Argument | Description | Type | Constraints | Notes |
|---|
| pcentre | rectangle centre | (R,R) | | |
| x | rectangle width | R+ | | |
| y | rectangle height | R+ | | |
Return
None
Usage
d_{rect}left(p_{centre}, s_{width}, s_{height}\right)=operatorname{polygon}left(p_{centre}+left(-rac{s_{width}}{2}, -rac{s_{height}}{2}\right), p_{centre}+left(rac{s_{width}}{2}, -rac{s_{height}}{2}\right), p_{centre}+left(rac{s_{width}}{2}, rac{s_{height}}{2}\right), p_{centre}+left(-rac{s_{width}}{2}, rac{s_{height}}{2}\right)\right)
d_{rect}left(left(3, 2\right), 4, 1\right)
Implementation
drect(pcentre,x,y)=polygon(pcentre+(−2x, −2y), pcentre+(2x, −2y), pcentre+(2x, 2y), pcentre+(−2x, 2y)) Dependencies
None
Aligned Rectangle
drectaligned(ppivot,pxy,palign) Draw a rectangle at pivot ppivot with dimensions pxy.
Arguments
| Argument | Description | Domain | Constraints | Notes |
|---|
| ppivot | pivot point | (R,R) | | |
| pxy | rectangle dimensions | (R,R) | | |
| palign | alignment | (R,R) | (−1,−1)≤palign≤(1,1) | (−1,−1) means the upper-right corner is used as the pivot (the rectangle is drawn in the negative quadrant). |
Return
None
Usage
d_{rectaligned}left(p_{pivot}, p_{xy}, p_{align}\right)=operatorname{polygon}left(p_{pivot}+left(left(p_{align}.x-1\right)rac{p_{xy}.x}{2}, left(p_{align}.y-1\right)rac{p_{xy}.y}{2}\right), p_{pivot}+left(left(p_{align}.x+1\right)rac{p_{xy}.x}{2}, left(p_{align}.y-1\right)rac{p_{xy}.y}{2}\right), p_{pivot}+left(left(p_{align}.x+1\right)cdotrac{p_{xy}.x}{2}, left(p_{align}.y+1\right)rac{p_{xy}.y}{2}\right), p_{pivot}+left(left(p_{align}.x-1\right)cdotrac{p_{xy}.x}{2}, left(p_{align}.y+1\right)rac{p_{xy}.y}{2}\right)\right)
d_{rectaligned}left(left(0, 0\right), left(3, 4\right), left(-1, -1\right)\right)
d_{rectaligned}left(left(2, 2\right), left(5, 5\right), left(1, -1\right)\right)
Implementation
drectaligned(ppivot,pxy,palign)=polygon(ppivot+((palign.x−1)2pxy.x, (palign.y−1)2pxy.y), ppivot+((palign.x+1)2pxy.x, (palign.y−1)2pxy.y), ppivot+((palign.x+1)⋅2pxy.x, (palign.y+1)2pxy.y), ppivot+((palign.x−1)⋅2pxy.x, (palign.y+1)2pxy.y)) Dependencies
None