2023-01-15 17:01:27 +00:00
|
|
|
#include "CubicBezierCurve.h"
|
|
|
|
|
|
|
|
CubicBezierCurve::CubicBezierCurve(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) : x1(x1), y1(y1), x2(x2), y2(y2), x3(x3), y3(y3), x4(x4), y4(y4) {}
|
|
|
|
|
2024-01-07 16:17:20 +00:00
|
|
|
Point CubicBezierCurve::nextVector(double t) {
|
2023-01-15 17:01:27 +00:00
|
|
|
double pow1 = pow(1 - t, 3);
|
|
|
|
double pow2 = pow(1 - t, 2);
|
|
|
|
double pow3 = pow(t, 2);
|
|
|
|
double pow4 = pow(t, 3);
|
|
|
|
|
|
|
|
double x = pow1 * x1 + 3 * pow2 * t * x2 + 3 * (1 - t) * pow3 * x3 + pow4 * x4;
|
|
|
|
double y = pow1 * y1 + 3 * pow2 * t * y2 + 3 * (1 - t) * pow3 * y3 + pow4 * y4;
|
|
|
|
|
2024-01-07 16:17:20 +00:00
|
|
|
return Point(x, y);
|
2023-01-15 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 19:48:02 +00:00
|
|
|
void CubicBezierCurve::scale(double x, double y, double z) {
|
2023-01-15 17:01:27 +00:00
|
|
|
x1 *= x;
|
|
|
|
y1 *= y;
|
|
|
|
x2 *= x;
|
|
|
|
y2 *= y;
|
|
|
|
x3 *= x;
|
|
|
|
y3 *= y;
|
|
|
|
x4 *= x;
|
|
|
|
y4 *= y;
|
|
|
|
}
|
|
|
|
|
2024-01-07 19:48:02 +00:00
|
|
|
void CubicBezierCurve::translate(double x, double y, double z) {
|
2023-01-15 17:01:27 +00:00
|
|
|
x1 += x;
|
|
|
|
y1 += y;
|
|
|
|
x2 += x;
|
|
|
|
y2 += y;
|
|
|
|
x3 += x;
|
|
|
|
y3 += y;
|
|
|
|
x4 += x;
|
|
|
|
y4 += y;
|
|
|
|
}
|
|
|
|
|
|
|
|
double CubicBezierCurve::length() {
|
2023-01-15 22:34:02 +00:00
|
|
|
if (len < 0) {
|
2023-01-15 17:01:27 +00:00
|
|
|
// Euclidean distance approximation based on octagonal boundary
|
|
|
|
double dx = std::abs(x4 - x1);
|
|
|
|
double dy = std::abs(y4 - y1);
|
|
|
|
|
|
|
|
len = 0.41 * std::min(dx, dy) + 0.941246 * std::max(dx, dy);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
2023-02-05 00:43:57 +00:00
|
|
|
|
|
|
|
std::unique_ptr<Shape> CubicBezierCurve::clone() {
|
|
|
|
return std::make_unique<CubicBezierCurve>(x1, y1, x2, y2, x3, y3, x4, y4);
|
|
|
|
}
|
2023-02-05 17:39:02 +00:00
|
|
|
|
|
|
|
std::string CubicBezierCurve::type() {
|
|
|
|
return std::string("CubicBezierCurve");
|
|
|
|
}
|