<commit_before>#include "geometry.h"

qreal const M_PI = 3.14159265358979323846;  // There's no such thing in math.h in C++11

using namespace mathUtils;

bool Geometry::eq(QPointF const &point1, QPointF const &point2, qreal eps)
{
	return Math::eq(point1.x(), point2.x(), eps) && Math::eq(point1.y(), point2.y(), eps);
}

qreal Geometry::scalarProduct(QVector2D const &vector1, QVector2D const &vector2)
{
	return vector1.x() * vector2.x() + vector1.y() * vector2.y();
}

qreal Geometry::vectorProduct(QVector2D const &vector1, QVector2D const &vector2)
{
	return vector1.x() * vector2.y() - vector1.y() * vector2.x();
}

QVector2D Geometry::projection(QVector2D const &projected, QVector2D const &target)
{
	QVector2D const normalizedTarget = target.normalized();
	// Scalar product is a projection lenght
	return normalizedTarget * scalarProduct(normalizedTarget, projected);
}

QPointF Geometry::normalPoint(QLineF const &line, QPointF const &point)
{
	qreal const x1 = line.p1().x();
	qreal const y1 = line.p1().y();
	qreal const x2 = line.p2().x();
	qreal const y2 = line.p2().y();
	qreal const x3 = point.x();
	qreal const y3 = point.y();

	if (x1 == x2) {
		return QPointF(x2, y3);
	}

	qreal const x = (x1 * Math::sqr(y2 - y1) + x3 * Math::sqr(x2 - x1) + (x2 - x1) * (y2 - y1) * (y3 - y1))
			/ (Math::sqr(y2 - y1) + Math::sqr(x2 - x1));
	qreal const y = ((y2 - y1) * (x - x1) / (x2 - x1)) + y1;

	return QPointF(x, y);
}

QLineF Geometry::normalLine(QLineF const &line, QPointF const &point)
{
	return QLineF(point, normalPoint(line, point));
}

qreal Geometry::distance(QLineF const &line, QPointF const &point)
{
	return normalLine(line, point).length();
}

qreal Geometry::distance(QPointF const &point1, QPointF const &point2)
{
	return sqrt(Math::sqr(point1.x() - point2.x()) + Math::sqr(point1.y() - point2.y()));
}

bool Geometry::intersects(QLineF const &line, QPainterPath const &path)
{
	QPainterPath linePath(line.p1());
	linePath.lineTo(line.p2());
	return path.intersects(linePath);
}

QVector2D Geometry::directionVector(qreal angleInDegrees)
{
	return directionVectorRad(angleInDegrees * M_PI / 180);
}

QVector2D Geometry::directionVectorRad(qreal angleInRadians)
{
	return QVector2D(cos(angleInRadians), sin(angleInRadians));
}

qreal Geometry::tangentLineAt(QPainterPath const &path, QPointF const &point)
{
	qreal const percentage = percentageAt(path, point);
	return path.angleAtPercent(percentage);
}

qreal Geometry::percentageAt(QPainterPath const &path, QPointF const &point)
{
	// Approximating percentage iteratively. First iterating by delta=1 percent increment, searching for
	// the closest point on path. Then repeating with found closest percentage value but with delta=0.1 and so on
	int const approximationLevel = 4;<commit_msg>linux compiler does not think that M_PI does not exist<commit_after>#include "geometry.h"

qreal const pi = 3.14159265358979323846;  // There's no such thing in math.h in C++11

using namespace mathUtils;

bool Geometry::eq(QPointF const &point1, QPointF const &point2, qreal eps)
{
	return Math::eq(point1.x(), point2.x(), eps) && Math::eq(point1.y(), point2.y(), eps);
}

qreal Geometry::scalarProduct(QVector2D const &vector1, QVector2D const &vector2)
{
	return vector1.x() * vector2.x() + vector1.y() * vector2.y();
}

qreal Geometry::vectorProduct(QVector2D const &vector1, QVector2D const &vector2)
{
	return vector1.x() * vector2.y() - vector1.y() * vector2.x();
}

QVector2D Geometry::projection(QVector2D const &projected, QVector2D const &target)
{
	QVector2D const normalizedTarget = target.normalized();
	// Scalar product is a projection lenght
	return normalizedTarget * scalarProduct(normalizedTarget, projected);
}

QPointF Geometry::normalPoint(QLineF const &line, QPointF const &point)
{
	qreal const x1 = line.p1().x();
	qreal const y1 = line.p1().y();
	qreal const x2 = line.p2().x();
	qreal const y2 = line.p2().y();
	qreal const x3 = point.x();
	qreal const y3 = point.y();

	if (x1 == x2) {
		return QPointF(x2, y3);
	}

	qreal const x = (x1 * Math::sqr(y2 - y1) + x3 * Math::sqr(x2 - x1) + (x2 - x1) * (y2 - y1) * (y3 - y1))
			/ (Math::sqr(y2 - y1) + Math::sqr(x2 - x1));
	qreal const y = ((y2 - y1) * (x - x1) / (x2 - x1)) + y1;

	return QPointF(x, y);
}

QLineF Geometry::normalLine(QLineF const &line, QPointF const &point)
{
	return QLineF(point, normalPoint(line, point));
}

qreal Geometry::distance(QLineF const &line, QPointF const &point)
{
	return normalLine(line, point).length();
}

qreal Geometry::distance(QPointF const &point1, QPointF const &point2)
{
	return sqrt(Math::sqr(point1.x() - point2.x()) + Math::sqr(point1.y() - point2.y()));
}

bool Geometry::intersects(QLineF const &line, QPainterPath const &path)
{
	QPainterPath linePath(line.p1());
	linePath.lineTo(line.p2());
	return path.intersects(linePath);
}

QVector2D Geometry::directionVector(qreal angleInDegrees)
{
	return directionVectorRad(angleInDegrees * pi / 180);
}

QVector2D Geometry::directionVectorRad(qreal angleInRadians)
{
	return QVector2D(cos(angleInRadians), sin(angleInRadians));
}

qreal Geometry::tangentLineAt(QPainterPath const &path, QPointF const &point)
{
	qreal const percentage = percentageAt(path, point);
	return path.angleAtPercent(percentage);
}

qreal Geometry::percentageAt(QPainterPath const &path, QPointF const &point)
{
	// Approximating percentage iteratively. First iterating by delta=1 percent increment, searching for
	// the closest point on path. Then repeating with found closest percentage value but with delta=0.1 and so on
	int const approximationLevel = 4;
{'fraction_non_alphanumeric': 0.14518730955368345, 'fraction_numerical': 0.03692417996056641, 'mean_word_length': 5.091703056768559, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6935285', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2056, 'n_words': 526}