2
$\begingroup$

I'm working with a Graphics object in Mathematica and I want to apply a transformation matrix to the entire object, including its axes.

Here's a simplified version of my code:

graphics=Graphics[{}, Axes -> True]
matrix={{1/Sqrt[2], -(1/Sqrt[2])}, {1/Sqrt[2], 1/Sqrt[2]}}
TheNeededFunctionThatTransformsGraphicsByMatrix[graphics,matrix] (* Should output a rotated axes. *)

Is there such a function in Mathematica?

$\endgroup$
5
  • $\begingroup$ ViewMatrix reference.wolfram.com/legacy/language/v13/ref/… $\endgroup$ Commented Dec 30, 2023 at 9:33
  • $\begingroup$ Or simply Rotate[Graphics[{}, Axes -> True], \[Pi]/4]. $\endgroup$ Commented Dec 30, 2023 at 9:43
  • $\begingroup$ Thanks! ViewMatrix seems to be a option that only can be applied to a Graphics3D object. $\endgroup$ Commented Dec 30, 2023 at 9:55
  • $\begingroup$ Do you need the output to be a Graphics[...] expression? Will it be OK to transform the graphics to an Image[...]? $\endgroup$
    – xzczd
    Commented Dec 30, 2023 at 10:15
  • $\begingroup$ @benjaminchanming: In 3D you can draw to the plane z=0 as if in 2D and then you can use ViewMatrix. $\endgroup$ Commented Dec 30, 2023 at 11:38

2 Answers 2

4
$\begingroup$

Using ImagePerspectiveTransformation:

... and the matrix from the docs:

g = Graphics[{Darker@Cyan, RegularPolygon[5]}
  , Axes -> True
  , AxesStyle -> Yellow
  , Background -> Black
  , PlotRange -> All
  , Frame -> True
  ]

matrix = {{1, 1/3, 0}, {1/3, 1, 0}, {0, 1/2, 1}}
ImagePerspectiveTransformation[Rasterize@g, matrix]

enter image description here

$\endgroup$
3
$\begingroup$
Rotate[Graphics[{}, Axes -> True], π/4]
$\endgroup$
1
  • $\begingroup$ Rotate works! But is there a more general function that do any linear transformation represented by a matrix? $\endgroup$ Commented Dec 30, 2023 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.