0

In many different types of drawing software, such as MS Paint, when the user holds shift while drawing a line, it snaps in 45 degree increments. I was wondering how it calculates this?

Here's an example.

New contributor
Graham Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • simple, for 45° just check if abs(dx) == abs(dy), for 90° check dx == 0 || dy == 0 – phuclv Mar 26 at 6:14
  • It depends on the software. One way to do it, is get only the X or Y difference of mouse cursor from start position, and apply that to both X and Y. Moving the mouse in one direction moves it diagonally. – LPChip Mar 26 at 7:32
  • How Windows draw lines docs.microsoft.com/en-au/windows/win32/gdi/lines. The reason it's limited to 15 degrees if to make it easy for the user to draw level lines – Mark Mar 27 at 1:21