-2

I need to find the point of intersection between some square xy + N,N where N is the size of the square and some line x1,y1,x2,y2 where x1,y1 lies on the center point of the square. Assume that the square is parallel to the x-axis & y-axis.

Related Questions:

  1. Intersection between a line section and square
  2. Coordinate of intersection between line and square

The answer in #1 provided essentially explains that you clamp one of the coordinates of the line to the square, which is inaccurate and produces incorrect results. The answer in #2 provides some obscure formula which isn't explained.

Kayle
  • 1

1 Answers1

0

I've worked out the answer. You find the long-side of the triangle formed by the line x1,y1,x2,y2 and multiple x2,y2 by a scalar which is the difference between the long side of the triangle and 1/2 the length of the square.

line = x1,y1,x2,y2
square = x3,y3 + N,N

difference = (x2,y2) - (x3,y3)

// upper/lower quadrant of one quadrant in cartesian coordinate space. if |difference.x| > |difference.y| then quadrant = 1 if |difference.x| <= |difference.y| then quadrant = 0

if quadrant == 0 then project.x = x2 project.y = y3

if quadrant == 1 then project.x = x3 project.y = y2

longside = |project.x + project.y - x1 + y1| scalar = (N / 2) / longside intersect = x1,y1 + (difference * scalar)

See example on Shadertoy: https://www.shadertoy.com/view/mtdyDn

Kayle
  • 1