1

I have two x,y pairs that create a line within a bounding box.

coord1 = 75, 180

coord2 = -30, 300

The bounding box is x0 to x500 and y0 to y400 enter image description here

I want to create an object that can tell me the coordinates of where the line intersects the bounding box.

i.e. Intercept.new(bounding_box, coord1, coord2).call! returns the intercept point [x,y]

I believe I need to use y = mx + b, but I'm having trouble writting an object that can take these two coordinates, factor in the bounding box, and tell me where the intersection point happens. Can anyone take a shot and help me out here?

EDIT Not a duplicate of the question linked in the comments. That question has a constant of the point B always being in the center of the rectangle.

hummmingbear
  • 2,294
  • 5
  • 25
  • 42
  • If a line intersects a 'box' there will be two points of intersection, no?. Also why is your y-axis inverted? – Sagar Pandya Jan 13 '17 at 23:02
  • 1
    @sagarpandya82 it's inverted because it's how the image-program I'm using uses `x,y`. It doesn't affect how `y=mx+b` works though. The line is finite, and only intersects with one part of the bounding box. – hummmingbear Jan 13 '17 at 23:07
  • What have you tried? Have you done anything with https://github.com/DanielVartanov/ruby-geometry or are you just asking for us to code for you? – OneNeptune Jan 13 '17 at 23:09
  • @OneNeptune that gem looks awesome. It has x/y-intercept methods, I'll start digging into it, thanks. Honestly, not necessarily asking anyone to code it for me, I just feel stuck, not sure where to start. – hummmingbear Jan 13 '17 at 23:48
  • @OneNeptune that gem is great, but can't calculate intersection points in a bounding box – hummmingbear Jan 14 '17 at 00:00
  • Possible duplicate of [How to find the intersection point between a line and a rectangle?](http://stackoverflow.com/questions/1585525/how-to-find-the-intersection-point-between-a-line-and-a-rectangle) – Peter O. Jan 14 '17 at 02:43

1 Answers1

1

You're on the right track with utilizing y = mx + b concepts, and some further linear algebra will be required to solve the problem exactly as you're drawing it. However, you stated you were just looking for direction on where to start for approaching this particular problem.

It seems someone ran into a similar problem regarding projectile intersections while developing a game that may be relevant to your struggles. Here's his blog post: http://factore.ca/blog/166-how-to-calculate-the-point-of-intersection-between-a-line-and-a-bounding-box

Here's a link to his ruby specific solution to his problem: https://github.com/adriand/intercept-calculator/blob/master/intercept_math.rb

Hope this helps!

OneNeptune
  • 883
  • 11
  • 20