using System;
using System.Windows;

namespace Machinations
{
    public static class GeometryUtils
    {
        public static double PointDistance(Point p1, Point p2)
        {
            return Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
        }

        public static Vector Subtract(Point p1, Point p2)
        {
            return new Vector(p1.X - p2.X, p1.Y - p2.Y);
        }

        public static double Normalize(ref Vector vector)
        {
            var length = Length(vector);
            if (length != 0)
            {
                vector.X = vector.X / length;
                vector.Y = vector.Y / length;
            }
            return length;
        }

        public static double Length(Vector vector)
        {
            return Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y);
        }

        public static void ScaleBy(ref Vector vector, double d)
        {
            vector.X *= d;
            vector.Y *= d;
        }

        public static void IncrementBy(ref Vector incremented, Vector adding)
        {
            incremented.X += adding.X;
            incremented.Y += adding.Y;
        }

        public static double DotProduct(Vector first, Vector other)
        {
            return first.X * other.X + first.Y * other.Y;
        }

        public static double ClosestPointOnLine(Point lineStart, Vector lineDirection, double lineLength, Point point)
        {
            double v3bx = point.X - lineStart.X;
            double v3by = point.Y - lineStart.Y;
            double p = v3bx * lineDirection.X + v3by * lineDirection.Y;
            if (p < 0) p = 0;
            if (p > lineLength) p = lineLength;
            return p;
        }

        public static Point Clone(this Point point)
        {
            return new Point(point.X, point.Y);
        }

        public static bool IsBetween(double minX, double maxX, double minY, double maxY, Point p)
        {
            return p.X >= minX && p.X <= maxX && p.Y >= minY && p.Y <= maxY;
        }
    }

    
}
