#include "config.h"
#include "FloatPoint.h"
#include "AffineTransform.h"
#include "FloatConversion.h"
#include "IntPoint.h"
#include "LayoutPoint.h"
#include "LayoutSize.h"
#include "TransformationMatrix.h"
#include <limits>
#include <math.h>
namespace WebCore {
FloatPoint::FloatPoint(const IntPoint& p) : m_x(p.x()), m_y(p.y())
{
}
FloatPoint::FloatPoint(const LayoutPoint& p) : m_x(p.x()), m_y(p.y())
{
}
void FloatPoint::normalize()
{
float tempLength = length();
if (tempLength) {
m_x /= tempLength;
m_y /= tempLength;
}
}
float FloatPoint::slopeAngleRadians() const
{
return atan2f(m_y, m_x);
}
float FloatPoint::length() const
{
return sqrtf(lengthSquared());
}
void FloatPoint::move(const LayoutSize& size)
{
m_x += size.width();
m_y += size.height();
}
void FloatPoint::moveBy(const LayoutPoint& point)
{
m_x += point.x();
m_y += point.y();
}
FloatPoint FloatPoint::matrixTransform(const AffineTransform& transform) const
{
double newX, newY;
transform.map(static_cast<double>(m_x), static_cast<double>(m_y), newX, newY);
return narrowPrecision(newX, newY);
}
FloatPoint FloatPoint::matrixTransform(const TransformationMatrix& transform) const
{
double newX, newY;
transform.map(static_cast<double>(m_x), static_cast<double>(m_y), newX, newY);
return narrowPrecision(newX, newY);
}
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();
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;
}
intersection.setX((dOffset - pOffset) / (pSlope - dSlope));
intersection.setY(pSlope * intersection.x() + pOffset);
return true;
}
}