ncase / sight-and-light Goto Github PK
View Code? Open in Web Editor NEWA programming tutorial for sight & light
A programming tutorial for sight & light
If the lines are crossing then the effect is not correct and it's not mentioned anywhere.
Hi there.
First of all I'd like to thank you for an awesome tutorial! I love how I can examine and play with the drafts one by one when slowly approaching the final version.
When implementing the visibility script on my own, I found two things that could be enhanced.
1) Vertical ray causing a division by zero
I found out that there is a little bug in your code. The problem is that when you create a strictly vertical ray (r_dx = 0
), you are dividing by zero when calculating the T1
param of an intersection.
// Plug the value of T2 to get T1
T1 = (s_px+s_dx*T2-r_px)/r_dx
How I fixed the code myself is that when the r_dx
is approaching zero, I'm calculating the T1
param from the y
components instead:
T1 = (s_py+s_dy*T2-r_py)/r_dy
As r_dx
and r_dy
cannot be both 0 at the same time (otherwise it wouldn't be a ray, but a point), it fixes the problem quite well.
2) Faster parallelity check
Right now you're using square roots to check if the ray and line segment are parallel.
// Are they parallel? If so, no intersect
var r_mag = Math.sqrt(r_dx*r_dx+r_dy*r_dy);
var s_mag = Math.sqrt(s_dx*s_dx+s_dy*s_dy);
if(r_dx/r_mag==s_dx/s_mag && r_dy/r_mag==s_dy/s_mag){
// Unit vectors are the same.
return null;
}
I dunno how exactly that thing is working, but you don't really need to complicate it that much. Two lines are parallel if and only if their direction vectors are parallel. Vectors are parallel if one can be written as a k
multiple of the other one. Which means:
(a,b) is parallel to (x,y) <=> a/x == b/y <=> a*y == b*x
So an enhanced parallelity check that doesn't need calculating square roots is this:
// If lines are parallel
if (r_dx * s_dy == r_dy * s_dx) {
return null; // they do not intersect
}
Hi, I was wandering if except for polygons this will also work with circles, and if yes, what code is required?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.