Draw Cosx with specific period width

2019-08-29 17:19发布

问题:

how can i draw a period of Cosx with width =100 and height 50?

my recent code is very hard to control them :(

// this to find y with each x to draw Cos
for (int i = 0, j = 0; i < 50; i += 1, j++)
{
    int y = (int)((Math.Cos((double)i * height/10F * Math.PI / cy) + 1.0) * (cx - 1) / widtd/10F);
    poi.SetValue(new Point(i, y),j);  // poi is an aray of point
}

回答1:

Based on OP's last comment I assume "width" means a single period. Therefore to calculate the points of a cosine function with amplitude amp and a period of period looks like this:

int amp = 50, period = 100;
Point[] poi = new Point[period];
for (int x = 0; x < period; x++)
{
    int y = (int)(amp * Math.Cos(x * 2 * Math.PI * (1.0 / period)));
    poi[x] = new Point(x, y);
}

Note that this is a "1:1" calculation, i.e. one point is one pixel.



回答2:

I found solution here:

string filename = @"D:\test.bmp";
        int width = 200;
        int height = 300;
        Bitmap b = new Bitmap(width, height);

        for (int i = 0; i < width; i++)
        {
            int y = (int)((Math.Cos((double)i * 2.0 * Math.PI / width) + 1.0) * (height - 1) / 2.0);
            b.SetPixel(i, y, Color.Black);
        }
        b.Save(filename);