Question

I have a problem with my C++ code where Im doing two steps in one with regards to pythagoras.

Im getting the length of a and b at the same time as doing the pythagoras function but my variable always equals 0 even though the X and Y points show up with their respective values.

Unfortunatly due to this when square rooting the resut it procceds with an error as you cannot square root 0.

It looks kinda complicated but this was the neatest I could get it

hypPrevious = ( 
                ( *(previousCalcp->getX()) - *(currentCalcp->getX()) ) 
                *   
                ( *(previousCalcp->getX()) - *(currentCalcp->getX()) )
              ) 
              +
              ( 
                ( *(currentCalcp->getY()) - *(previousCalcp->getY()) ) 
                *   
                ( *(currentCalcp->getY()) - *(previousCalcp->getY()) )
              );
Was it helpful?

Solution

With the information you gave us, the best advice I can give you is rewriting your code and add a debugging line to check what is going wrong:

int x1 = *(previousCalcp->getX());
int y1 = *(previousCalcp->getY())
int x2 = *(currentCalcp->getX());
int y2 = *(currentCalcp->getY());
int dx = x1 - x2;
int dy = y1 - y2;
hypPrevious = dx*dx + dy*dy;
std::cout << "(" << x1 << "," << y1 << ") and (" << x2 << "," << y2 << ") resulted in " << hypPrevious << std::endl;

Don't forget to change the type int to whatever you are using in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top