Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя ajecc

Автор ajecc, история, 6 лет назад, По-английски

Can somebody explain how we can find the intersection of 2 line segments using only integer arithmetic (like this solution to a problem in a recent round 42626026)? I know how we can test if they intersect (with cross product), but I can't figure out how to find the exact point.

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
6 лет назад, # |
Rev. 3   Проголосовать: нравится +18 Проголосовать: не нравится

What do you mean only integer arithmetic? Then you should probably represent the answer as rational number. The idea is as follows. Consider intersection of lines instead of intersection of segments (then check if point lies on both segments). Assume first line is given as set of points which look like and second line is solution of equation . Then you can find that for intersection point holds . After that you simply return for the value t you found.

Switching between distinct representations of lines should be an easy exercise for you.

P.S. You can also find the intersection point in algebraic way, solving system of two linear equations. This is straightforward but may yield a bit more of code.

P.P.S. You may refer to this article for more detailed explanation of basic analytic geometry stuff, including your question.

»
6 лет назад, # |
  Проголосовать: нравится +24 Проголосовать: не нравится

If you represent lines as equations like

ax + by = c
dx + ey = f

then intersection is the solution of the equation syste, which you can find using Cramer's rule, which means that you need to calculate 3 2x2 determinants A, B, C, and answers are A/C, B/C. Lines are parallel (or coincide) iff C=0

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    this is so easy to understand. now i feel a little dumb for asking this question. thank you

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      For real? Like, this method was clearly mentioned in my answer alongside, with the one which needs only one line of code and assumes geometric approach and you think, this one is better? :\

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        It's just that I totally forgot that you could represent a line with ax+by=c instead of the classical representation with mx+n=y and the comment made sense right away. Stupid of me, i know. I'm sorry if I dissapointed you in any way.

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится -8 Проголосовать: не нравится

          Ok, sorry for being too emotional. I really was a bit disappointed since I mentioned system of linear equations method in my reply and that article. But no worries :)

          Anyway, you should probably avoid representing line as since it conceals true meaning of this equation, which is, of course, in coordinate form, where is normal vector to the line and is arbitrary point lying on the line.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится -10 Проголосовать: не нравится

        ofc this one is better — no thinking required

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится -13 Проголосовать: не нравится

          I didn't ask for opinion of a person who despises geometry and thinking. Get lost.