Draw Cosx with specific period width

天大地大妈咪最大 提交于 2019-12-25 01:14:57

问题


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);


来源:https://stackoverflow.com/questions/27643839/draw-cosx-with-specific-period-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!