Skip to content

Commit

Permalink
Added methods for calculating length and area.
Browse files Browse the repository at this point in the history
This commit exposes the following methods:

- tg_line_length
- tg_ring_perimeter
- tg_ring_area

See #5
  • Loading branch information
tidwall committed Oct 4, 2023
1 parent 5c505b9 commit f205537
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
27 changes: 27 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ tg_geom_interects((struct tg_geom*)ring, geom);
- [tg_ring_nearest_segment()](#group___ring_funcs_1ga716e10054b4bda84efb259d57aff5015)
- [tg_ring_line_search()](#group___ring_funcs_1gabee40f4a66c2ebb4516a9f980ff5d998)
- [tg_ring_ring_search()](#group___ring_funcs_1gacd2c483213d8110c9373e8e47bd9f49e)
- [tg_ring_area()](#group___ring_funcs_1gabe14408b3ad596ed14b4ce698c2e7826)
- [tg_ring_perimeter()](#group___ring_funcs_1ga0d063ecfef895e21f6d7520db05d302e)


<a name='group___line_funcs'></a>
Expand Down Expand Up @@ -379,6 +381,7 @@ tg_geom_interects((struct tg_geom*)line, geom);
- [tg_line_index_level_rect()](#group___line_funcs_1gacc18e4ddb48ccc717f61f298fd6ae970)
- [tg_line_nearest_segment()](#group___line_funcs_1gaa85835e9619ba7d8557fda3335a0353a)
- [tg_line_line_search()](#group___line_funcs_1gafef8ca6e5381e93d4c4952343597c0e1)
- [tg_line_length()](#group___line_funcs_1ga994796f0088c82bb0672d84f1a1ce9d1)


<a name='group___poly_funcs'></a>
Expand Down Expand Up @@ -3834,6 +3837,22 @@ Iterates over all segments in ring A that intersect with segments in ring B.



<a name='group___ring_funcs_1gabe14408b3ad596ed14b4ce698c2e7826'></a>
## tg_ring_area()
```c
double tg_ring_area(const struct tg_ring *ring);
```
Calculate the area of a ring.


<a name='group___ring_funcs_1ga0d063ecfef895e21f6d7520db05d302e'></a>
## tg_ring_perimeter()
```c
double tg_ring_perimeter(const struct tg_ring *ring);
```
Calculate the perimeter length of a ring.


<a name='group___line_funcs_1ga7f834a7213c8d87d8b7c7117bdf8bf63'></a>
## tg_line_new()
```c
Expand Down Expand Up @@ -4326,6 +4345,14 @@ Iterates over all segments in line A that intersect with segments in line B.



<a name='group___line_funcs_1ga994796f0088c82bb0672d84f1a1ce9d1'></a>
## tg_line_length()
```c
double tg_line_length(const struct tg_line *line);
```
Calculate the length of a line.


<a name='group___poly_funcs_1ga56c2615488ca202baa944c85c20a40f1'></a>
## tg_poly_new()
```c
Expand Down
2 changes: 1 addition & 1 deletion tests/test_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void test_line_various() {
assert(tg_line_memsize(NULL) == 0);
tg_line_free(line2);
assert(!tg_line_copy(0));

assert(tg_line_length(LINE(u1)) == 30.0);
}

int main(int argc, char **argv) {
Expand Down
2 changes: 2 additions & 0 deletions tests/test_ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,8 @@ void test_ring_various() {
assert(tg_ring_contains_line(0, (struct tg_line*)RING(az), 0, 0) == 0);
assert(tg_ring_contains_line(0, 0, 0, 0) == 0);

assert(tg_ring_perimeter(RING(u1)) == 40.0);
assert(tg_ring_area(RING(u1)) == 100.0);
}

void test_ring_pip(void) {
Expand Down
15 changes: 11 additions & 4 deletions tg.c
Original file line number Diff line number Diff line change
Expand Up @@ -13511,13 +13511,15 @@ struct tg_geom *tg_parse_hex(const char *hex) {
return tg_parse_hexn_ix(hex, hex?strlen(hex):0, TG_DEFAULT);
}

static double ring_area(const struct tg_ring *ring) {
/// Calculate the area of a ring.
double tg_ring_area(const struct tg_ring *ring) {
if (tg_ring_empty(ring)) return 0;
// The ring area has already been calculated by process_points.
return ring->area;
}

static double ring_perimeter(const struct tg_ring *ring) {
/// Calculate the perimeter length of a ring.
double tg_ring_perimeter(const struct tg_ring *ring) {
if (tg_ring_empty(ring)) return 0;
int nsegs = tg_ring_num_segments(ring);
double perim = 0;
Expand All @@ -13538,8 +13540,8 @@ double tg_ring_polsby_popper_score(const struct tg_ring *ring) {
// and all other shapes will be smaller. Itty bitty scores mean the
// polygon is really something nuts or has bad data.
double score = 0.0;
double perim = ring_perimeter(ring);
double area = ring_area(ring);
double perim = tg_ring_perimeter(ring);
double area = tg_ring_area(ring);
if (perim > 0) {
score = (area * M_PI * 4) / (perim * perim);
}
Expand Down Expand Up @@ -14263,3 +14265,8 @@ void tg_geom_search(const struct tg_geom *geom, struct tg_rect rect,
multi_index_search(multi, rect, 0, 0, iter, udata);
}
}

/// Calculate the length of a line.
double tg_line_length(const struct tg_line *line) {
return tg_ring_perimeter((struct tg_ring*)line);
}
4 changes: 3 additions & 1 deletion tg.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ void tg_ring_ring_search(const struct tg_ring *a, const struct tg_ring *b,
bool (*iter)(struct tg_segment aseg, int aidx, struct tg_segment bseg,
int bidx, void *udata),
void *udata);

double tg_ring_area(const struct tg_ring *ring);
double tg_ring_perimeter(const struct tg_ring *ring);
/// @}

/// @defgroup LineFuncs Line functions
Expand Down Expand Up @@ -308,6 +309,7 @@ void tg_line_line_search(const struct tg_line *a, const struct tg_line *b,
bool (*iter)(struct tg_segment aseg, int aidx, struct tg_segment bseg,
int bidx, void *udata),
void *udata);
double tg_line_length(const struct tg_line *line);
/// @}

/// @defgroup PolyFuncs Polygon functions
Expand Down

0 comments on commit f205537

Please sign in to comment.