src/Azos/Geometry/CartesianUtils.cs
/// <summary>
/// Returns a point of intersection between a ray cast from the center of a rectangle
///  under certain polar coordinate angle and a rectangle side
/// </summary>
Returns a point of intersection between a ray cast from the center of a rectangle
under certain polar coordinate angle and a rectangle side
Returns a point of intersection between a ray cast from the center of a rectangle under certain polar coordinate angle and a rectangle side
public static Point FindRayFromRectangleCenterSideIntersection(Rectangle rect, double theta)
    {
        Point center = new Point(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
        double rayLength = rect.Width > rect.Height ? rect.Width : rect.Height; 
        if (rayLength < 100) rayLength = 100; 
        PolarPoint rayEnd = new PolarPoint(rayLength, theta);
        double k = (rayEnd.Point.X!=0)? ((double)rayEnd.Point.Y) / ((double)rayEnd.Point.X) : 0; 
        int x, y;
        List<Point> lst = new List<Point>();
        x = center.X + ((k != 0) ? (int)((rect.Top - center.Y) / k) : 0);
        y = rect.Top;
        if ((x >= rect.Left) &&
            (x <= rect.Right))
           lst.Add(new Point(x, y));
        x = center.X + ((k != 0) ? (int)((rect.Bottom - center.Y) / k) : 0);
        y = rect.Bottom;
        if ((x >= rect.Left) &&
            (x <= rect.Right))
          lst.Add(new Point(x, y));
        x = rect.Right;
        y = center.Y + (int)(k * (rect.Right - center.X));
        if ((y >= rect.Top) &&
            (y <= rect.Bottom))
          lst.Add(new Point(x, y));
        x = rect.Left;
        y = center.Y + (int)(k * (rect.Left - center.X));
        if ((y >= rect.Top) &&
            (y <= rect.Bottom))
          lst.Add(new Point(x, y));
        Point minPoint = new Point(int.MaxValue, int.MaxValue);
        int minDistance = int.MaxValue;
        Point re = rayEnd.Point; 
        re.Offset(center.X, center.Y); 
        foreach (Point p in lst) 
        {
           int dst = Distance(p, re);
           if (dst < minDistance)
           {
             minPoint = p;
             minDistance = dst;
           }
        }
        return minPoint;
      }
public static Point FindRayFromRectangleCenterSideIntersection ( Rectangle rect , double theta ) { Point center = new Point ( rect . Left + rect . Width / 2 , rect . Top + rect . Height / 2 ) ; double rayLength = rect . Width > rect . Height ? rect . Width : rect . Height ; if ( rayLength < 100 ) rayLength = 100 ; PolarPoint rayEnd = new PolarPoint ( rayLength , theta ) ; double k = ( rayEnd . Point . X != 0 ) ? ( ( double ) rayEnd . Point . Y ) / ( ( double ) rayEnd . Point . X ) : 0 ; int x , y ; List < Point > lst = new List < Point > ( ) ; x = center . X + ( ( k != 0 ) ? ( int ) ( ( rect . Top - center . Y ) / k ) : 0 ) ; y = rect . Top ; if ( ( x >= rect . Left ) && ( x <= rect . Right ) ) lst . Add ( new Point ( x , y ) ) ; x = center . X + ( ( k != 0 ) ? ( int ) ( ( rect . Bottom - center . Y ) / k ) : 0 ) ; y = rect . Bottom ; if ( ( x >= rect . Left ) && ( x <= rect . Right ) ) lst . Add ( new Point ( x , y ) ) ; x = rect . Right ; y = center . Y + ( int ) ( k * ( rect . Right - center . X ) ) ; if ( ( y >= rect . Top ) && ( y <= rect . Bottom ) ) lst . Add ( new Point ( x , y ) ) ; x = rect . Left ; y = center . Y + ( int ) ( k * ( rect . Left - center . X ) ) ; if ( ( y >= rect . Top ) && ( y <= rect . Bottom ) ) lst . Add ( new Point ( x , y ) ) ; Point minPoint = new Point ( int . MaxValue , int . MaxValue ) ; int minDistance = int . MaxValue ; Point re = rayEnd . Point ; re . Offset ( center . X , center . Y ) ; foreach ( Point p in lst ) { int dst = Distance ( p , re ) ; if ( dst < minDistance ) { minPoint = p ; minDistance = dst ; } } return minPoint ; }
Returns a point of intersection between a ray cast from the center of a rectangle
under certain polar coordinate angle and a rectangle side
Returns a point of intersection between a ray cast from the center of a rectangle under certain polar coordinate angle and a rectangle side
//make ray "infinite" in comparison to rect //safeguard //create ray "end" point //get line incline  aka. y = kx //north //south //east //west //rayEnd is relative to absolute 0,0 // need to make relative to rectangle center //find closest point
rect
Rectangle
theta
double