From f4900b8df237c0dd438a9210e8b1e8483fd3b2eb Mon Sep 17 00:00:00 2001 From: "yang.yongquan" <3395816735@qq.com> Date: Fri, 7 Oct 2022 10:20:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E7=B2=BE=E5=BA=A6?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=BE=85=E8=A7=A3=E5=86=B3=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArchitectureColoredPainting/CubicBezier.cpp | 38 ++++++++++---------- ArchitectureColoredPainting/CubicBezier.h | 12 +++---- ArchitectureColoredPainting/Line.cpp | 4 +-- ArchitectureColoredPainting/Line.h | 12 +++---- ArchitectureColoredPainting/ShortCutTree.cpp | 2 +- ArchitectureColoredPainting/ShortCutTree.h | 2 +- ArchitectureColoredPainting/StraightLine.cpp | 6 ++-- ArchitectureColoredPainting/StraightLine.h | 6 ++-- 8 files changed, 41 insertions(+), 41 deletions(-) diff --git a/ArchitectureColoredPainting/CubicBezier.cpp b/ArchitectureColoredPainting/CubicBezier.cpp index 515527c..99b3277 100644 --- a/ArchitectureColoredPainting/CubicBezier.cpp +++ b/ArchitectureColoredPainting/CubicBezier.cpp @@ -1,14 +1,14 @@ #include "CubicBezier.h" -float CubicBezier::getLineValueByT(float t, bool isY) { - vector p; +double CubicBezier::getLineValueByT(double t, bool isY) { + vector p; if (isY) p = vY; else p = vX; float pt = 1 - t; return pt * pt * pt * p[0] + 3 * t * pt * pt * p[1] + 3 * t * t * pt * p[2] + t * t * t * p[3]; } -Point CubicBezier::getPointByT(Point a, Point b, float t) { +Point CubicBezier::getPointByT(Point a, Point b, double t) { return { a.x * (1 - t) + b.x * t, a.y * (1 - t) + b.y * t }; } @@ -19,11 +19,11 @@ Point CubicBezier::calculateControlPoint(Point a, Point b) { }; } -void CubicBezier::findPointsOfDivison(vector &p, vector& res) { - float a = -3 * p[0] + 9 * p[1] - 9 * p[2] + 3 * p[3]; - float b = 6 * p[0] - 12 * p[1] + 6 * p[2]; - float c = -3 * p[0] + 3 * p[1]; - float deta = b * b - 4 * a * c, t = 0; +void CubicBezier::findPointsOfDivison(vector &p, vector& res) { + double a = -3 * p[0] + 9 * p[1] - 9 * p[2] + 3 * p[3]; + double b = 6 * p[0] - 12 * p[1] + 6 * p[2]; + double c = -3 * p[0] + 3 * p[1]; + double deta = b * b - 4 * a * c, t = 0; if (fabs(a) > eps) { if (deta < eps) return; deta = sqrt(deta); @@ -58,7 +58,7 @@ void CubicBezier::transformToCubic() { siz = 4; } -void CubicBezier::splitBezierCubic(float t, vector& res) { +void CubicBezier::splitBezierCubic(double t, vector& res) { LinePtr Lf(new CubicBezier()); float pt = 1 - t; Point pointE = { getLineValueByT(t, false), getLineValueByT(t, true) }; @@ -77,25 +77,25 @@ void CubicBezier::splitBezierCubic(float t, vector& res) { void CubicBezier::monotonization(vector & res) { transformToCubic(); - vector re; + vector re; re.clear(); findPointsOfDivison(vX, re); findPointsOfDivison(vY, re); sort(re.begin(), re.end()); - float lt = -1, x = 0, y = 0; - for (float &t : re) { + double lt = -1, x = 0, y = 0; + for (double&t : re) { if (fabs(t - lt) <= eps) continue; splitBezierCubic(t, res); lt = t; } } -float CubicBezier::findTByValue(float value, bool isY) { - float l = 0, r = 1, be = 0; +double CubicBezier::findTByValue(double value, bool isY) { + double l = 0, r = 1, be = 0; if (isY) be = vY[0]; else be = vX[0]; while (fabs(r-l) > eps) { - float mid = (l + r) / 2, midValue = getLineValueByT(mid, isY); + double mid = (l + r) / 2, midValue = getLineValueByT(mid, isY); if (fabs(midValue - be) > fabs(value - be)) { r = mid; } @@ -106,15 +106,15 @@ float CubicBezier::findTByValue(float value, bool isY) { return l; } -int CubicBezier::judgeBoundIntersection(float xy, float l, float r, bool isY) { - float be = *vX.begin(), en = *vX.rbegin(); +int CubicBezier::judgeBoundIntersection(double xy, double l, double r, bool isY) { + double be = *vX.begin(), en = *vX.rbegin(); if (isY) { be = *vY.begin(); en = *vY.rbegin(); } if ((be - xy) * (en - xy) > eps) return 0; - float t = findTByValue(xy, isY); - float value = getLineValueByT(t, !isY); + double t = findTByValue(xy, isY); + double value = getLineValueByT(t, !isY); if (l <= value && value <= r && fabs(t) > eps && fabs(1 - t) > eps) { return 1 + direction(isY); } diff --git a/ArchitectureColoredPainting/CubicBezier.h b/ArchitectureColoredPainting/CubicBezier.h index 4b6c02d..d2691f7 100644 --- a/ArchitectureColoredPainting/CubicBezier.h +++ b/ArchitectureColoredPainting/CubicBezier.h @@ -6,15 +6,15 @@ class CubicBezier : public Line { using Line::Line; private: - static Point getPointByT(Point a, Point b, float t); + static Point getPointByT(Point a, Point b, double t); static Point calculateControlPoint(Point a, Point b); - static void findPointsOfDivison(vector &p, vector& res); - void splitBezierCubic(float t, vector& res); + static void findPointsOfDivison(vector &p, vector& res); + void splitBezierCubic(double t, vector& res); public: - virtual float findTByValue(float value, bool isY); + virtual double findTByValue(double value, bool isY); virtual void monotonization(vector & res); - virtual float getLineValueByT(float t, bool isY); - virtual int judgeBoundIntersection(float xy, float l, float r, bool isY); + virtual double getLineValueByT(double t, bool isY); + virtual int judgeBoundIntersection(double xy, double l, double r, bool isY); void transformToCubic(); }; diff --git a/ArchitectureColoredPainting/Line.cpp b/ArchitectureColoredPainting/Line.cpp index 473becb..a8673a7 100644 --- a/ArchitectureColoredPainting/Line.cpp +++ b/ArchitectureColoredPainting/Line.cpp @@ -45,8 +45,8 @@ void Line::push_back(Point now) { } bool Line::isLineContained(QVector4D bound) { - float xMi = min(*vX.begin(), *vX.rbegin()), xMx = max(*vX.begin(), *vX.rbegin()); - float yMi = min(*vY.begin(), *vY.rbegin()), yMx = max(*vY.begin(), *vY.rbegin()); + double xMi = min(*vX.begin(), *vX.rbegin()), xMx = max(*vX.begin(), *vX.rbegin()); + double yMi = min(*vY.begin(), *vY.rbegin()), yMx = max(*vY.begin(), *vY.rbegin()); if (bound.x() <= xMi && xMx <= bound.z() && bound.y() <= yMi && yMx <= bound.w()) return true; else return false; diff --git a/ArchitectureColoredPainting/Line.h b/ArchitectureColoredPainting/Line.h index d20e66c..db8cf49 100644 --- a/ArchitectureColoredPainting/Line.h +++ b/ArchitectureColoredPainting/Line.h @@ -16,12 +16,12 @@ using std::shared_ptr; using std::make_shared; using std::swap; -const float eps = 1e-6; +const double eps = 1e-5; struct Point { - float x, y; + double x, y; Point operator - (const Point a) { return { x - a.x, y - a.y }; } - float operator * (const Point a) { return x * a.y - y * a.x; } + double operator * (const Point a) { return x * a.y - y * a.x; } bool operator== (const Point& a) const { return fabs(x - a.x) <= eps && fabs(y - a.y) <= eps; } @@ -36,7 +36,7 @@ struct Point { class Line { protected: - vector vX, vY; + vector vX, vY; int siz; public: typedef shared_ptr LinePtr; @@ -47,9 +47,9 @@ public: Point getBegin(); Point getEnd(); int direction(bool isY); - virtual float getLineValueByT(float t, bool isY) = 0; + virtual double getLineValueByT(double t, bool isY) = 0; virtual void monotonization(vector & res) = 0; - virtual int judgeBoundIntersection(float xy, float l, float r, bool isY) = 0; + virtual int judgeBoundIntersection(double xy, double l, double r, bool isY) = 0; virtual bool judgeIntersection(QVector4D bound); bool isLineContained(QVector4D bound); Point operator[](int index) const; diff --git a/ArchitectureColoredPainting/ShortCutTree.cpp b/ArchitectureColoredPainting/ShortCutTree.cpp index 838c3f5..7ea773b 100644 --- a/ArchitectureColoredPainting/ShortCutTree.cpp +++ b/ArchitectureColoredPainting/ShortCutTree.cpp @@ -80,7 +80,7 @@ void ShortCutTree::generateShortCutsegmentement(ShortCutNode& nowTreeNode) { } } -bool ShortCutTree::handleShortCutNode(ShortCutNode& fa, ShortCutNode& nowTreeNode, float yValue, vector& v, int& sumIncrement) { +bool ShortCutTree::handleShortCutNode(ShortCutNode& fa, ShortCutNode& nowTreeNode, double yValue, vector& v, int& sumIncrement) { nowTreeNode.windingIncrement = sumIncrement; for (int & lineIndex : fa.lineSet) { int type = allLine[lineIndex]->judgeBoundIntersection(yValue, nowTreeNode.bound.x(), nowTreeNode.bound.z(), true); diff --git a/ArchitectureColoredPainting/ShortCutTree.h b/ArchitectureColoredPainting/ShortCutTree.h index 87129e2..21fcba7 100644 --- a/ArchitectureColoredPainting/ShortCutTree.h +++ b/ArchitectureColoredPainting/ShortCutTree.h @@ -38,7 +38,7 @@ private: int getPointIndex(Point nowPoint); void generateShortCutsegmentement(ShortCutNode& nowTreeNode); - bool handleShortCutNode(ShortCutNode& fa, ShortCutNode& nowTreeNode, float yValue, vector& v, int& sumIncrement); + bool handleShortCutNode(ShortCutNode& fa, ShortCutNode& nowTreeNode, double yValue, vector& v, int& sumIncrement); void spliteToShortCutTree(); static void monotonization(vector& inL, vector &outL); bool isLineEqual(PointIndexVector& a, PointIndexVector& b) const; diff --git a/ArchitectureColoredPainting/StraightLine.cpp b/ArchitectureColoredPainting/StraightLine.cpp index ec35127..e738bd1 100644 --- a/ArchitectureColoredPainting/StraightLine.cpp +++ b/ArchitectureColoredPainting/StraightLine.cpp @@ -1,6 +1,6 @@ #include "StraightLine.h" -float StraightLine::getLineValueByT(float t, bool isY) { +double StraightLine::getLineValueByT(double t, bool isY) { float pBe, pEn; if (isY) { pBe = *vY.begin(); @@ -13,7 +13,7 @@ float StraightLine::getLineValueByT(float t, bool isY) { return t * (pEn - pBe) + pBe; } -float StraightLine::findTByValue(float value, bool isY) { +double StraightLine::findTByValue(double value, bool isY) { Point be = getPointByIndex(0), en = getPointByIndex(1); if (!isY) { if(fabs(be.x - en.x) <= eps) return 0; @@ -25,7 +25,7 @@ float StraightLine::findTByValue(float value, bool isY) { } } -int StraightLine::judgeBoundIntersection(float xy, float l, float r, bool isY) { +int StraightLine::judgeBoundIntersection(double xy, double l, double r, bool isY) { Point be = getBegin(), en = getEnd(); if (isY) { swap(be.x, be.y); diff --git a/ArchitectureColoredPainting/StraightLine.h b/ArchitectureColoredPainting/StraightLine.h index ce45986..88fb09b 100644 --- a/ArchitectureColoredPainting/StraightLine.h +++ b/ArchitectureColoredPainting/StraightLine.h @@ -3,8 +3,8 @@ class StraightLine : public Line { using Line::Line; - virtual float getLineValueByT(float t, bool isY); + virtual double getLineValueByT(double t, bool isY); virtual void monotonization(vector & res) {}; - virtual float findTByValue(float value, bool isY); - virtual int judgeBoundIntersection(float xy, float l, float r, bool isY); + virtual double findTByValue(double value, bool isY); + virtual int judgeBoundIntersection(double xy, double l, double r, bool isY); }; \ No newline at end of file