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

  Рет қаралды 25,672

Inside code

Inside code

Жыл бұрын

Source code: gist.github.com/inside-code-y...
🔴 Learn graph theory algorithms: inscod.com/graphalgo
⚙ Learn dynamic programming: inscod.com/dp_course
💡 Learn to solve popular coding interview problems: inscod.com/50problems_course
⌛ Learn time and space complexity analysis: inscod.com/complexity_course
🔁 Learn recursion: inscod.com/recursion_course
NB: This video is ad-free, you can choose to support Inside code by purchasing one of the courses above or dropping a super thanks!
NB2: Discounts of courses above are permanent
I also post content on LinkedIn (inscod.com/linkedin) and Instagram (inscod.com/instagram)

Пікірлер: 47
@insidecode
@insidecode Жыл бұрын
Discover the new graph theory algorithms course: inscod.com/graphalgo 🔴 / \ 🔵-🔴 | | 🔴-🔵
@vid2422
@vid2422 Жыл бұрын
That even/odd technic blew my mind, I never expected the method to be simple yet genius at the same time
@insidecode
@insidecode Жыл бұрын
Me neither
@BlackSharkfr
@BlackSharkfr 8 ай бұрын
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.
@miguelchenier4817
@miguelchenier4817 8 ай бұрын
Thank you. This worked so well. I've been beating myself up trying to think of a solution.
@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?
@toohadi
@toohadi Жыл бұрын
Great video, thanks!
@matshagerlind
@matshagerlind 3 ай бұрын
Thank you very much, great video! :)
@surenbono6063
@surenbono6063 8 ай бұрын
...do you have link to the world timezones vertex defined database?..for automatic local GPS clock decoder
@pauliusviskaitis9640
@pauliusviskaitis9640 Жыл бұрын
Thanks!
@Prakash-cw2ml
@Prakash-cw2ml Жыл бұрын
It very useful🎉
@hhkl3bhhksm466
@hhkl3bhhksm466 7 ай бұрын
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 6 ай бұрын
adventofcode 2023 Day 10 pipe maze ?
@hhkl3bhhksm466
@hhkl3bhhksm466 6 ай бұрын
@@WeloveKoora Yeah, haha, did you also use the same method?
@WeloveKoora
@WeloveKoora 6 ай бұрын
@@hhkl3bhhksm466 Yes, after 24 hours of searching hhhhh
@WeloveKoora
@WeloveKoora 6 ай бұрын
@@hhkl3bhhksm466 it was very challenging problem, but i learned a lot
@antonioradu2842
@antonioradu2842 Жыл бұрын
just got a homework where I need to check if a point is inside a polygon, thx :D
@cosmoquissoyt5636
@cosmoquissoyt5636 Жыл бұрын
Alto capo me sirvió
@aakashs1806
@aakashs1806 Ай бұрын
Does this work for bodies in 3D space?
@CrescentP-wi7ps
@CrescentP-wi7ps 4 ай бұрын
4:55 You didn't handle divide by zero case.
@samsara2024
@samsara2024 4 ай бұрын
Nice video thanks. Could you upload the same video for 3D non convex o bjects? :)
@manolski7615
@manolski7615 Жыл бұрын
what if y2 - y1 = 0 for the 2nd cond? Do we just ignore that count?
@fnxph03n1x
@fnxph03n1x 9 ай бұрын
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 6 ай бұрын
@@fnxph03n1x if y2 == y1 then the first condition ( (yp < y1) != (yp < y2) ) will never be True, so the second condition will not be evaluated.
@user-px5pj7ux5k
@user-px5pj7ux5k 7 ай бұрын
Hi sir, I made a net simulation, how do I know that the particles are inside the net?
@xLainik
@xLainik 6 ай бұрын
Check out "Determining if a point lies on the interior of a polygon" by University of Michigan. It's on google
@juststudying1019
@juststudying1019 10 ай бұрын
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 10 ай бұрын
But how to know the edges of that directon?
@juststudying1019
@juststudying1019 10 ай бұрын
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 10 ай бұрын
Why would we count the intersection twice?
@juststudying1019
@juststudying1019 10 ай бұрын
@@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 10 ай бұрын
No we increment the counter only when there's an intersection
@andreasbritzemeier6901
@andreasbritzemeier6901 9 ай бұрын
Does anyone know if there is an easy extension to 3D ?
@user-lx4sp1gl4f
@user-lx4sp1gl4f 8 ай бұрын
Project you shape to 2D plane and then try this method.
@RandomGeometryDashStuff
@RandomGeometryDashStuff Жыл бұрын
what if ray passes through polygon corner?
@insidecode
@insidecode Жыл бұрын
I think that it will count once
@janasimjanovska855
@janasimjanovska855 11 ай бұрын
@@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?
@senthil6382
@senthil6382 Жыл бұрын
is it possible to generate all the points inside the polygon, if the polygon is represented by vertices
@insidecode
@insidecode Жыл бұрын
There is an infinity of points inside a polygon
@Phosdoq
@Phosdoq 3 ай бұрын
some shapes must be treated with their own condition: like circles, rectangles, triangles...
@colefrankenhoff1428
@colefrankenhoff1428 6 күн бұрын
Nope, they don't
@Phosdoq
@Phosdoq 6 күн бұрын
@@colefrankenhoff1428 they don't if you are bad programmer. you will gain instant 120% performance if you do so :)
@SetszawA
@SetszawA 6 ай бұрын
Bad solution if point is in the direction of two or more edges, it will give inconsistent results.
@igorfujs7349
@igorfujs7349 4 ай бұрын
So true.
@user-gm2im7ws5c
@user-gm2im7ws5c 5 ай бұрын
Why always think in terms of the plane? How about presenting an algorithm example for three-dimensional space?
@_danisson
@_danisson Ай бұрын
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
How to find fixed-radius neighbors of a point? - Inside code
6:59
Inside code
Рет қаралды 2,5 М.
Ray Tracing: How NVIDIA Solved the Impossible!
16:11
Two Minute Papers
Рет қаралды 789 М.
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,7 МЛН
ОСКАР ИСПОРТИЛ ДЖОНИ ЖИЗНЬ 😢 @lenta_com
01:01
Heartwarming: Stranger Saves Puppy from Hot Car #shorts
00:22
Fabiosa Best Lifehacks
Рет қаралды 20 МЛН
Quicksort In Python Explained (With Example And Code)
14:13
FelixTechTips
Рет қаралды 136 М.
Super Fast Ray Casting in Tiled Worlds using DDA
30:03
javidx9
Рет қаралды 176 М.
The hidden beauty of the A* algorithm
19:22
polylog
Рет қаралды 841 М.
Sliding Window Technique
6:18
Profound Academy
Рет қаралды 6 М.
Point in convex polygon - fast O(qlogn) algorithm
2:34
MathEsthesia
Рет қаралды 10 М.
Coding Challenge 180: Falling Sand
23:00
The Coding Train
Рет қаралды 806 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
When Your Game Is Bad But Your Optimisation Is Genius
8:52
Vercidium
Рет қаралды 1,4 МЛН
This is how Paint's bucket fill works (Flood fill algorithm)
8:54
Ray casting fully explained. Pseudo 3D game
5:09
WeirdDevers
Рет қаралды 26 М.
OZON РАЗБИЛИ 3 КОМПЬЮТЕРА
0:57
Кинг Комп Shorts
Рет қаралды 1,2 МЛН
ПОКУПКА ТЕЛЕФОНА С АВИТО?🤭
1:00
Корнеич
Рет қаралды 3,7 МЛН