1

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.

enter image description here

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.

Abdo21
  • 498
  • 4
  • 14
  • 1
    @m0skit0 Thank you for your response. This is not a homework question. I am designing a circuit drawing tool for Android, and I have been stuck on this point. I have tried to come up with a solution on my own, but I am having difficulty figuring out the best approach. Any help or suggestions would be greatly appreciated. Thank you. – Abdo21 Feb 19 '23 at 23:11
  • @Abdo21 Please review "the intersection of two lines" and realize that the "circuits" are actually rectangles. Otherwise show code of what you have tried. Do start with the simplest cases first before tackling the others. – Morrison Chang Feb 19 '23 at 23:24
  • 1
    @MorrisonChang Thank you for your response. I appreciate your suggestion, but I don't think this problem is the same as the problem of the intersection of two lines. In this case, we have two points (the source and target) and need to compute the corner point based on the orientation of the components. The orientation of the component determines which side of the rectangle we need to connect. Unfortunately, I have not been able to come up with a solution on my own. Any help or suggestions would be greatly appreciated. Thank you. – Abdo21 Feb 19 '23 at 23:35
  • The first row is a line intersection problem. The second and third rows are rotating the projection rectangles (not the circuit image) where the intersection occurs with additional rules like not going through the circuit. – Morrison Chang Feb 19 '23 at 23:48
  • @MorrisonChang I agree that I could use brute force to solve this problem with if-else statements for each case, but I was hoping to find a more optimal solution. As I am designing a circuit drawing tool for Android, it's important to me that the solution is efficient. Thank you. – Abdo21 Feb 19 '23 at 23:58
  • If you are doing every second fine, but in that case the connections should be cached/part of your display pipeline. If you are trying to optimize hundreds of "circuits" in various combinations, I'm not sure if Android is an appropriate platform. – Morrison Chang Feb 20 '23 at 00:16
  • @MorrisonChang The circuits that I intend to draw are relatively small and not very complex, I will try the brute force method and see if it performs well. Thank you again for your help! – Abdo21 Feb 20 '23 at 00:31
  • I think there'll be cases where there's no good solution. For example, what if the components are facing in opposite directions, with the connection points overlapping? – Dawood ibn Kareem Feb 20 '23 at 00:44

1 Answers1

1

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.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • Thank you for taking the time to provide this information. You're right, there are a few missing cases that need to be considered I will take this approach into consideration when working on the solution for this problem. Thank you. – Abdo21 Feb 20 '23 at 02:17