252 } |
252 } |
253 } |
253 } |
254 }; |
254 }; |
255 } |
255 } |
256 |
256 |
|
257 macro_rules! fp_scalar_bin_op_impl { |
|
258 ($($op: tt)::+, $name: tt) => { |
|
259 impl $($op)::+<FPNum> for Point { |
|
260 type Output = FPPoint; |
|
261 |
|
262 #[inline] |
|
263 fn $name(self, rhs: FPNum) -> Self::Output { |
|
264 FPPoint::new(rhs.$name(self.x), rhs.$name(self.y)) |
|
265 } |
|
266 } |
|
267 }; |
|
268 } |
|
269 |
|
270 macro_rules! left_fp_scalar_bin_op_impl { |
|
271 ($($op: tt)::+, $name: tt) => { |
|
272 impl $($op)::+<Point> for FPNum { |
|
273 type Output = FPPoint; |
|
274 |
|
275 #[inline] |
|
276 fn $name(self, rhs: Point) -> Self::Output { |
|
277 FPPoint::new(self.$name(rhs.x), self.$name(rhs.y)) |
|
278 } |
|
279 } |
|
280 }; |
|
281 } |
|
282 |
257 bin_op_impl!(Add, add); |
283 bin_op_impl!(Add, add); |
258 bin_op_impl!(Sub, sub); |
284 bin_op_impl!(Sub, sub); |
259 bin_op_impl!(Mul, mul); |
285 bin_op_impl!(Mul, mul); |
260 bin_op_impl!(Div, div); |
286 bin_op_impl!(Div, div); |
261 scalar_bin_op_impl!(Mul, mul); |
287 scalar_bin_op_impl!(Mul, mul); |
262 scalar_bin_op_impl!(Div, div); |
288 scalar_bin_op_impl!(Div, div); |
|
289 fp_scalar_bin_op_impl!(Mul, mul); |
|
290 fp_scalar_bin_op_impl!(Div, div); |
|
291 left_fp_scalar_bin_op_impl!(Mul, mul); |
|
292 left_fp_scalar_bin_op_impl!(Div, div); |
263 bin_assign_op_impl!(AddAssign, add_assign); |
293 bin_assign_op_impl!(AddAssign, add_assign); |
264 bin_assign_op_impl!(SubAssign, sub_assign); |
294 bin_assign_op_impl!(SubAssign, sub_assign); |
265 bin_assign_op_impl!(MulAssign, mul_assign); |
295 bin_assign_op_impl!(MulAssign, mul_assign); |
266 bin_assign_op_impl!(DivAssign, div_assign); |
296 bin_assign_op_impl!(DivAssign, div_assign); |
267 |
297 |
789 } |
819 } |
790 } |
820 } |
791 |
821 |
792 pub struct BezierCurveSegments { |
822 pub struct BezierCurveSegments { |
793 segment: Line, |
823 segment: Line, |
794 c1: FPPoint, |
824 control_point1: FPPoint, |
795 c2: FPPoint, |
825 control_point2: FPPoint, |
796 offset: FPNum, |
826 offset: FPNum, |
797 max_offset: FPNum, |
827 max_offset: FPNum, |
798 delta: FPNum, |
828 delta: FPNum, |
799 have_finished: bool, |
829 have_finished: bool, |
800 } |
830 } |
801 |
831 |
802 impl BezierCurveSegments { |
832 impl BezierCurveSegments { |
803 pub fn new(segment: Line, p1: FPPoint, p2: FPPoint, delta: FPNum) -> Self { |
833 pub fn new(segment: Line, p1: FPPoint, p2: FPPoint, delta: FPNum) -> Self { |
804 Self { |
834 Self { |
805 segment, |
835 segment, |
806 c1: segment.start.to_fppoint() - p1, |
836 control_point1: segment.start.to_fppoint() - p1, |
807 c2: segment.end.to_fppoint() - p2, |
837 control_point2: segment.end.to_fppoint() - p2, |
808 offset: fp!(0), |
838 offset: fp!(0), |
809 max_offset: fp!(4095 / 4096), |
839 max_offset: fp!(4095 / 4096), |
810 delta, |
840 delta, |
811 have_finished: false, |
841 have_finished: false, |
812 } |
842 } |
823 |
853 |
824 let r1 = fp!(1) - self.offset * 3 + offset_sq * 3 - offset_cub; |
854 let r1 = fp!(1) - self.offset * 3 + offset_sq * 3 - offset_cub; |
825 let r2 = self.offset * 3 - offset_sq * 6 + offset_cub * 3; |
855 let r2 = self.offset * 3 - offset_sq * 6 + offset_cub * 3; |
826 let r3 = offset_sq * 3 - offset_cub * 3; |
856 let r3 = offset_sq * 3 - offset_cub * 3; |
827 |
857 |
828 let x = r1 * self.segment.start.x |
858 let p = r1 * self.segment.start |
829 + r2 * self.c1.x() |
859 + r2 * self.control_point1 |
830 + r3 * self.c2.x() |
860 + r3 * self.control_point2 |
831 + offset_cub * self.segment.end.x; |
861 + offset_cub * self.segment.end; |
832 let y = r1 * self.segment.start.y |
|
833 + r2 * self.c1.y() |
|
834 + r3 * self.c2.y() |
|
835 + offset_cub * self.segment.end.y; |
|
836 |
862 |
837 self.offset += self.delta; |
863 self.offset += self.delta; |
838 |
864 |
839 Some(Point::new(x.round(), y.round())) |
865 Some(Point::from_fppoint(&p)) |
840 } else if !self.have_finished { |
866 } else if !self.have_finished { |
841 self.have_finished = true; |
867 self.have_finished = true; |
842 |
868 |
843 Some(self.segment.end) |
869 Some(self.segment.end) |
844 } else { |
870 } else { |