Example #1
0
        /// <summary>
        /// Vrátí bod přímce, který od daného bodu na přímce vzdálenou o danou velikost
        /// </summary>
        /// <param name="line">přímka</param>
        /// <param name="origin">počáteční bod</param>
        /// <param name="distance">vzdálenost mezi body</param>
        /// <returns></returns>
        public static Point getPointOnLineInDistance(Line line, Point origin, double distance)
        {
            Point endPoint = new Point();

            //distance^2 = (x1-x2)^2 + (y1-y2)^2
            Vector<double> leftSide = Vector<double>.Build.Dense(new double[] { 0, 0, distance * distance }); // [0]x^2 + [1]x + [2]
            Vector<double> xPow = Vector<double>.Build.Dense(new double[] { 1, -2 * origin.X, origin.X * origin.X }); // [0]x^2 + [1]x + [2]
            Vector<double> yPow = Vector<double>.Build.Dense(new double[] { line.k * line.k, 2 * (line.q - origin.Y) * line.k, (line.q - origin.Y) * (line.q - origin.Y) }); // [0]x^2 + [1]x + [2]
            Vector<double> rightSide = xPow + yPow;
            Vector<double> quadraticEqutation = rightSide - leftSide;
            Tuple<Complex, Complex> roots = MathNet.Numerics.FindRoots.Quadratic(quadraticEqutation.ElementAt(2), quadraticEqutation.ElementAt(1), quadraticEqutation.ElementAt(0));

            //výběr toho bodu, který je vzdálenější od středu
            endPoint.X = roots.Item1.Real;
            if (line.vertical)
            {
                endPoint.Y = origin.Y + Math.Sign(origin.Y) * distance;
            }
            else
            {
                Point endPointTemp = new Point(roots.Item2.Real, 0);
                Point trueOrigin = new Point(0, 0);
                endPoint.Y = line.getY(roots.Item1.Real);
                endPointTemp.Y = line.getY(roots.Item2.Real);

                if (getDistance(trueOrigin, endPoint) < getDistance(trueOrigin, endPointTemp))
                {
                    endPoint = endPointTemp;
                }
            }

            return endPoint;
        }