I tried to wrap my head around this all day ...
In fact, I have the coordinates of two points, which will always be inside a rectangle. I also know the position of the corners of a rectangle. These two entry points are set at runtime.
I need an algorithm to find 2 points where the bisector line formed by the line segment between these points intersects this rectangle.

Some details:
In the image above, A and B are set by their coordinates: A (x1, y1) and B (x2, y2). Basically, I need to find the position of C and D. Red X is the center of segment AB. This point (let it be the center) should be on the CD line.
What I've done:
Knowing that the center and the slope of the CD gave me the CD equation, I tried to find a solution by trying the position of the points on all 4 borders of a rectangle. However, for some reason this does not work: every time I have a solution, say, for C, D is built externally or vice versa.
Here are the equations that I use:
Based on this, I could also get another segment (say, I found C and know the center), but the geometry could not find a continuation of this segment until it intersects the other side of the rectangular box.
Updated to include code snippet
(see comments in the main function)
typedef struct { double x; double y; } Point; Point calculate_center(Point p1, Point p2) { Point point; point.x = (p1.x+p2.x)/2; point.y = (p1.y+p2.y)/2; return point; } double calculate_pslope(Point p1, Point p2) { double dy = p1.y - p2.y; double dx = p1.x - p2.x; double slope = dy/dx; // this is p1 <-> p2 slope return -1/slope; } int calculate_y_knowing_x(double pslope, Point center, double x, Point *point) { double min= 0.00; double max= 512.00; double y = (pslope * (x - center.x)) + center.y; if(y >= min && y <= max) { point->x = corner; point->y = y; printf("++> found Y for X, point is P(%f, %f)\n", point->x, point->y); return 1; } return 0; } int calculate_x_knowing_y(double pslope, Point center, double y, Point *point) { double min= 0.00; double max= 512.00; double x = (y - center.y + pslope*center.x)/pslope; if(x >= min && x <= max) { point->x = x; point->y = y; printf("++> found X for Y, point is: P(%f, %f)\n", point->x, point->y); return 1; } return 0; } int main(int argc, char **argv) { Point A, B; // parse argv and define A and B // this code is omitted here, let assume: // Ax = 175.00; // Ay = 420.00; // Bx = 316.00; // By = 62.00; Point C; Point D; Point center; double pslope; center = calculate_center(A, B); pslope = calculate_pslope(A, B); // Here where the fun happens: // I'll need to find the right succession of calls to calculate_*_knowing_* // for 4 cases: x=0, X=512 #=> call calculate_y_knowing_x // y=0, y=512 #=> call calculate_x_knowing_y // and do this 2 times for both C and D points. // Also, if point C is found, point D should not be on the same side (thus C != D) // for the given A and B points the succession is: calculate_y_knowing_x(pslope, center, 0.00, C); calculate_y_knowing_x(pslope, center, 512.00, D); // will yield: C(0.00, 144.308659), D(512.00, 345.962291) // But if A(350.00, 314.00) and B(106.00, 109.00) // the succesion should be: // calculate_y_knowing_x(pslope, center, 0.00, C); // calculate_x_knowing_y(pslope, center, 512.00, D); // to yield C(0.00, 482.875610) and D(405.694672, 0.00) return 0; }
This is the C code.
Notes:
- The image was drawn by hand.
- The coordinate system is rotated 90 ° counterclockwise, but should not affect the decision.
- I am looking for an algorithm in C, but I can read other programming languages
- This is a 2D problem