libflint/docs/math.md

75 lines
1.4 KiB
Markdown
Raw Permalink Normal View History

# math
2022-03-31 00:02:36 +00:00
General math functions
## Functions
## max_int
Return the maximum integer between `int a` and `int b`
```c
int max_int(int a, int b);
```
## min_int
Return the minimum integer between `int a` and `int b`
```c
int min_int(int a, int b);
```
## clamp_int
Clamps an integer between a high and low value
```c
int clamp_int(int i, int low, int high);
```
## binstr_to_int
Converts a string representing a binary number into an integer. Supports underscores as spacing
```c
int binstr_to_int(const char *s);
/* Usage */
int a = binstr_to_int("10011101");
int b = binstr_to_int("1001_1101_0010_1011");
```
## bresenham
Uses bresenham's line algorithim to generate a line in 2D space.
Returns a pointer to an array of `Point`.
The `sz` parameter holds the size of the array.
```c
Point *bresenham(int x0, int y0, int x1, int y1, size_t *sz);
```
## bresenham_p
Works the same as `bresenham()` but uses the `Point` struct instead of `int`
```c
Point *bresenham_p(Point p1, Point p2, size_t *sz);
```
2024-05-06 19:17:13 +00:00
## Comparison Functions
Comparison functions to compare two items. These functions must return `1` if `a > b`, `-1` if
`a < b`, or `0` if `a == b`. This follows the pattern defined by `compar` functions in the C standard
library, making these usuable in the standard sorting functions like `qsort`
```c
int compar_int(const void *a, const void *b);
int compar_char(const void *a, const void *b);
// Example
qsort(arr, arr_sz, sizeof(int), compar_int);
```