1

I am currently developing a math-physics program and I need to calculate if two vectors overlap one another, the one is vertical and the other is horizontral. Is there any fast algorithm to do this because what I came up so far, has a lot of repeats. Eg lets say we have vector V1((0,1),(2,1)) and a V2((1,0),(1,2)) where the first parenthesis is the coordinates starting and the second coordinates that the vector reaches. I want as a result to take that they overlap at (1,1)

So far the only idea I came up is to ''expand'' each vector to a list of points and then compare the lists e.g for V1 its list would be (0,1) (1,1) (2,1)

JmRag
  • 1,443
  • 7
  • 19
  • 56

1 Answers1

0

Something like this? (V1 is always the horizontal vector and the coordinates are ordered, i.e. V1.x0 < V1.x1 and V2.y0 < V2.y1):

func intersection( V1, V2 )
    if v1.x0 <= v2.x0 and v1.x1 >= v2.x0 and
       v2.y0 <= v1.y0 and v2.y1 >= v1.y0 then

        return ( V2.x0, V1.y0 );
    else
        return NO_INTERSECTION;
    end if
end func
yombo
  • 334
  • 2
  • 5