jingwood / d2dlib

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.
MIT License
234 stars 40 forks source link

Inverse Matrix #35

Closed Unreal852 closed 3 years ago

Unreal852 commented 3 years ago

Is it possible to invert matrix ? ( C# lib ). Actually i'm drawing rectangles, and using Translate and Scale to allow Pan & Zoom. Thats working, but i can't calculate mouse position because the rectangles positions changed via the Matrix translate but there is no way to also translate mouse pos (or i didn't find how).

jingwood commented 3 years ago

I have added an example code to do the hit-test using an inverse transformation. You may have to create a completed transformation system to draw objects and perform the hit-test for every objects.

I think an common method is to make objects have transform property and its inverse. e.g.:

To draw the object:

g.setTransform(obj.Transform);
g.drawRect(...);

To hit-test point:

var transformedPoint = obj.InverseTransform.TransformPoint(p);
bool isHitted = rect.ContainsPoint(transformedPoint);

I have also added two new APIs SetTransform and GetTransform to set and get a transform into Direct2D context. You can get a transform during drawing object and calculate the inverse for hit-test. The Direct2D receives a 3x2 matrix which can be simply converted from a 3x3 matrix.

Here is the example code

Unreal852 commented 3 years ago

Working like a charm ! Thanks