Beta

Combine Overlapping Polygons

Description
Loading description...
Algorithms
Geometry
  • Please sign in or sign up to leave a comment.
  • lachesism Avatar

    The current reference solution returns a clearly incorrect answer in the following test case:

    poly1 = [
        (Fraction(-27, 1), Fraction(153, 1)), 
        (Fraction(31, 1), Fraction(70, 1)), 
        (Fraction(156, 1), Fraction(81, 1)), 
        (Fraction(68, 1), Fraction(14, 1)), 
        (Fraction(121, 1), Fraction(-71, 1)), 
        (Fraction(8, 1), Fraction(-29, 1)), 
        (Fraction(-85, 1), Fraction(-93, 1)), 
        (Fraction(-66, 1), Fraction(0, 1)), 
        (Fraction(-176, 1), Fraction(46, 1)), 
        (Fraction(-51, 1), Fraction(61, 1))
    ]
    poly2 = [
        (Fraction(11, 1), Fraction(-47, 1)), 
        (Fraction(-161, 1), Fraction(-100, 1)), 
        (Fraction(-91, 1), Fraction(-4, 1)), 
        (Fraction(-243, 1), Fraction(63, 1)), 
        (Fraction(-42, 1), Fraction(59, 1)), 
        (Fraction(36, 1), Fraction(153, 1)), 
        (Fraction(90, 1), Fraction(54, 1)), 
        (Fraction(290, 1), Fraction(46, 1)), 
        (Fraction(122, 1), Fraction(-11, 1)), 
        (Fraction(168, 1), Fraction(-111, 1))
    ]
    

  • lachesism Avatar

    In Fixed Tests - Simple Case - Test 1, I got the following message:

    (
        (Fraction(20, 1), Fraction(0, 1)), 
        (Fraction(20, 1), Fraction(11, 1)), 
        (Fraction(30, 1), Fraction(15, 1)), 
        (Fraction(15, 1), Fraction(30, 1)), 
        (Fraction(11, 1), Fraction(20, 1)), 
        (Fraction(0, 1), Fraction(20, 1)), 
        (Fraction(0, 1), Fraction(0, 1))
    )
    should match
    [
        (Fraction(0, 1), Fraction(0, 1)), 
        (Fraction(0, 1), Fraction(20, 1)), 
        (Fraction(11, 1), Fraction(20, 1)), 
        (Fraction(15, 1), Fraction(30, 1)), 
        (Fraction(30, 1), Fraction(15, 1)), 
        (Fraction(20, 1), Fraction(11, 1)), 
        (Fraction(20, 1), Fraction(0, 1))
    ]
    

    Could you explain why my answer is incorrect? 🤔

    • scarecrw Avatar

      Ahh, that's on me. There was an error in comparing polygons that was corrected in the sample tests but not the main ones. Should be fixed now.

      Question marked resolved by scarecrw 8 months ago
  • hobovsky Avatar

    I wonder if using floats as inputs and outputs does not make things unnecessarily complicated, because now you have to add tolerance to all kinds of checks like equality of points, collinearity, etc., and document it. However, this kind of linear calculations can be done completely in rational numbers (i.e. fractions). If you made input points always placed on integer lattice, or possibly in locations with fractional coordinates, then all intersection points could be represented with fractions, and compared for strict equality. There would be no need for tolerance checks, there would be no potential ambiguities like should the points common for input and output be returned identical, or will they be also compared with tolerance, etc.

    • scarecrw Avatar

      Yeah, my original idea was just to be fairly lenient with tolerance checks when testing solutions, but the deeper I got into this the more I realized there are going to be tolerance checks all over the place.

      I'll take a stab at using rational numbers.

      Thanks!

    • scarecrw Avatar

      Alright, I gave this a go. Definitely got rid of a lot of the parts of the solution/checker I didn't like.

      I didn't expect that it would impact the performance as much as it did, though. I had to cut down the random tests, and the reference solution takes 4~7 seconds now instead of ~1 before. I'll keep looking to see if I can improve it.

      Let me know if you have other thoughts.

  • dfhwze Avatar

    Finally, polygons do not have a set starting point nor direction

    But the points need to be adjacent? Or does a random order of correct points pass the specs?

    • scarecrw Avatar

      They do need to be in order, but can start at any vertex and go either clockwise or counterclockwise. I'll see if I can come up with better language for that in the description.

  • Voile Avatar

    The description requires an assumption that the polygons are non self-intersecting (they're clearly non-convex). Otherwise there would be no way to tell which side is "inside".

    • scarecrw Avatar

      That's what I had intended with

      No line segments in the polygon overlap

      but I'll change it to "non self-intersecting" as that's clearer.

      Suggestion marked resolved by scarecrw 8 months ago
  • Voile Avatar

    Modifying the input will cause test display to show the input polygon after it has been modified.