[ad_1]

My GUI application implements interactive user control using arcball.

```
case SDL_MOUSEBUTTONDOWN:
{
mousepressed = true;
p = project(event.motion.x, event.motion.y);
break;
}
case SDL_MOUSEBUTTONUP:
{
mousepressed = false;
lastQ = currentQ * lastQ;
currentQ = Quaternion<double>(true);
break;
}
case SDL_MOUSEMOTION:
{
if (mousepressed)
{
q = project(event.motion.x, event.motion.y);
n = CrossProduct(p, q);
theta = std::acos(p * q / (p.Magnitude() * q.Magnitude()));
currentQ = Quaternion<double>(n, theta);
}
break;
}
```

The problem is that the program sometimes crashes. I believe the problem lies in this line `theta = std::acos(p * q / (p.Magnitude() * q.Magnitude()));`

. I think sometimes product of magnitude of vectors `p`

and `q`

equal to almost zero and when the dot product of these vectors gets divided with this value, my program throws the divide by zero exception. I’m not sure how to modify this line. Any ideas?

[ad_2]