Giter Site home page Giter Site logo

sight-and-light's People

Contributors

ncase avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sight-and-light's Issues

Crossing Lines

If the lines are crossing then the effect is not correct and it's not mentioned anywhere.

2 fixes: Vertical ray division by zero + Better parallelity check

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
}

Circle obstacle

Hi, I was wandering if except for polygons this will also work with circles, and if yes, what code is required?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.