use super::sampled_contour::*;
use super::column_sampled_contour::*;
use smallvec::*;
use std::ops::{Range};
#[derive(Clone)]
pub struct ScaledContour<TContour> {
contour: TContour,
scale_factor: f64,
offset_x: f64,
offset_y: f64,
size: ContourSize,
}
impl<TContour> ScaledContour<TContour>
where
TContour: SampledContour,
{
#[inline]
pub fn from_contour(contour: TContour, scale_factor: f64, offset: (f64, f64)) -> Self {
let ContourSize(width, height) = contour.contour_size();
let width = (width as f64) * scale_factor + offset.0;
let height = (height as f64) * scale_factor + offset.1;
let width = width.ceil();
let height = height.ceil();
let size = ContourSize(width as _, height as _);
let (offset_x, offset_y) = offset;
ScaledContour { contour, scale_factor, size, offset_x, offset_y }
}
}
impl<TContour> SampledContour for ScaledContour<TContour>
where
TContour: SampledContour,
{
#[inline]
fn contour_size(&self) -> ContourSize {
self.size
}
#[inline]
fn intercepts_on_line(&self, y: f64) -> SmallVec<[Range<f64>; 4]> {
let y = (y - self.offset_y) / self.scale_factor;
self.contour.intercepts_on_line(y)
.into_iter()
.map(|range| {
(range.start * self.scale_factor + self.offset_x)..(range.end * self.scale_factor + self.offset_x)
})
.collect()
}
}
impl<TContour> ColumnSampledContour for ScaledContour<TContour>
where
TContour: ColumnSampledContour,
{
#[inline]
fn intercepts_on_column(&self, x: f64) -> SmallVec<[Range<f64>; 4]> {
let x = (x - self.offset_x) / self.scale_factor;
self.contour.intercepts_on_column(x)
.into_iter()
.map(|range| {
(range.start * self.scale_factor + self.offset_y)..(range.end * self.scale_factor + self.offset_y)
})
.collect()
}
}