Home > other >  Very specific question to mouse-movement and coordinate translation (C# / Win-Forms)
Very specific question to mouse-movement and coordinate translation (C# / Win-Forms)

Time:09-27

I'm working on a small painting program in which I can use the mouse to move/resize the shapes with handles on the corners. This already works well, except when the shape is rotated.

I need a translation between X- and Y-Coordinates. I've tried some sine/cosine calculations, but without success. Either I have fundamental errors in my formulas or the changes for X/Y in the MouseMove event are too small for this calculation.

Does anyone have experience with this topic or perhaps a few good links (maybe with examples)?

Thx in advance, Peter

CodePudding user response:

Avoid using angles as much as possible, prefer to use transforms, i.e. matrices.

One example of this would be system.numerics.matrix3x2, this has methods to create transforms from angles, translations, scaling etc. Some important properties of matrices is that you can combine them and invert them. In addition to simply using them to transform a point.

It is also often useful to draw a matrix to visualize the transform, i.e. multiply a zero-vector, the x unit vector and y unit vector with the matrix, and draw some lines between these points, that should give you a good visualization of what the transform does.

While not absolutely required, some linear algebra knowledge is very useful when doing things like this.

  • Related