30 行
1.2 KiB
C++
30 行
1.2 KiB
C++
#pragma once
|
|
#include <CoreGraphics/CoreGraphics.h>
|
|
#include <cmath>
|
|
#include <algorithm>
|
|
|
|
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);
|
|
return std::remainder((vertical ? M_PI_2 : 0.) - angle, M_PI);
|
|
}
|
|
|
|
// 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, double displayAspect)
|
|
: 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);
|
|
}
|
|
};
|