I am making something in Java (particularly Android) that displays something like the following:
The user can drag the corners of the box, and as expected, the dashed lines should move accordingly, always having 3 horizontal lines (one half way, one 25%, and one 75% between the top and the bottom) and 3 vertical lines. However, if either the left or right edge is infinity (which it is when the user first sees the screen), it looks something like this:
How can I change my code so that the infinite slope edges are accounted for and still show the desired screen? Here is my code for detecting the positions of where the dashed lines should be, their slopes, and finally them drawn to screen.
//get edge slopes to calculate dashed lines
//c1X is the top left corner X position, c2X is for the top right corner, etc.
float topLineSlope = (c2Y - c1Y)/(c2X - c1X);
float rightLineSlope = (c3Y - c2Y)/(c3X - c2X);
float bottomLineSlope = (c4Y - c3Y)/(c4X - c3X);
float leftLineSlope = (c1Y - c4Y)/(c1X - c4X);
//b in y=mx+b
float topLineB = c1Y - (topLineSlope * c1X);
float rightLineB = c2Y - (rightLineSlope * c2X);
float bottomLineB = c3Y - (bottomLineSlope * c3X);
float leftLineB = c4Y - (leftLineSlope * c4X);
//final dashed line coordinates
float topLineMiddleX = (c1X + c2X) / 2.0f;
float topLineMiddleY = topLineSlope * topLineMiddleX + topLineB;
float bottomLineMiddleX = (c3X + c4X) / 2.0f;
float bottomLineMiddleY = bottomLineSlope * bottomLineMiddleX + bottomLineB;
float leftLineMiddleX = (c4X + c1X) / 2.0f;
float leftLineMiddleY = leftLineSlope * leftLineMiddleX + leftLineB;
float rightLineMiddleX = (c2X + c3X) / 2.0f;
float rightLineMiddleY = rightLineSlope * rightLineMiddleX + rightLineB;
float topLineLeftX = (c1X + topLineMiddleX) / 2.0f;
float topLineLeftY = topLineSlope * topLineLeftX + topLineB;
float bottomLineLeftX = (c4X + bottomLineMiddleX) / 2.0f;
float bottomLineLeftY = bottomLineSlope * bottomLineLeftX + bottomLineB;
float topLineRightX = (topLineMiddleX + c2X) / 2.0f;
float topLineRightY = topLineSlope * topLineRightX + topLineB;
float bottomLineRightX = (c3X + bottomLineMiddleX) / 2.0f;
float bottomLineRightY = bottomLineSlope * bottomLineRightX + bottomLineB;
float leftLineTopX = (c1X + leftLineMiddleX) / 2.0f;
float leftLineTopY = leftLineSlope * leftLineTopX + leftLineB;
float rightLineTopX = (c2X + rightLineMiddleX) / 2.0f;
float rightLineTopY = rightLineSlope * rightLineTopX + rightLineB;
float leftLineBottomX = (leftLineMiddleX + c4X) / 2.0f;
float leftLineBottomY = leftLineSlope * leftLineBottomX + leftLineB;
float rightLineBottomX = (c3X + rightLineMiddleX) / 2.0f;
float rightLineBottomY = rightLineSlope * rightLineBottomX + rightLineB;
canvas.drawLine(topLineMiddleX, topLineMiddleY, bottomLineMiddleX, bottomLineMiddleY, dashedLine);
canvas.drawLine(leftLineMiddleX, leftLineMiddleY, rightLineMiddleX, rightLineMiddleY, dashedLine);
canvas.drawLine(topLineLeftX, topLineLeftY, bottomLineLeftX, bottomLineLeftY, dashedLine);
canvas.drawLine(topLineRightX, topLineRightY, bottomLineRightX, bottomLineRightY, dashedLine);
canvas.drawLine(leftLineTopX, leftLineTopY, rightLineTopX, rightLineTopY, dashedLine);
canvas.drawLine(leftLineBottomX, leftLineBottomY, rightLineBottomX, rightLineBottomY, dashedLine);