Skip to content

Commit

Permalink
Merge pull request #257 from ebassi/point-distance-helpers
Browse files Browse the repository at this point in the history
Add squared distance operator for 2D points
  • Loading branch information
ebassi authored Jul 21, 2023
2 parents 5c142f1 + 362c9d8 commit a0595ec
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/graphene-sections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ graphene_point_init_from_point
graphene_point_init_from_vec2
graphene_point_equal
graphene_point_distance
graphene_point_distance_squared
graphene_point_near
graphene_point_interpolate
graphene_point_to_vec2
Expand Down
4 changes: 4 additions & 0 deletions include/graphene-point.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ float graphene_point_distance (const graphene_
float *d_x,
float *d_y);

GRAPHENE_AVAILABLE_IN_1_12
float graphene_point_distance_squared (const graphene_point_t *a,
const graphene_point_t *b);

GRAPHENE_AVAILABLE_IN_1_0
bool graphene_point_near (const graphene_point_t *a,
const graphene_point_t *b,
Expand Down
25 changes: 25 additions & 0 deletions src/graphene-point.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ graphene_point_distance (const graphene_point_t *a,
return graphene_simd4f_get_x (graphene_simd4f_length2 (v_res));
}

/**
* graphene_point_distance_squared:
* @a: a #graphene_point_t
* @b: a #graphene_point_t
*
* Computes the squared distance between @a and @b.
*
* Returns: the distance between the two points, squared
*
* Since: 1.12
*/
float
graphene_point_distance_squared (const graphene_point_t *a,
const graphene_point_t *b)
{
if (a == b)
return 0.f;

graphene_simd4f_t v_a = graphene_simd4f_init (a->x, a->y, 0.f, 0.f);
graphene_simd4f_t v_b = graphene_simd4f_init (b->x, b->y, 0.f, 0.f);
graphene_simd4f_t v_res = graphene_simd4f_sub (v_a, v_b);

return graphene_simd4f_get_x (graphene_simd4f_dot2 (v_res, v_res));
}

/**
* graphene_point_near:
* @a: a #graphene_point_t
Expand Down
6 changes: 6 additions & 0 deletions tests/point.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ point_distance (mutest_spec_t *spec)
mutest_float_value (y_d),
mutest_to_be_close_to, 1.0, 0.00001,
NULL);

d = graphene_point_distance_squared (&p, &q);
mutest_expect ("the squared distance between (0, 0) and (1, 1) to be 2",
mutest_float_value (d),
mutest_to_be_close_to, 2.f, 0.0001,
NULL);
}

static void
Expand Down

0 comments on commit a0595ec

Please sign in to comment.