diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-06-14 19:58:03 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-06-14 19:58:03 +0000 |
commit | 3a8af6f1536d56fcd50a5f0a0cf264aa62a37599 (patch) | |
tree | 3742b029bc1f4849fd668d1a5af033bbfa0b67c8 | |
parent | 5c73e21b426ed381a1ae8d1bcac6fc78e5952b8f (diff) | |
download | dimension-3a8af6f1536d56fcd50a5f0a0cf264aa62a37599.tar.xz |
New dmnsn_line_index() function.
-rw-r--r-- | libdimension/dimension/geometry.h | 2 | ||||
-rw-r--r-- | libdimension/geometry.c | 28 |
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; +} |