From: buliabyak Date: Tue, 16 Oct 2007 05:09:08 +0000 (+0000) Subject: add some utility functions from gradient code X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=7cffa9e201bbf676266b476c6fbdeef3c34a1bbc;p=inkscape.git add some utility functions from gradient code --- diff --git a/src/libnr/nr-point-fns.cpp b/src/libnr/nr-point-fns.cpp index a92c2b00b..a18e878b4 100644 --- a/src/libnr/nr-point-fns.cpp +++ b/src/libnr/nr-point-fns.cpp @@ -61,6 +61,48 @@ NR::Point abs(NR::Point const &b) return ret; } +NR::Point * +get_snap_vector (NR::Point p, NR::Point o, double snap, double initial) +{ + double r = NR::L2 (p - o); + if (r < 1e-3) + return NULL; + double angle = NR::atan2 (p - o); + // snap angle to snaps increments, starting from initial: + double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap; + // calculate the new position and subtract p to get the vector: + return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p); +} + +NR::Point +snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap) +{ + double length = NR::L2(end - begin); + NR::Point be = (end - begin) / length; + double r = NR::dot(p - begin, be); + + if (r < 0.0) return begin; + if (r > length) return end; + + double snapdist = length * snap; + double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist; + + return (begin + r_snapped * be); +} + +double +get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end) +{ + double length = NR::L2(end - begin); + NR::Point be = (end - begin) / length; + double r = NR::dot(p - begin, be); + + if (r < 0.0) return 0.0; + if (r > length) return 1.0; + + return (r / length); +} + /* Local Variables: diff --git a/src/libnr/nr-point-fns.h b/src/libnr/nr-point-fns.h index 9f9a8f9e2..a8c38466f 100644 --- a/src/libnr/nr-point-fns.h +++ b/src/libnr/nr-point-fns.h @@ -90,6 +90,12 @@ Point abs(Point const &b); } /* namespace NR */ +NR::Point *get_snap_vector (NR::Point p, NR::Point o, double snap, double initial); + +NR::Point snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap); + +double get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end); + #endif /* !__NR_POINT_OPS_H__ */ /*