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 cam
There might be more efficient ways of doing this, but here's one way.
Define a function in C using the equation of the polygon you wish to draw. The function is defined such that it accepts a point coordinates, and returns whether the point lies inside the polygon or not. For example, for a circle, the function could accept the point (x,y)
, the centre (x0,y0)
, and the radius r
, and return (x-x0)^2 + (y-y0)^2 - r^2 < 0
. Let this function be f
.
Determine the bounding box rectangle of the polygon, if possible, or else, the smallest rectangle you can determine which completely encloses the polygon. This will give you a rectangular matrix.
Now, iterate over the points in the rectangular matrix. For each point, call the function you previously defined. Assign the coordinate a 1
if it returns True, and 0
if it returns False. This will construct the polygon.
Suppose you want to draw a circle with centre (x0,y0)
, radius r
, then you can use:
int f(int i, int j, int x0, int y0, int r)
{
return pow((i-x0),2) + pow((j-y0),2) - pow(r,2) < 0;
}
for(int i = x0-r; i <= x0 + r; i++)
{
for(int j = y0-r; j <= y0 + r; j++)
{
if(f(i,j,x0,y0,r))
{
M[i][j] = 1;
}
else
{
M[i][j] = 0;
}
}
}