Draw rectangles, circles or arbitrary polygons in a m x n matrix

Viewed 1654

I want to simulate the flow around objects in two dimensions. Therefore I wrote a program in C which uses the Navier-Stokes equations to describe the motion of fluids. Now I came to the point where I actually want more than just placing a rectangle in the simulation domain. To draw such a rectangle I just do something like:

for(int i=start_x; i<end_x; i++)
    for(int j=start_y; j<end_y; j++)
        M[i][j] = 1; // barrier cell = 1

Doing this I get a nice rectangle. No surprise. But what would be an approach if I want to simulate the flow around a circle, a cross, a triangle, a wing profile or any other arbitrary polygon? Is there an easy way to draw such 2D objects in a matrix M of size m x n?


I just found an easy way to draw almost any shape I want. The answer of @Nominal Animal inspired me to find this solution. I just use a .png file and convert it to a .pgm file using the command convert picture.png picture.pgm (using Linux). In my code I only need a few more lines:

FILE *pgmFile;
pgmFile = fopen("picture.pgm", "r");
for(int i=0; i<1024; i++){
    for(int j=0; j<1024; j++){
        int d = fgetc(pgmFile);
        if(d < 255){
            M[i][j] = 1; // barrier cell = 1
        }
    }
}
fclose(pgmFile);

Here I use a picture of 1024 x 1024 pixels. If the value of the pixel is smaller than 255 (not white) than I set the pixel of M[i][j] to 1. Here is a result I made with the Stack Overflow logo (flux is coming from the left): enter image description here

Velocity plot, Re = 20000 (Reynolds number)

5 Answers
Related