diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-09-21 23:40:30 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-12-18 17:29:36 +0800 |
| commit | b89a4783e033db1530322a35a1674630c413e847 (patch) | |
| tree | 20d297c70c0047e65a707f41bfbc96318e1bcb1f /src/platform/graphics/Geometry.cpp | |
| parent | 6bbadfc6b1eaf14f68a27cb8e378f5e09ab38de6 (diff) | |
| download | cru-b89a4783e033db1530322a35a1674630c413e847.tar.gz cru-b89a4783e033db1530322a35a1674630c413e847.tar.bz2 cru-b89a4783e033db1530322a35a1674630c413e847.zip | |
HALF WORK!
Diffstat (limited to 'src/platform/graphics/Geometry.cpp')
| -rw-r--r-- | src/platform/graphics/Geometry.cpp | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/platform/graphics/Geometry.cpp b/src/platform/graphics/Geometry.cpp index 859a7bd6..896b324f 100644 --- a/src/platform/graphics/Geometry.cpp +++ b/src/platform/graphics/Geometry.cpp @@ -1,9 +1,11 @@ #include "cru/platform/graphics/Geometry.h" - #include "cru/base/StringUtil.h" +#include "cru/platform/GraphicsBase.h" +#include "cru/platform/Matrix.h" #include "cru/platform/graphics/Factory.h" #include <cmath> +#include <numbers> #include <unordered_set> namespace cru::platform::graphics { @@ -19,6 +21,40 @@ std::unique_ptr<IGeometry> IGeometry::CreateStrokeGeometry( "not supported on this platform."); } +IGeometryBuilder::ArcInfo IGeometryBuilder::CalculateArcInfo( + const Point& start_point, const Point& radius, float angle, + bool is_large_arc, bool is_clockwise, const Point& end_point) { + auto matrix = + Matrix::Rotation(-angle) * Matrix::Scale(1 / radius.x, 1 / radius.y); + auto s1 = matrix.TransformPoint(start_point), + s2 = matrix.TransformPoint(end_point); + if (!is_clockwise) { + using std::swap; + swap(s1, s2); + } + + auto mid = (s1 + s2) / 2; + auto d = s1.Distance(s2) / 2; + auto dc = std::sqrt(1 - d * d); + auto a = std::atan2(s1.x - s2.x, s2.y - s1.y); + Point center(mid.x - dc * std::cos(a), mid.y - dc * std::sin(a)); + + if (std::abs(center.x) < 0.000001) { + center.x = 0.f; + } + if (std::abs(center.y) < 0.000001) { + center.y = 0.f; + } + + auto start_angle = std::atan2(s1.y - center.y, s1.x - center.x); + auto end_angle = std::atan2(s2.y - center.y, s2.x - center.x); + if (end_angle < 0) { + end_angle += std::numbers::pi_v<float> * 2; + } + + return {matrix.Inverted()->TransformPoint(center), start_angle, end_angle}; +} + void IGeometryBuilder::RelativeMoveTo(const Point& offset) { MoveTo(GetCurrentPosition() + offset); } @@ -233,9 +269,9 @@ void IGeometryBuilder::ParseAndApplySvgPathData(std::string_view path_d) { ++position; } - auto result = cru::string::ParseToNumber<float>( - path_d.substr(position), cru::string::ParseToNumberFlags::AllowTrailingJunk); + path_d.substr(position), + cru::string::ParseToNumberFlags::AllowTrailingJunk); if (!result.valid) throw Exception("Invalid svg path data number."); |
