### 5 releases

0.2.3 | Oct 29, 2024 |
---|---|

0.2.2 | Aug 25, 2024 |

0.2.1 | Aug 22, 2024 |

0.2.0 | Aug 21, 2024 |

0.1.0 | Aug 21, 2024 |

#**52** in Graphics APIs

**131** downloads per month

**MIT**license

68KB

1.5K
SLoC

# elements

A geometry markup language and diagram renderer.

## Development

There are two main parts to this project: the markup language and the diagram renderer. The functions that make up the markup
language are mostly defined in the

folder, while the lexing and interpreting functions are defined in `lang`

and
`lexer.rs`

in the main `interpreter.rs`

folder respectively. These functions first convert a source string into a vector of `src`

s,
and then interpret these tokens into a set of `Token`

s. These `Value`

s then implement the `Value`

trait, which will turn them
into `Element`

objects that hold the `Svg`

trait. The rendering system, located in the file `Render`

, then takes these objects
and outputs the correct svg code.`renderer.rs`

- implement better labelling system

Note: main repository is developed using Mercurial, at https://hg.sr.ht/~lnjng/elements.

## Usage

To install the program, simply use cargo:

`cargo`` install elements-lang`

To run the program, use the following command:

`elements`` ``<`input file`>`

The program will then output the svg code to stdout as well as to a file called

. To enable the labelling system, the
`out .svg`

`--``label`

flag can be used.Here is an example to render a triangle:

`(``setq` A `(`point `0` `0``)``)`
`(``setq` B `(`point `0` `3``)``)`
`(``setq` C `(`point `4` `0``)``)`
`(`triangle A B C`)`

More examples can be found under the

directory.`examples`

## Reference

The language is written in simple lisp syntax. Notably, however, you can simply write out the variable name with no parantheses, and the interpreter will automatically substitute the value of the variable and render the appropriate object. For example, the following code will render a triangle with vertices at (0, 0), (0, 3), and (4, 0):

`;` this is a triangle
`(``setq` A `(`point `0` `0``)``)`
`(``setq` B `(`point `0` `3``)``)`
`(``setq` C `(`point `4` `0``)``)`
`(``setq` `T` `(`triangle A B C`)``)`
T

Notice how the

function is used to set variables, and how comments are started with a semicolon.`setq`

Functions are also often overloaded to provide more functionality with the same easy syntax. The following are the available geometric functions:

`point`

`point`

`(`point [Int`/``Float`] [Int`/``Float`]`)` -> Point

The

function creates a point with the given x and y coordinates in the first and second parameters respectively.`point`

`circumcenter`

`circumcenter`

`(`circumcenter [Triangle]`)` -> Point

The

function takes in a triangle and returns the circumcenter of that triangle.`circumcenter`

`orthocenter`

`orthocenter`

`(`orthocenter [Triangle]`)` -> Point

The

function takes in a triangle and returns the orthocenter of that triangle.`orthocenter`

`centroid`

`centroid`

`(`centroid [Triangle]`)` -> Point

The

function takes in a triangle and returns the centroid of that triangle.`centroid`

`inradius`

`inradius`

`(`inradius [Triangle]`)` -> Int/Float

The

function takes in a triangle and returns the inradius of that triangle.`inradius`

`incenter`

`incenter`

`(`incenter [Triangle]`)` -> Point

The

function takes in a triangle and returns the incenter of that triangle.`incenter`

`lineseg`

`lineseg`

`(`lineseg [Point] [Point]`)` -> Lineseg

The

function creates a line segment with the given two points as the endpoints.`lineseg`

`midpoint`

`midpoint`

`(`midpoint [Point] [Point]`)` -> Point

The

function returns a point that is the midpoint of the two given points.`midpoint`

`triangle`

`triangle`

`(`triangle [Point] [Point] [Point]`)` -> Triangle

The first case for creating a triangle involves three parameters. The three parameters are the three vertices of the triangle.

`(`triangle [Angle]`)` -> Triangle

The second case for creating a triangle is given an angle (i.e. two connected line segments), the function will create a triangle with the angle as the vertex.

`(`triangle [Circle]`)` -> Triangle

The third and ambiguous case, when given a circle, the function will return a randomly generated inscribed triangle. The triangle will have points that are greater than half the radius apart.

`circle`

`circle`

`(`circle [Point] [Int`/``Float`]`)` -> Circle

The first case for creating a circle involves two parameters. The first is a parameter denoting the center of the circle. The second parameter is a number denoting the radius of the circle.

`(`circle`)` -> Circle

An ambiguous case for this function, when no parameters are given this function will create a standard circle at (0, 0) with radius 5.

`angle`

`angle`

`(`angle [Point] [Point] [Point]`)` -> Angle

The

function creates an angle from three points denoted in the three parameters.`angle`

`iangle`

`iangle`

`(`iangle [Circle] [Int`/``Float`]`)` -> Angle

The

function creates an inscribed angle in a circle. The first parameter is the circle, and the second parameter is the
angle in degrees.`iangle`

`intersect`

`intersect`

`(`intersect [Lineseg] [Circle] [Int]`)` -> Point

The first case for the

function involves three parameters. The first parameter is a line segment, the second is a circle,
and the third is an int representing either 0 or 1, the index of the point of intersection. As a line can maximally meet a circle at
two points, the index is used to determine which point to return.`intersect`

`(`intersect [Lineseg] [Lineseg]`)` -> Point

The second case for the

function involves two line segments. The function will return the point of intersection between
the two line segments.`intersect`

#### Dependencies

~310KB