From 9af250c7d6780a41dcaf536c05e3e1987a1bdcd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Chlumsk=C3=BD?= Date: Fri, 30 Oct 2020 10:02:40 +0100 Subject: [PATCH] Analytic length of quadratic segment --- core/edge-segments.cpp | 20 ++++++++++++++++++++ core/edge-segments.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/core/edge-segments.cpp b/core/edge-segments.cpp index 26c1b3f..72ef3ee 100644 --- a/core/edge-segments.cpp +++ b/core/edge-segments.cpp @@ -113,6 +113,26 @@ Vector2 CubicSegment::directionChange(double param) const { return mix((p[2]-p[1])-(p[1]-p[0]), (p[3]-p[2])-(p[2]-p[1]), param); } +double LinearSegment::length() const { + return (p[1]-p[0]).length(); +} + +double QuadraticSegment::length() const { + Vector2 ab = p[1]-p[0]; + Vector2 br = p[2]-p[1]-ab; + float abab = dotProduct(ab, ab); + float abbr = dotProduct(ab, br); + float brbr = dotProduct(br, br); + float abLen = sqrt(abab); + float brLen = sqrt(brbr); + float crs = crossProduct(ab, br); + float h = sqrt(abab+abbr+abbr+brbr); + return ( + brLen*((abbr+brbr)*h-abbr*abLen)+ + crs*crs*log((brLen*h+abbr+brbr)/(brLen*abLen+abbr)) + )/(brbr*brLen); +} + SignedDistance LinearSegment::signedDistance(Point2 origin, double ¶m) const { Vector2 aq = origin-p[0]; Vector2 ab = p[1]-p[0]; diff --git a/core/edge-segments.h b/core/edge-segments.h index 9ec3cbc..1c8fb59 100644 --- a/core/edge-segments.h +++ b/core/edge-segments.h @@ -58,6 +58,7 @@ public: Point2 point(double param) const; Vector2 direction(double param) const; Vector2 directionChange(double param) const; + double length() const; SignedDistance signedDistance(Point2 origin, double ¶m) const; int scanlineIntersections(double x[3], int dy[3], double y) const; void bound(double &l, double &b, double &r, double &t) const; @@ -80,6 +81,7 @@ public: Point2 point(double param) const; Vector2 direction(double param) const; Vector2 directionChange(double param) const; + double length() const; SignedDistance signedDistance(Point2 origin, double ¶m) const; int scanlineIntersections(double x[3], int dy[3], double y) const; void bound(double &l, double &b, double &r, double &t) const;