I have to make a matrix which contains elements in an ellipse shaped region, but I need to make ellipse region in such a way that larger axis of ellipse is inclined with angle θ with horizontal. With Colorize the matrix should look like this:

I have employed two ways so far for this:

  • Use Graphics, followed by MorphologicalComponents and then use Position to get a list and then convert it into matrix.

  • Write the equation of an inclined ellipse as f[x,y] = 0 and then use condition f[x,y] <= 0 and run it on matrix coordinates to get the list and then finally convert it into matrix.

However, these two methods are a bit slow and I would like to use something like DiskMatrix or any other way to make it faster.

  • 1
    $\begingroup$ Doesn't MorphologicalComponents[ Rasterize[Graphics[Disk[{0, 0}, {2, 1}]], ImageSize -> 800]] give a matrix? $\endgroup$
    – acl
    Aug 5, 2014 at 16:23

3 Answers 3


One way, probably not the cleanest:

gr = Graphics[Rotate[Disk[{0, 0}, {4, 2}], Pi/6], ImageSize -> 250];
a  = Image[gr, "Bit", ColorSpace -> "Grayscale"] // ImageData;
a // Colorize

enter image description here

You can control the border size using PlotRangePadding or ArrayPad, for Graphics or absolute scaling.

This is not slow:

gr = Graphics[Rotate[Disk[{0, 0}, {4, 2}], Pi/6], ImageSize -> 5000];

Timing[a = Image[gr, "Bit", ColorSpace -> "Grayscale"] // ImageData;]

{0.140401, Null}

{3714, 5000}

By comparison:

DiskMatrix[2154] // Dimensions // Timing
{0.187201, {4309, 4309}}

So my method is faster than DiskMatrix for an output of the same size.

Binarize can be used directly on a Graphics expression but it appears to not be as fast.


For a worst case scenario when your graphics broke down, and you are forced to work only with numbers

f[x_, y_, a_, b_] := (x + y)^2/a^2 + (x - y)^2/b^2
R = 10;
mat=Table[If[f[m, n, 2, 3] < R^2, Style[X, Red], O], {m, -2 R,2 R}, {n, -2 R, 2 R}];
Grid[mat, Spacings -> {0, 0}]

Change R, a, b to change the shape.

enter image description here

Its probably not very practical way, just added to give some different flavour!

Controlling the angle

The orientation can be controlled by the angle q.

f[x_, y_, a_, b_, q_] := (x Cos[q] + y Sin[q])^2/a^2 + (y Cos[q] - x Sin[q])^2/b^2
R = 10;
mat[a_, b_, q_] := Table[If[f[m, n, a, b, q] < R^2, Style[X, Red], O], {m, -2R,2 R}, {n, -2 R, 2 R}]

fig = Table[{Style[q, 20, Bold], Grid[mat[1, 2, q], Spacings -> {0, 0}]}, {q,0, Pi/2, Pi/6}];

enter image description here

  • $\begingroup$ The inclusion of styling in the matrix generation itself obfuscates and slows the code. I suggest refactoring this to produce a pure binary matrix first, for comparison to other methods. $\endgroup$
    – Mr.Wizard
    Aug 5, 2014 at 17:04
  • $\begingroup$ indeed @Mr.Wizard, it goes like a turtle. $\endgroup$
    – Sumit
    Aug 6, 2014 at 9:15

Credit goes to @acl

   Rasterize[Graphics[{Rotate[Disk[{0, 0}, {4, 2}], Pi/6]}], 
       ImageSize -> 30] /. (0) -> Style[0, Red] // MatrixForm

enter image description here

  • $\begingroup$ FYI: this is about 4.5X slower than the code in my answer. $\endgroup$
    – Mr.Wizard
    Aug 5, 2014 at 16:49
  • $\begingroup$ note Rasterize does not (evidently) have an option to make a 1-bit image, so you end up with gray values at the edges of the ellipse. You could use Round instead of MorphologicalComponents however. (still a tad slower than Image ). $\endgroup$
    – george2079
    Aug 5, 2014 at 19:40

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.