#pragma once #include #include #include inline double DYLevelDelta(CGPoint first, CGPoint second, double aspect, bool vertical) { const double angle = std::atan2(second.y - first.y, (second.x - first.x) * aspect); const double delta = std::remainder((vertical ? M_PI_2 : 0.) - angle, M_PI); return delta <= -M_PI_2 + 1e-12 ? M_PI_2 : delta; } // Coordinates are normalized to a top-down image. Rotation is measured in // display pixels so the same line aligns in landscape, portrait and anamorphic footage. struct DYLevelTransform { double aspect, cosine, sine, zoom; DYLevelTransform(double angle = 0, double displayAspect = 1) : aspect(displayAspect), cosine(std::cos(angle)), sine(std::sin(angle)), zoom(std::max(1., std::abs(cosine) + std::abs(sine) * std::max(aspect, 1. / aspect))) {} CGPoint toSource(CGPoint output) const { const double x = (output.x - .5) * aspect / zoom, y = (output.y - .5) / zoom; return CGPointMake(.5 + (cosine * x + sine * y) / aspect, .5 + cosine * y - sine * x); } CGPoint toOutput(CGPoint source) const { const double x = (source.x - .5) * aspect * zoom, y = (source.y - .5) * zoom; return CGPointMake(.5 + (cosine * x - sine * y) / aspect, .5 + cosine * y + sine * x); } };