From e435d94e4e54fff2ddb2c6694138559fc6692f36 Mon Sep 17 00:00:00 2001 From: Haibo Date: Tue, 20 Nov 2018 15:00:01 +0800 Subject: [PATCH] add: Geometry::GetBoundingBox & Geometry::ComputeArea --- core/base/Geometry.cpp | 28 ++++++++++++++++++++++++++++ core/base/Geometry.h | 6 ++++++ core/base/Rect.hpp | 7 +++++++ 3 files changed, 41 insertions(+) diff --git a/core/base/Geometry.cpp b/core/base/Geometry.cpp index 2f52d850..f0f326a8 100644 --- a/core/base/Geometry.cpp +++ b/core/base/Geometry.cpp @@ -36,6 +36,20 @@ namespace easy2d { } + Rect Geometry::GetBoundingBox() + { + if (!geo_) + return Rect{}; + + D2D1_RECT_F rect; + // no matter it failed or not + geo_->GetBounds( + ConvertToD2DMatrix(GetTransformMatrix()), + &rect + ); + return rect; + } + float Geometry::GetLength() { float length = 0.f; @@ -71,6 +85,20 @@ namespace easy2d return false; } + float Geometry::ComputeArea() + { + if (!geo_) + return 0.f; + + float area = 0.f; + // no matter it failed or not + geo_->ComputeArea( + ConvertToD2DMatrix(GetTransformMatrix()), + &area + ); + return area; + } + bool Geometry::ContainsPoint(Point const & point) { if (!geo_) diff --git a/core/base/Geometry.h b/core/base/Geometry.h index fc277fbb..e35b2c41 100644 --- a/core/base/Geometry.h +++ b/core/base/Geometry.h @@ -46,6 +46,9 @@ namespace easy2d virtual ~Geometry(); + // 获取外切包围盒 + Rect GetBoundingBox(); + // 判断图形是否包含点 bool ContainsPoint( Point const& point @@ -66,6 +69,9 @@ namespace easy2d Point* tangent ); + // 计算面积 + float ComputeArea(); + protected: cpGeometry geo_; }; diff --git a/core/base/Rect.hpp b/core/base/Rect.hpp index 7ab9145b..51806d91 100644 --- a/core/base/Rect.hpp +++ b/core/base/Rect.hpp @@ -67,6 +67,13 @@ namespace easy2d , size(other.size.width, other.size.height) {} + Rect( + const D2D1_RECT_F& other + ) + : origin(other.left, other.top) + , size(other.right - other.left, other.bottom - other.top) + {} + Rect& operator= (const Rect& other) { origin = other.origin;