Checking if a point is inside a polygon is RIDICULOUSLY simple (Ray casting algorithm) - Inside code

Поділитися
Вставка
  • Опубліковано 20 жов 2024

КОМЕНТАРІ • 51

  • @insidecode
    @insidecode  Рік тому +4

    Discover the new graph theory algorithms course: inscod.com/graphalgo
    🔴
    / \
    🔵-🔴
    | |
    🔴-🔵

  • @vid2422
    @vid2422 Рік тому +22

    That even/odd technic blew my mind, I never expected the method to be simple yet genius at the same time

  • @BlackSharkfr
    @BlackSharkfr 11 місяців тому +13

    Simple and fast algorithm, but it has one small edge case (literally).
    This code behaves inconsistently when the point is located on one of the polygon's edges : some edges will count as inside, and others will count as outside.
    To fix this : check whether the point is on any of the edges before actually running this algorithm.

  • @jumpsneak
    @jumpsneak 2 дні тому +1

    No sound???

  • @hhkl3bhhksm466
    @hhkl3bhhksm466 10 місяців тому +4

    Hey, this is kinda weird, but I was doing a programming challenge and you needed to know where a certain point needed to be in a list of coordinates for a polygon. This video helped a lot, and the solution worked like a charm. Keep up the good work

    • @WeloveKoora
      @WeloveKoora 10 місяців тому +1

      adventofcode 2023 Day 10 pipe maze ?

    • @hhkl3bhhksm466
      @hhkl3bhhksm466 10 місяців тому

      @@WeloveKoora Yeah, haha, did you also use the same method?

    • @WeloveKoora
      @WeloveKoora 10 місяців тому

      @@hhkl3bhhksm466 Yes, after 24 hours of searching hhhhh

    • @WeloveKoora
      @WeloveKoora 10 місяців тому

      @@hhkl3bhhksm466 it was very challenging problem, but i learned a lot

  • @miguelchenier4817
    @miguelchenier4817 Рік тому +1

    Thank you. This worked so well. I've been beating myself up trying to think of a solution.

  • @iWillRun_K
    @iWillRun_K 2 місяці тому

    I was thinking to check where point lines relative to each vector representing polygon side, it is also O(n) (in worst case) time and it works faster than above algorithm since it don't have to iterate through each side of polygon,

  • @JamesSarantidis
    @JamesSarantidis 3 місяці тому

    Awesome. I watch searching for an explanation on ray casting and yours was perfect. Now, I have a different problem to solve. I want to generate the shape of a city starting from a rectangle and want to morph semi-randomly in a way that it contains some Point of Interest (POI) while it excludes Points of Avoidance (POA). Any Idea as to how to do this smartly; without checking each time if every single deformation left some POI out or let some POA in?

  • @kleare520
    @kleare520 Місяць тому

    I'd say a proper and rigorous implementation that works for ANY points and polygon is RIDICULOUSLY difficult for what it acheives. I'm not talking about the edge cases "what if you select a point on the edge", I'm talking about the (very annoying edge case) where the raycast intersect with a vertex of the polygon. In that case, you have no choice but to pick another random direction, and then the math become way more difficult and messy. Also you need to run tons of test to avoid division by 0.
    Thankfully there are some workarround. If you can ensure that your polygone coordinates are integers, then just offset the y raycast coordinate by 0.5.

  • @antonioradu2842
    @antonioradu2842 Рік тому +1

    just got a homework where I need to check if a point is inside a polygon, thx :D

  • @manolski7615
    @manolski7615 Рік тому +3

    what if y2 - y1 = 0 for the 2nd cond? Do we just ignore that count?

    • @fnxph03n1x
      @fnxph03n1x Рік тому

      If y2 == y1, it means that you have a horizontal line, then you can check if your point is on this line too or not. if y2 == y1, check if yp == y1 (or == y2, both are equal), if yes, your point is on the same direction as the line y1 == y2, and then you can check the x-condition. If x-condition is also satisfied, it means that your point is on the edge itself. It's up to you if you want the points on the edge to be included in or not. If you wanted it included, increment the counter

    • @gdclemo
      @gdclemo 9 місяців тому

      @@fnxph03n1x if y2 == y1 then the first condition ( (yp < y1) != (yp < y2) ) will never be True, so the second condition will not be evaluated.

  • @trungkstn
    @trungkstn 3 місяці тому

    How about case y1 = y2

  • @surenbono6063
    @surenbono6063 11 місяців тому

    ...do you have link to the world timezones vertex defined database?..for automatic local GPS clock decoder

  • @CrescentP-wi7ps
    @CrescentP-wi7ps 7 місяців тому +2

    4:55 You didn't handle divide by zero case.

  • @RandomGeometryDashStuff
    @RandomGeometryDashStuff Рік тому +4

    what if ray passes through polygon corner?

    • @insidecode
      @insidecode  Рік тому +5

      I think that it will count once

    • @janasimjanovska855
      @janasimjanovska855 Рік тому

      @@insidecode what if the point is outside of a concave quadrilateral, we cast a line along the x - axis, and the line passes through a vertex?

  • @aakashs1806
    @aakashs1806 5 місяців тому

    Does this work for bodies in 3D space?

  • @samsaraAI2025
    @samsaraAI2025 8 місяців тому

    Nice video thanks. Could you upload the same video for 3D non convex o bjects? :)

  • @juststudying1019
    @juststudying1019 Рік тому

    Thanks, but why to check the intersection of the point with each edge? isn't it enough to check for one direction and the equilivant edges?

    • @insidecode
      @insidecode  Рік тому +1

      But how to know the edges of that directon?

    • @juststudying1019
      @juststudying1019 Рік тому

      Yes that's right, but in this case will we count the point intersection twice and it will be even, for example a point in a square if we count it with the four edges it will be 4, is that right? so it is outside but if we only calculate the intersection with left edge for example it will be odd then it is inside
      @@insidecode

    • @insidecode
      @insidecode  Рік тому

      Why would we count the intersection twice?

    • @juststudying1019
      @juststudying1019 Рік тому

      @@insidecode I mean ehen we loop through each edge of the shape when the point is inside it will be counter 4 times if the shape is square

    • @insidecode
      @insidecode  Рік тому

      No we increment the counter only when there's an intersection

  • @matshagerlind
    @matshagerlind 6 місяців тому

    Thank you very much, great video! :)

  • @JehanSaren
    @JehanSaren 11 місяців тому

    Hi sir, I made a net simulation, how do I know that the particles are inside the net?

    • @xLainik
      @xLainik 9 місяців тому

      Check out "Determining if a point lies on the interior of a polygon" by University of Michigan. It's on google

  • @toohadi
    @toohadi Рік тому

    Great video, thanks!

  • @Prakash-cw2ml
    @Prakash-cw2ml Рік тому +1

    It very useful🎉

  • @senthil6382
    @senthil6382 Рік тому

    is it possible to generate all the points inside the polygon, if the polygon is represented by vertices

    • @insidecode
      @insidecode  Рік тому +4

      There is an infinity of points inside a polygon

  • @pauliusviskaitis9640
    @pauliusviskaitis9640 Рік тому

    Thanks!

  • @andreasbritzemeier6901
    @andreasbritzemeier6901 Рік тому

    Does anyone know if there is an easy extension to 3D ?

  • @Phosdoq
    @Phosdoq 7 місяців тому

    some shapes must be treated with their own condition: like circles, rectangles, triangles...

    • @colefrankenhoff1428
      @colefrankenhoff1428 3 місяці тому

      Nope, they don't

    • @Phosdoq
      @Phosdoq 3 місяці тому

      @@colefrankenhoff1428 they don't if you are bad programmer. you will gain instant 120% performance if you do so :)

  • @cosmoquissoyt5636
    @cosmoquissoyt5636 Рік тому

    Alto capo me sirvió

  • @SetszawA
    @SetszawA 10 місяців тому +1

    Bad solution if point is in the direction of two or more edges, it will give inconsistent results.

  • @解夢人
    @解夢人 9 місяців тому

    Why always think in terms of the plane? How about presenting an algorithm example for three-dimensional space?

    • @_danisson
      @_danisson 4 місяці тому

      what question is that ? a lot of things works in terms of the plane. If you have a three dimensional space maybe you can use another algorithm