summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/FloatPoint.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/WebCore/platform/graphics/FloatPoint.cpp')
-rw-r--r--Source/WebCore/platform/graphics/FloatPoint.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/Source/WebCore/platform/graphics/FloatPoint.cpp b/Source/WebCore/platform/graphics/FloatPoint.cpp
index 226ae71..abe9b86 100644
--- a/Source/WebCore/platform/graphics/FloatPoint.cpp
+++ b/Source/WebCore/platform/graphics/FloatPoint.cpp
@@ -31,6 +31,7 @@
#include "TransformationMatrix.h"
#include "FloatConversion.h"
#include "IntPoint.h"
+#include <limits>
#include <math.h>
namespace WebCore {
@@ -73,4 +74,43 @@ FloatPoint FloatPoint::narrowPrecision(double x, double y)
return FloatPoint(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y));
}
+float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c)
+{
+ if (p2.x() == p1.x())
+ return std::numeric_limits<float>::infinity();
+
+ // y = mx + c
+ float slope = (p2.y() - p1.y()) / (p2.x() - p1.x());
+ c = p1.y() - slope * p1.x();
+ return slope;
+}
+
+bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection)
+{
+ float pOffset = 0;
+ float pSlope = findSlope(p1, p2, pOffset);
+
+ float dOffset = 0;
+ float dSlope = findSlope(d1, d2, dOffset);
+
+ if (dSlope == pSlope)
+ return false;
+
+ if (pSlope == std::numeric_limits<float>::infinity()) {
+ intersection.setX(p1.x());
+ intersection.setY(dSlope * intersection.x() + dOffset);
+ return true;
+ }
+ if (dSlope == std::numeric_limits<float>::infinity()) {
+ intersection.setX(d1.x());
+ intersection.setY(pSlope * intersection.x() + pOffset);
+ return true;
+ }
+
+ // Find x at intersection, where ys overlap; x = (c' - c) / (m - m')
+ intersection.setX((dOffset - pOffset) / (pSlope - dSlope));
+ intersection.setY(pSlope * intersection.x() + pOffset);
+ return true;
+}
+
}