I found a bad case: AB = ((0.0f, 0.0f), (10.0f, 0.0f)) and CD = ((10.0f, 0.0f), (20.0f, 0.0f)) bottom will be 0 so it will find no solution while the intersection is 10.
@@Radu better to keep talking on the ticket that I reopened on your virtual-world project. Tonight I'm busy. Yes collinearity is painful and shall be managed separately: if segment are superposed, you have plenty of intersections (but let say the 2 corners of the smaller segment), else 0 intersection when they are parallel, else 1 intersection. On other point in your road generation, you discard the case where segments are connected by a single vertex :)
After trying a number of methods, I'd determined line segment collision detection to be an integral part of an effective way to determine A) if a sprites projected path is going to be intersecting a block B) which side of the block and therefore how to respond to it. I've looked at a number of resources that give similar equations with little explanation of beyond what the components are. I hate using math I don't understand in my projects, so the in depth explanation given here was a godsend. Keep producing awesome content!
Radu is a super teacher!! Teaching the intuition and logic behind more complex topics, by simply breaking down the complex into smaller simple pieces is a brilliant way to teach, and surely the lesson will stick with the student forever, I'm glad that people like you exist and are willing to share that knowledge, Thank you Radu!!
@@Radu Actually,I saw your PhD defending .I have to admit you did a great work! I just wanted to tell you about your pure talent of explanation. Always thought that I am dumb in math and will never get it.But after some "switches" to good written work or people who helped me to understand more clearly the subject ,I feel myself more confident in math right now . You know about people psychology/attitude .If you dont get once you will never return to it again ,saying it's not for me or it's too hard.So the world needs a good teachers and it doesn't matter if you are a prof in univ or youtube maker. Anyway love your content,wish you the best. P.s :Can't imagine my self studying CS w/o Stackoverflow or youtubers lol.
Great linear interpolation related video! I hope my English level could be good enough to be able to express gratitude for your videos. The creative way in which you explain, the program you develop to exemplify what is being explained in an entertaining way, the editing... all the time spent in front of the camera and off it, has great value to me. All your students should be proud to have such a great teacher!
This is a really good and pictorial explanation! As a mathematician I would have explained it quite differently, but probably you are right that this scares people away :D
I'm glad to see a mathematician agreeing with me :-) I was actually waiting for the opposite :-D what I did here is not really standard... and not a formal proof, but I feel these things are needed as well. Visualizing things like that helped me a lot during my research: not only to verify new ideas, but also to see if there are bugs in the code.
other way i was thinking about intersection is sorting that happens in production factory where 2 similar or different product either intersect or separate out
Thank you sir, I really appreciate that you explained it in a programming way and not in the math way. 🙏 You have no idea how many videos I've watched to understand this topic, but because all of them explained it with the math language, I couldn't comprehend it at all and it just frustrated me
As all of your videos have been so far - very entertaining and enlightening. I am excited to see the rest of this series 😀 Thank you for putting the effort into these.
I really love your tutorials 👏👏 How do I detect a shape click on the canvas? Example if I have a circle or rectangle on the canvas at any point and I click on any of them how will I know if the user clicked a section of the rectangle or circle?
interesting approach, i just used the 2point vector form, and shuffled them around to get the intersection point. It's maybe a little bit easier. like (y2-y1)/(x2-x1) * (x-x1) *y1. Shorter, less to calculate.
You mean looking for a little picture of a car, and then simply replace.. ctx.rect(....) with.. ctx.drawImage(img, sx,sy,sw,sh, dx,dy,dw,dh) ??? i believe in you! you can do it! ;-)
Thanks! About the car, I did post one challenge on my community tab where the goal was to make a car, but I will eventually show you how to draw it and how to make them have random colors (it will be the final lecture).
@@ap666-o8h Glad to hear. I've put the most effort into the Neural Network lecture. Should be out on 15th of April (on my birthday :-D) if all goes according to plan.
for the calculation of uTop, why is there a A.x - B.x instead of B.x - A.x? I followed the same steps as the steps for getting t and can't seem to get this result
@@Radu Thanks for the reply. Yeah I figured it out. I checked and the subtraction in the divisor of my u equation is actually reversed. So I had to multiply both side by -1 and got the same answer.
Slove the system ecuation composed of both linear equations d1:ax+by+c=0 and d2:ax+by+c=0, and you get the intersection point , but first test the slope for paralel lines. at least that is what I've tried.
What you're writing here is the line equation. And lerp, as the name says: 'linear' interpolation is also a line, yes. But in lerp m is defined as a difference between two end values, say: t and w (with your naming convention). So y=(w-t)x+t. That's because lerp is supposed to give you values for y within the interval [t,w] with values for x in [0,1]. You can also use x outside of the [0,1] range and then it's called extrapolation.
You can get the code for getIntersection from the self driving car repository: github.com/gniziemazity/Self-driving-car But the code of this whole demonstration is not online.
Hard to help in the comments... just keep in mind that the result you get can be different as long as it is equivalent. Like -(B-A) is the same as (A-B), but a minus (-) must be there.
Thanks for the wonderful content, very much appreciated. I just have a question which is killing me, you didn't initialize the variable myCanvas and somehow it is working with you, hooow?😃
All IDs defined using HTML are by default global variables. I don't do this very often because its easy to overwrite those if not careful... I don't recommend that practice, but for something like this, where we're not really building anything real, it's ok. Good question! I think not many people know this!
@@Radu wow this is new to me, I have an experience for like 4 years now and this is the first time to know this. Thanks again for the explanation and for the content.
@@osamaabozahra Maybe better that way :-) it's not a good practice, I think. And, I'm sure that after 4 years you know many other things I don't! Keep it up and thanks for watching!
How is it possible to select the canvas on the id without a query selector? What is this sorcery! And thanks for great videoes - don't hold the math =)
@@Radu I really love and admire your thinking and explaining capabilities. You are JUST amazing ! I am continuously watching your videos from last 3 days and feel JS is easy and fun. Understanding mathematical expressions taking some time, though. 🤭
@@Radu If the segments overlap, I would return a point of intersection anywhere in the region of overlap, maybe using both segments current direction of movement to set a collision point that's "happening first". What's your take on this? Was it intentional to not trigger a collision with overlapping segments?
@@rmsv Well, if they overlap there is no collision 'point' but a collision 'segment'... so proper way would be to return that. Now, I couldn't possibly return all (infinitely many) points that form that segment :-) so maybe returning it as a kind of interval that represents all points within the range. I don't know :-| another possible way would be to return nothing... if intersection would mean that one segment must have one endpoint on one side and another on the other (which overlapping segments do not have). I think this is actually the case in this video. Would have to check the code again to verify and I'm not at the office, so I'll probably forget :-D
@@Radu The part of getting back to math, even tho it's basic math I'm not used to think at all, I did the uTop alone and got the same as you (glad for that). I do got questions for this project on C#, as I'm stuck in this part specificaly, whenever I try to create my lines, I put them in a Canvas but for some reason they all get messed up, the orientations isn't good and half of the lines get hidden for some unknown reason to me. It does work with Rectangles, except I don't know how to get their coordinates inside the Canvas. Thanks for the lessons, I watched everything twice now. I'm doing an Automation bachelor degree and A.I. is something I want to get to know, in it's raw state.
@@zVOLT220z Glad to hear you find this useful. If you're having trouble with doing it in C# you could try sharing the code (like maybe on my Discord) and I'll have a look.
Here I transcribed the code: (But I made some minor changes to fit my needs, but It works and you might find this helpfull) let canvas = document.getElementById("canvas") let ctx = canvas.getContext("2d") let angle=0; const A={x:200, y:150}; const B={x:150, y:250}; const C={x:50, y:100}; const D={x:250, y:200}; const mouse={x:0,y:0}; document.onmousemove=(event)=>{ mouse.x=event.x; mouse.y=event.y; } function lerp(A,B,s) { return A+(B-A)*s } let t=-1; let u = -1; function drawDot(point,label,isOutside) { ctx.beginPath() ctx.fillStyle=isOutside?"red":"white"; ctx.arc(point.x,point.y,10,0,Math.PI*2); ctx.fill() ctx.stroke(); ctx.fillStyle="black"; ctx.textBaseline="middle"; ctx.font="bold 14px Arial" ctx.textAlign="center" ctx.fillText(label,point.x,point.y); } function animate() { const radius=50; A.x=mouse.x+Math.cos(angle)*radius; A.y=mouse.y-Math.sin(angle)*radius; B.x=mouse.x-Math.cos(angle)*radius; B.y=mouse.y+Math.sin(angle)*radius; angle+=0.02; ctx.clearRect(0,0,canvas.width,canvas.height); ctx.beginPath(); ctx.moveTo(A.x, A.y); ctx.lineTo(B.x, B.y) ctx.moveTo(C.x, C.y); ctx.lineTo(D.x, D.y) ctx.stroke(); drawDot(A,"A",false); drawDot(B,"B",false); drawDot(C,"C",false); drawDot(D,"D",false); let M={ x:lerp(A.x,B.x,t), y:lerp(A.y,B.y,t) } let N={ x:lerp(C.x,D.x,u), y:lerp(C.y,D.y,u) } const I=getIntersection(A,B,C,D); if (I) {drawDot(I,"I",false)} //ctx.beginPath(); //ctx.rect(canvas.width/2,0,I.bottom/100,10); //ctx.fill(); drawDot(N,"N",u1); u+=0.005; drawDot(M,"M",t1); t+=0.005; requestAnimationFrame(animate); } function getIntersection(A,B,C,D){ const vTop=(D.x-C.x)*(A.y-C.y)-(D.y-C.y)*(A.x-C.x); const uTop=(C.y-A.y)*(A.x-B.x)-(C.x-A.x)*(A.y-B.y); const bottom=(D.y-C.y)*(B.x-A.x)-(D.x-C.x)*(B.y-A.y); if(bottom!=0){ const v=vTop/bottom; const u=uTop/bottom; if(v>=0 && v=0 && u
Amazing tutorial Dr Radu, I have made a little change breaking "getIntersection" into two functions and interchanging arguments:
function getIntersection(A, B, C, D) {
const t = calculateOffsets(A, B, C, D)
const u = calculateOffsets(C, D, A, B)
if (t && u) {
return {
x: lerp(A.x, B.x, t),
y: lerp(A.y, B.y, t),
offset: t
}
}
return null
}
function calculateOffsets(A, B, C, D) {
const top = (D.y - C.y) * (A.x - C.x) - (D.x - C.x) * (A.y - C.y)
const bottom = (D.x - C.x) * (B.y - A.y) - (D.y - C.y) * (B.x - A.x)
if (bottom != 0.0) {
const offset = top / bottom
if (offset >= 0 && offset
Hey! That's really nice :-)
Pinning your comment for others to see!
I found a bad case: AB = ((0.0f, 0.0f), (10.0f, 0.0f)) and CD = ((10.0f, 0.0f), (20.0f, 0.0f)) bottom will be 0 so it will find no solution while the intersection is 10.
You're right! Bottom is also zero when points of the segments are collinear...
I wonder what we can do about it? 🤔
@@Radu better to keep talking on the ticket that I reopened on your virtual-world project. Tonight I'm busy. Yes collinearity is painful and shall be managed separately: if segment are superposed, you have plenty of intersections (but let say the 2 corners of the smaller segment), else 0 intersection when they are parallel, else 1 intersection. On other point in your road generation, you discard the case where segments are connected by a single vertex :)
I will have to check those tickets at some point. Have had many things keeping me busy, recently :-\
I simply can't explain how much I love this guy, one of the best teachers I ever had.
Good that you are learning :-)
Radu is pure gold )
You lay things out clearly, explain things well and code at a pace that is easy to follow along. Top notch teaching Radu 💯!
Thank you :-)
@@Radu You are very welcome Radu 😁!
After trying a number of methods, I'd determined line segment collision detection to be an integral part of an effective way to determine A) if a sprites projected path is going to be intersecting a block B) which side of the block and therefore how to respond to it. I've looked at a number of resources that give similar equations with little explanation of beyond what the components are. I hate using math I don't understand in my projects, so the in depth explanation given here was a godsend. Keep producing awesome content!
I'm happy to hear you found good uses for it :-) Thanks for watching!
One of the best tutor for learning maths and coding.
Thank you :-)
a master class, thank you very much Radu, waiting for the next one.
Thanks Eternam :-) glad to hear you're excited!
Radu is a super teacher!!
Teaching the intuition and logic behind more complex topics, by simply breaking down the complex into smaller simple pieces is a brilliant way to teach, and surely the lesson will stick with the student forever,
I'm glad that people like you exist and are willing to share that knowledge,
Thank you Radu!!
Glad to be of help :-)
Great stuff :) I really like the approach of visualizing things to make the explanations clearer
Thanks! I'll make try to make more things like this.
i really love this sort of coding, which involves maths! It makes the problem so much more engaging and challenging!
I like it too... It also makes the math easier to understand when visualized like that. Thanks for watching!
You opened my mind in a way that nobody did before, thanks.
Happy to hear that :-)
Dude ,you have to become a prof.You are such genious of explaining the complex things in the easiest way.Thank you sir.
I got my PhD in 2017. Does it count? :-)
@@Radu Actually,I saw your PhD defending .I have to admit you did a great work!
I just wanted to tell you about your pure talent of explanation.
Always thought that I am dumb in math and will never get it.But after some "switches" to good written work or people who helped me to understand more clearly the subject ,I feel myself more confident in math right now .
You know about people psychology/attitude .If you dont get once you will never return to it again ,saying it's not for me or it's too hard.So the world needs a good teachers and it doesn't matter if you are a prof in univ or youtube maker.
Anyway love your content,wish you the best.
P.s :Can't imagine my self studying CS w/o Stackoverflow or youtubers lol.
Thanks for the nice comment :-) [PS: UA-cam just enabled a feature that I can easily find these replies... wasn't possible until now]
thank you teacher. I'm Brazilian, I was looking for some content about collision between line segments, but I couldn't find it, you saved me.
Glad you found it useful. Another, more mathematical approach is linked in my video description.
Wow! Didn't even realize I actually watched a half hour content without skipping anything. Awesome explainations man. You've earned a sub.
Glad to hear :-) hope you'll find it useful!
Great linear interpolation related video! I hope my English level could be good enough to be able to express gratitude for your videos. The creative way in which you explain, the program you develop to exemplify what is being explained in an entertaining way, the editing... all the time spent in front of the camera and off it, has great value to me. All your students should be proud to have such a great teacher!
Thanks for the nice words, and thanks for watching :-)
This is a really good and pictorial explanation! As a mathematician I would have explained it quite differently, but probably you are right that this scares people away :D
I'm glad to see a mathematician agreeing with me :-) I was actually waiting for the opposite :-D what I did here is not really standard... and not a formal proof, but I feel these things are needed as well. Visualizing things like that helped me a lot during my research: not only to verify new ideas, but also to see if there are bugs in the code.
Hey mathematician please teach me.
Can you think of a way to use segment intersection in your project? :-)
I've tried to use intersection in roundabout and confused the hell out of myself (⊙_⊙;)
other way i was thinking about intersection is sorting that happens in production factory where 2 similar or different product either intersect or separate out
@@basiccoder2166 Oh, wow. Did you di intersection between line and circle then? :-)
@@basiccoder2166 Yeah, that's the (intersection) concept from set theory :-)
@@Radu yes tried to😂
Thank you sir, I really appreciate that you explained it in a programming way and not in the math way. 🙏
You have no idea how many videos I've watched to understand this topic, but because all of them explained it with the math language, I couldn't comprehend it at all and it just frustrated me
I'm happy to hear :-) it's the reason I made it.
Watched, it is just all about algebra, solving system of equations, at last I found the practical use of those countless school exercises )
Yeah, one hope I have with these videos is to make people see that the things they learn in school are not 'useless'.
Very useful!
Right now, i know how to use this function in all cases as i need after understanding it. Thank you.
You're welcome!
As all of your videos have been so far - very entertaining and enlightening. I am excited to see the rest of this series 😀
Thank you for putting the effort into these.
Great to hear that :-) Thanks for watching!
I love Radu. Great teacher.
Thanks :-)
Underrated channel.
Thanks for watching :-)
I really love your tutorials 👏👏
How do I detect a shape click on the canvas?
Example if I have a circle or rectangle on the canvas at any point and I click on any of them how will I know if the user clicked a section of the rectangle or circle?
You may want to look into the 'isPointInPath' method of the canvas context.
May be you can check if your mouseX and MouseY is same as PointX PointY
@@Radu oh okay 🔥🔥
literally the only explanation i can find for the hardest part of raycasting
Really? There must be others as well :-) it's a popular topic!
Thank you very much for your tutorials.
Glad you like them! :-)
Okay. Well, THAT was INCREDIBLE!
Haha, glad to hear :-)
I liked.😁👍 Simple and direct. Very good teaching. Demonstrating mastery over what you want to explain 👏👏👏👏. So you've gained another subscriber. 👍😁
Thank you :-)
Brilliant explanation, thank you so much!
Glad it was helpful!
Thank you for this explination. This is pobably the best vidoes i have seen on youtube relating to CS!!!!!!!!!!!!!!!!❤
Glad to hear that. I tried to handle the concept well.
Great video! I learn a lot with your tutorials!
Thanks :-) Happy to hear that!
Thank you, professor Gem! 💎❤
:-)
English is not my native language, but I can perfectly understand everything you say.
You are amazing, I love your channel and I wish I knew sooner!
Hi, and welcome to the channel :-)
your intro is great :D
Haha, thanks! There's an extended version on my channel :-)
@@Radu I showed it to my gf, and now we will just randomly sing "coding with Radu.🎶Coding with Radu🎶"
@@mattoattacko Probably a one hit wonder :-))
@@Radu Would win Eurovision IMO😁
@@mattoattacko Hahaha :-)) next year maybe!
please explain how to find the uTop, u cant calculate it, i always get (B - A) there instead of (A - B), please help
interesting approach, i just used the 2point vector form, and shuffled them around to get the intersection point. It's maybe a little bit easier.
like (y2-y1)/(x2-x1) * (x-x1) *y1. Shorter, less to calculate.
Nice work :-)
Beautiful stuff
Thanks!
i love your intro
Thank you :-) there's also an extended version if curious ua-cam.com/video/33YwKcVQ1vw/v-deo.html
please explain me how to get the uTop, mt calculation desn't work
Amazing video, as always. One question: is the drawing of the car need to be done by us, or did I miss something?
waiting for the next video
You mean looking for a little picture of a car, and then simply replace..
ctx.rect(....)
with..
ctx.drawImage(img, sx,sy,sw,sh, dx,dy,dw,dh)
???
i believe in you! you can do it! ;-)
@@fleckenfurz77 ok, thank you. i'll try.
Thanks! About the car, I did post one challenge on my community tab where the goal was to make a car, but I will eventually show you how to draw it and how to make them have random colors (it will be the final lecture).
@@Radu Thank you! I'll wait for that lesson. Meanwhile, I'm really curoius about the ML part, I'm wainting for it!.
@@ap666-o8h Glad to hear. I've put the most effort into the Neural Network lecture. Should be out on 15th of April (on my birthday :-D) if all goes according to plan.
Hello. Can I get the code source of this video?
Sorry, haven't saved this one since it doesn't really do anything...
for the calculation of uTop, why is there a A.x - B.x instead of B.x - A.x? I followed the same steps as the steps for getting t and can't seem to get this result
A.x - B.x is same as - (B.x - A.x). Could it be that you have another minus (-) sign somewhere that cancels this one out?
@@Radu Thanks for the reply. Yeah I figured it out. I checked and the subtraction in the divisor of my u equation is actually reversed. So I had to multiply both side by -1 and got the same answer.
Great!
28:00
Slove the system ecuation composed of both linear equations d1:ax+by+c=0 and d2:ax+by+c=0, and you get the intersection point , but first test the slope for paralel lines. at least that is what I've tried.
You can do that. Have to also consider vertical lines (slope is undefined then).
out of curiosity is the function lerp is same as y = mx+t ?
What you're writing here is the line equation. And lerp, as the name says: 'linear' interpolation is also a line, yes.
But in lerp m is defined as a difference between two end values, say: t and w (with your naming convention).
So y=(w-t)x+t.
That's because lerp is supposed to give you values for y within the interval [t,w] with values for x in [0,1].
You can also use x outside of the [0,1] range and then it's called extrapolation.
@@Radu Thanks
@@basiccoder2166 No problem!
Excellent. The only thing I would suggest is adding a link to the code.
Yeah, unfortunately it got deleted somehow 🤣
Need to follow him...
:-)
You are an angle ,,,🎉❤😊
:-)
you are awesome
Thanks :-) and thanks for watching!
Rock lee reference. 🔥🔥🔥🔥🔥🔥🔥🔥
:-) yeah, one of my favorite scenes in the whole series.
is there a github to get the source files ? thanks
You can get the code for getIntersection from the self driving car repository: github.com/gniziemazity/Self-driving-car
But the code of this whole demonstration is not online.
19:55
What about it?
love it!
Cool! Thanks :-)
pleaseeee help me understand how to get th uTop. i cant do it. i get the wrong answer all the time idkw
Hard to help in the comments... just keep in mind that the result you get can be different as long as it is equivalent. Like -(B-A) is the same as (A-B), but a minus (-) must be there.
@@Radu yes, that waz the problem, thank you radu.
I'm so happy i found your channel😇
@@eitanbegayev9290 thanks for watching!
Thanks for the wonderful content, very much appreciated.
I just have a question which is killing me, you didn't initialize the variable myCanvas and somehow it is working with you, hooow?😃
All IDs defined using HTML are by default global variables. I don't do this very often because its easy to overwrite those if not careful... I don't recommend that practice, but for something like this, where we're not really building anything real, it's ok.
Good question! I think not many people know this!
@@Radu wow this is new to me, I have an experience for like 4 years now and this is the first time to know this.
Thanks again for the explanation and for the content.
@@osamaabozahra Maybe better that way :-) it's not a good practice, I think. And, I'm sure that after 4 years you know many other things I don't!
Keep it up and thanks for watching!
Cyprien fait des maths. Love it.
Who is Cyprien? :-)
@@Radu ua-cam.com/video/b5Q1K7jzZN4/v-deo.html
:-))
now i realize i was always using lerp to generate random number between min and max value😅
Cool :-) you probably know lerp very well in that case... probably just didn't know it has a name.
Valeu!
E de bine? :-D
Yes, Master, this class was awesome!@@Radu
Merci :-)
bro has 654 missed calls from microsoft
I hope not :-/ I worked for them a while back and missed calls might mean I did something bad :-D
How is it possible to select the canvas on the id without a query selector? What is this sorcery! And thanks for great videoes - don't hold the math =)
They are globals, by default, but I don't recommend doing this. They can easily get overwritten.
*How did you do that !???*
😯😲😳🤯
Do what exactly? :-)
@@Radu
I really love and admire your thinking and explaining capabilities. You are JUST amazing !
I am continuously watching your videos from last 3 days and feel JS is easy and fun.
Understanding mathematical expressions taking some time, though. 🤭
@@Xoundz Thank you :-) happy to hear you're learning!
How to select rectangle and resize rectangle please help me. I made drawing app
Can you share some sample code you're working with? I'm not sure I understand your problem...
If the two segments overlap, there are parallel but collision occurs.
True, would you implement this differently?
@@Radu If the segments overlap, I would return a point of intersection anywhere in the region of overlap, maybe using both segments current direction of movement to set a collision point that's "happening first". What's your take on this? Was it intentional to not trigger a collision with overlapping segments?
@@rmsv Well, if they overlap there is no collision 'point' but a collision 'segment'... so proper way would be to return that. Now, I couldn't possibly return all (infinitely many) points that form that segment :-) so maybe returning it as a kind of interval that represents all points within the range. I don't know :-| another possible way would be to return nothing... if intersection would mean that one segment must have one endpoint on one side and another on the other (which overlapping segments do not have). I think this is actually the case in this video. Would have to check the code again to verify and I'm not at the office, so I'll probably forget :-D
Holy Shit 😂. Can you teach me mathematics?
Probably, since you watched this video, I already did :-D
is the dropping of hand weights in your intro supposed to be a resemblance to Rock Lee from Naruto?
:-))) yes...
My head is gonna explode
You find it difficult? Which part is causing you trouble?
@@Radu The part of getting back to math, even tho it's basic math I'm not used to think at all, I did the uTop alone and got the same as you (glad for that). I do got questions for this project on C#, as I'm stuck in this part specificaly, whenever I try to create my lines, I put them in a Canvas but for some reason they all get messed up, the orientations isn't good and half of the lines get hidden for some unknown reason to me. It does work with Rectangles, except I don't know how to get their coordinates inside the Canvas. Thanks for the lessons, I watched everything twice now. I'm doing an Automation bachelor degree and A.I. is something I want to get to know, in it's raw state.
@@zVOLT220z Glad to hear you find this useful. If you're having trouble with doing it in C# you could try sharing the code (like maybe on my Discord) and I'll have a look.
Coding with radu la la la
Haha :-) here is the extended intro, if curious: ua-cam.com/video/33YwKcVQ1vw/v-deo.html
@@Radu noice : )
@@Radu love this intro
:-))
Fain
Merci :-)
Here I transcribed the code: (But I made some minor changes to fit my needs, but It works and you might find this helpfull)
let canvas = document.getElementById("canvas")
let ctx = canvas.getContext("2d")
let angle=0;
const A={x:200, y:150};
const B={x:150, y:250};
const C={x:50, y:100};
const D={x:250, y:200};
const mouse={x:0,y:0};
document.onmousemove=(event)=>{
mouse.x=event.x;
mouse.y=event.y;
}
function lerp(A,B,s)
{
return A+(B-A)*s
}
let t=-1;
let u = -1;
function drawDot(point,label,isOutside)
{
ctx.beginPath()
ctx.fillStyle=isOutside?"red":"white";
ctx.arc(point.x,point.y,10,0,Math.PI*2);
ctx.fill()
ctx.stroke();
ctx.fillStyle="black";
ctx.textBaseline="middle";
ctx.font="bold 14px Arial"
ctx.textAlign="center"
ctx.fillText(label,point.x,point.y);
}
function animate()
{
const radius=50;
A.x=mouse.x+Math.cos(angle)*radius;
A.y=mouse.y-Math.sin(angle)*radius;
B.x=mouse.x-Math.cos(angle)*radius;
B.y=mouse.y+Math.sin(angle)*radius;
angle+=0.02;
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.beginPath();
ctx.moveTo(A.x, A.y);
ctx.lineTo(B.x, B.y)
ctx.moveTo(C.x, C.y);
ctx.lineTo(D.x, D.y)
ctx.stroke();
drawDot(A,"A",false);
drawDot(B,"B",false);
drawDot(C,"C",false);
drawDot(D,"D",false);
let M={
x:lerp(A.x,B.x,t),
y:lerp(A.y,B.y,t)
}
let N={
x:lerp(C.x,D.x,u),
y:lerp(C.y,D.y,u)
}
const I=getIntersection(A,B,C,D);
if (I) {drawDot(I,"I",false)}
//ctx.beginPath();
//ctx.rect(canvas.width/2,0,I.bottom/100,10);
//ctx.fill();
drawDot(N,"N",u1);
u+=0.005;
drawDot(M,"M",t1);
t+=0.005;
requestAnimationFrame(animate);
}
function getIntersection(A,B,C,D){
const vTop=(D.x-C.x)*(A.y-C.y)-(D.y-C.y)*(A.x-C.x);
const uTop=(C.y-A.y)*(A.x-B.x)-(C.x-A.x)*(A.y-B.y);
const bottom=(D.y-C.y)*(B.x-A.x)-(D.x-C.x)*(B.y-A.y);
if(bottom!=0){
const v=vTop/bottom;
const u=uTop/bottom;
if(v>=0 && v=0 && u
Thanks!