Giter Site home page Giter Site logo

Interesting Conclusion about tinyrenderer HOT 3 OPEN

ssloy avatar ssloy commented on July 17, 2024
Interesting Conclusion

from tinyrenderer.

Comments (3)

vchizhov avatar vchizhov commented on July 17, 2024

The idea is to make the code not use divisions and float. Note that if you have an inequality: a*b/c + d >= 0, then as long as c>0 you can turn it into: a*b + c*d >=0.

from tinyrenderer.

yasirtug avatar yasirtug commented on July 17, 2024

I remember having a hard time trying to grasp that part until eventually losing my trust to the writer and opening this issue angrily. Now I look again, I understand how the improvement is made and unfortunately, the explanation is really shallow and somewhat irrelevant to the change that is made.

'Taking the division out of the loop', after 'noticing that each division has the same divisor', would be just by calculating 1/{common divisor} beforehand and then multiplying the dividend with this value in the loop. Something like this (compare with 3rd attempt):

float divisor = 1/(float)(x1-x0);
for (int x=x0; x<=x1; x++) { 
    float t = (x-x0)*divisor;
}

But the change writer made (4th attempt) does this:

float derror = std::abs(dy/float(dx));
float error = 0;
for (int x=x0; x<=x1; x++) {
    error += derror;
} 

The improvement made here (not multiplying at every step) is possible not only because of the common divisor, also because of unmentioned proportional increase of dividend(x-x0) in the loop. Actually, it is more about the latter and one only needs to see that t variable in 3rd attempt is increasing by the same amount on every iteration, to easily implement the same optimization.

Whatever. My guess is, writer already had the resulting snippets (maybe from classes) and showed some impatience while filling the gaps. I also think the resulting explanation has something to do with trying to be smart (sorry, this costed me too much time and i am angry again).

from tinyrenderer.

vchizhov avatar vchizhov commented on July 17, 2024

While I do agree with your general feeling, we should also be fair considering who this tutorial was supposedly meant for. The important point being that the algorithm was most likely meant to be derived by readers (you can find my derivation here: https://github.com/vchizhov/ssloy_software_renderer/blob/master/src/line_bresenham.hpp). The algorithm description in wikipedia is also quite helpful (a bit different from mine, but ultimately equivalent): https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#Algorithm_for_integer_arithmetic

from tinyrenderer.

Related Issues (20)

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.