Calculate a point along the line A-B at a given distance from A

前端 未结 6 1376
情歌与酒
情歌与酒 2020-11-29 08:54

I\'m going quite mad trying to calculate the point along the given line A-B, at a given distance from A, so that I can "draw" the line between two given points. It

6条回答
  •  迷失自我
    2020-11-29 09:13

        private static Point CalculatePoint(Point a, Point b, int distance)
        {
    
            // a. calculate the vector from o to g:
            double vectorX = b.X - a.X;
            double vectorY = b.Y - a.Y;
    
            // b. calculate the proportion of hypotenuse
            double factor = distance / Math.Sqrt(vectorX * vectorX + vectorY * vectorY);
    
            // c. factor the lengths
            vectorX *= factor;
            vectorY *= factor;
    
            // d. calculate and Draw the new vector,
            return new Point((int)(a.X + vectorX), (int)(a.Y + vectorY));
        }
    

提交回复
热议问题