summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libdimension/dimension/geometry.h2
-rw-r--r--libdimension/geometry.c28
2 files changed, 29 insertions, 1 deletions
diff --git a/libdimension/dimension/geometry.h b/libdimension/dimension/geometry.h
index a2e54ab..4effea8 100644
--- a/libdimension/dimension/geometry.h
+++ b/libdimension/dimension/geometry.h
@@ -76,5 +76,7 @@ dmnsn_line dmnsn_matrix_line_mul(dmnsn_matrix lhs, dmnsn_line rhs);
/* A point on a line, defined by x0 + t*n */
dmnsn_vector dmnsn_line_point(dmnsn_line l, double t);
+/* Solve for the t value such that x0 + t*n = x */
+double dmnsn_line_index(dmnsn_line l, dmnsn_vector x);
#endif /* DIMENSION_GEOMETRY_H */
diff --git a/libdimension/geometry.c b/libdimension/geometry.c
index edb73cd..026932b 100644
--- a/libdimension/geometry.c
+++ b/libdimension/geometry.c
@@ -235,7 +235,8 @@ dmnsn_matrix_line_mul(dmnsn_matrix lhs, dmnsn_line rhs)
l.x0 = dmnsn_matrix_vector_mul(lhs, rhs.x0);
l.n = dmnsn_vector_sub(
dmnsn_matrix_vector_mul(lhs, dmnsn_vector_add(rhs.x0, rhs.n)),
- l.x0);
+ l.x0
+ );
return l;
}
@@ -245,3 +246,28 @@ dmnsn_line_point(dmnsn_line l, double t)
{
return dmnsn_vector_add(l.x0, dmnsn_vector_mul(t, l.n));
}
+
+/* Solve for the t value such that x0 + t*n = x */
+double
+dmnsn_line_index(dmnsn_line l, dmnsn_vector x)
+{
+ double d = 0.0;
+ unsigned int nz = 0;
+
+ if (l.n.x != 0.0) {
+ d += (x.x - l.x0.x)/l.n.x;
+ ++nz;
+ }
+
+ if (l.n.y != 0.0) {
+ d += (x.y - l.x0.y)/l.n.y;
+ ++nz;
+ }
+
+ if (l.n.z != 0.0) {
+ d += (x.z - l.x0.z)/l.n.z;
+ ++nz;
+ }
+
+ return d/nz;
+}