I have a source component and a target component and I want to calculate the corner point where they will be connected. I am implementing a method that takes the source and target points and their orientations (either horizontal or vertical) as inputs. Here is the skeleton of the method:
private final static int HORIZONTAL = 0;
private final static int VERTICAL = 1;
private PointF computeCornerPoint(PointF sourcePoint, int sourceComponentOrientation, PointF targetPoint, int targetComponentOrientation) {
PointF cornerPoint = new PointF();
// TODO: Calculate the corner point here
return cornerPoint;
}
I have created a diagram that shows all the possible cases. In the diagram, the green points are the source and target points, and the red point is the corner point that I want to compute.
Also, note that this problem is symmetric, so the source and target components can be swapped, and the diagram I provided only shows one case.
In Android, the x-coordinate increases from left to right, and the y-coordinate increases from top to bottom.
Can you suggest an algorithm or a solution to compute the corner point for all these cases?
Additionally, please let me know if there is any information missing that would be helpful in solving this problem.
Horizontal/vertical isn’t enough – we need to know which side of the component the connection point is on. Also I’m not sure how you want to handle connection points on opposite sides.
For the cases you’ve shown, the answer will be either (source.x, target.y) or (target.x, source.y). If (for example) the source point is on the right side of its component, then (target.x, source.y) is vetoed if target.x < source.x or preferred if source.x < target.x; (source.x, target.y) is a middling option. The other seven cases (three source directions and four target directions) are symmetric. We demand an option that is not vetoed, and should choose one that is preferred if possible.
Let’s assume that (source.dx, source.dy) and (target.dx, target.dy) give the coordinates of their respective connection points relative to the center of the component (so using the usual mathematical coordinate system with positive dx pointing right and positive dy pointing up, (dx, dy) = (1, 0) is right, and (dx, dy) = (0, 1) is up, etc.). The generic condition for a component is, a point (x′, y′) is vetoed/preferred/middling if component.dx * (x′ - component.x) + component.dy * (y′ - component.y) is less than/greater than/equal to zero respectively. If we let “vetoed” be -2 points, preferred be 1 point, and middling be 0 points, we can add up the scores for both candidate points and take the max.