This source file includes following definitions.
- min4
- max4
- dot
- determinant
- isPointInTriangle
- boundingBox
- withinEpsilon
- isRectilinear
- containsPoint
- containsQuad
- rightMostCornerToVector
- intersectsRect
- lineIntersectsCircle
- intersectsCircle
- intersectsEllipse
- isCounterclockwise
#include "config.h"
#include "platform/geometry/FloatQuad.h"
#include <algorithm>
#include <limits>
using namespace std;
namespace WebCore {
static inline float min4(float a, float b, float c, float d)
{
return min(min(a, b), min(c, d));
}
static inline float max4(float a, float b, float c, float d)
{
return max(max(a, b), max(c, d));
}
inline float dot(const FloatSize& a, const FloatSize& b)
{
return a.width() * b.width() + a.height() * b.height();
}
inline float determinant(const FloatSize& a, const FloatSize& b)
{
return a.width() * b.height() - a.height() * b.width();
}
inline bool isPointInTriangle(const FloatPoint& p, const FloatPoint& t1, const FloatPoint& t2, const FloatPoint& t3)
{
FloatSize v0 = t3 - t1;
FloatSize v1 = t2 - t1;
FloatSize v2 = p - t1;
float dot00 = dot(v0, v0);
float dot01 = dot(v0, v1);
float dot02 = dot(v0, v2);
float dot11 = dot(v1, v1);
float dot12 = dot(v1, v2);
float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
return (u >= 0) && (v >= 0) && (u + v <= 1);
}
FloatRect FloatQuad::boundingBox() const
{
float left = min4(m_p1.x(), m_p2.x(), m_p3.x(), m_p4.x());
float top = min4(m_p1.y(), m_p2.y(), m_p3.y(), m_p4.y());
float right = max4(m_p1.x(), m_p2.x(), m_p3.x(), m_p4.x());
float bottom = max4(m_p1.y(), m_p2.y(), m_p3.y(), m_p4.y());
return FloatRect(left, top, right - left, bottom - top);
}
static inline bool withinEpsilon(float a, float b)
{
return fabs(a - b) < numeric_limits<float>::epsilon();
}
bool FloatQuad::isRectilinear() const
{
return (withinEpsilon(m_p1.x(), m_p2.x()) && withinEpsilon(m_p2.y(), m_p3.y()) && withinEpsilon(m_p3.x(), m_p4.x()) && withinEpsilon(m_p4.y(), m_p1.y()))
|| (withinEpsilon(m_p1.y(), m_p2.y()) && withinEpsilon(m_p2.x(), m_p3.x()) && withinEpsilon(m_p3.y(), m_p4.y()) && withinEpsilon(m_p4.x(), m_p1.x()));
}
bool FloatQuad::containsPoint(const FloatPoint& p) const
{
return isPointInTriangle(p, m_p1, m_p2, m_p3) || isPointInTriangle(p, m_p1, m_p3, m_p4);
}
bool FloatQuad::containsQuad(const FloatQuad& other) const
{
return containsPoint(other.p1()) && containsPoint(other.p2()) && containsPoint(other.p3()) && containsPoint(other.p4());
}
static inline FloatPoint rightMostCornerToVector(const FloatRect& rect, const FloatSize& vector)
{
FloatPoint point;
if (vector.width() >= 0)
point.setY(rect.maxY());
else
point.setY(rect.y());
if (vector.height() >= 0)
point.setX(rect.x());
else
point.setX(rect.maxX());
return point;
}
bool FloatQuad::intersectsRect(const FloatRect& rect) const
{
FloatSize v1, v2, v3, v4;
if (!isCounterclockwise()) {
v1 = m_p2 - m_p1;
v2 = m_p3 - m_p2;
v3 = m_p4 - m_p3;
v4 = m_p1 - m_p4;
} else {
v1 = m_p4 - m_p1;
v2 = m_p1 - m_p2;
v3 = m_p2 - m_p3;
v4 = m_p3 - m_p4;
}
FloatPoint p = rightMostCornerToVector(rect, v1);
if (determinant(v1, p - m_p1) < 0)
return false;
p = rightMostCornerToVector(rect, v2);
if (determinant(v2, p - m_p2) < 0)
return false;
p = rightMostCornerToVector(rect, v3);
if (determinant(v3, p - m_p3) < 0)
return false;
p = rightMostCornerToVector(rect, v4);
if (determinant(v4, p - m_p4) < 0)
return false;
return true;
}
static inline bool lineIntersectsCircle(const FloatPoint& center, float radius, const FloatPoint& p0, const FloatPoint& p1)
{
float x0 = p0.x() - center.x(), y0 = p0.y() - center.y();
float x1 = p1.x() - center.x(), y1 = p1.y() - center.y();
float radius2 = radius * radius;
if ((x0 * x0 + y0 * y0) <= radius2 || (x1 * x1 + y1 * y1) <= radius2)
return true;
if (p0 == p1)
return false;
float a = y0 - y1;
float b = x1 - x0;
float c = x0 * y1 - x1 * y0;
float distance2 = c * c / (a * a + b * b);
if (distance2 > radius2)
return false;
float x = - a * c / (a * a + b * b);
float y = - b * c / (a * a + b * b);
return (((x0 <= x && x <= x1) || (x0 >= x && x >= x1))
&& ((y0 <= y && y <= y1) || (y1 <= y && y <= y0)));
}
bool FloatQuad::intersectsCircle(const FloatPoint& center, float radius) const
{
return containsPoint(center)
|| lineIntersectsCircle(center, radius, m_p1, m_p2)
|| lineIntersectsCircle(center, radius, m_p2, m_p3)
|| lineIntersectsCircle(center, radius, m_p3, m_p4)
|| lineIntersectsCircle(center, radius, m_p4, m_p1);
}
bool FloatQuad::intersectsEllipse(const FloatPoint& center, const FloatSize& radii) const
{
FloatQuad transformedQuad(*this);
transformedQuad.move(-center.x(), -center.y());
transformedQuad.scale(radii.height(), radii.width());
FloatPoint originPoint;
return transformedQuad.intersectsCircle(originPoint, radii.height() * radii.width());
}
bool FloatQuad::isCounterclockwise() const
{
return determinant(m_p2 - m_p1, m_p3 - m_p2) < 0;
}
}