C++ 正弦和余弦图表绘图仪…某些算法错误C++;
所以我在这里创建了这个图表绘制程序。然而,我有一些小故障。首先,我应该描述一下这个程序是如何工作的。基本上使用stdscr和创建一个新的win,我必须创建一个“函数生成器”程序。字符将打印在模拟正弦和余弦波形的屏幕上。当角色向下滚动窗口时,窗口将滚动。其中一个窗口将包含框架(页眉、行、页脚)。另一个窗口将滚动并打印正弦和余弦波形。我已经检查了我的代码很长一段时间了,不管我有什么毛病,我都搞不清楚 如果你在想象这个程序时仍然有困难,那么想想乒乓球吧……顶部和底部有两条垂直线,然后中间有一条线。在这个屏幕上,正弦波和余弦波将被打印出来 我的错误是,sin和cosine没有按我想要的方式打印……它们似乎超出了Newwin窗口的范围。另外,将从10-1指定的页脚将不会移动到我希望它在页脚中的位置C++ 正弦和余弦图表绘图仪…某些算法错误C++;,c++,plot,trigonometry,C++,Plot,Trigonometry,所以我在这里创建了这个图表绘制程序。然而,我有一些小故障。首先,我应该描述一下这个程序是如何工作的。基本上使用stdscr和创建一个新的win,我必须创建一个“函数生成器”程序。字符将打印在模拟正弦和余弦波形的屏幕上。当角色向下滚动窗口时,窗口将滚动。其中一个窗口将包含框架(页眉、行、页脚)。另一个窗口将滚动并打印正弦和余弦波形。我已经检查了我的代码很长一段时间了,不管我有什么毛病,我都搞不清楚 如果你在想象这个程序时仍然有困难,那么想想乒乓球吧……顶部和底部有两条垂直线,然后中间有一条线。在这
#include <curses.h>
#include <math.h>
#include "fmttime.h"
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
enum colors
{
BLACK,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE,
};
void Sim_Advance(int degrees); // Function prototype declarations
double Sim_Cos();
double Sim_Sin();
void frame(const char* title, int gridcolor, int labelcolor);
void mark(WINDOW* window, char ch, int color, double value);
static const double PI = (3.1415927/180); // Degrees to radian factor
static double PA = 0; // Phase angle
static int x; // X dimension of screen
static int y; // Y dimension of screen
static int delay1 = 300000; // 300ms Delay
struct timeval tv; // Timeval object declaration
int main(void)
{
initscr(); // Curses.h initilizations
cbreak();
nodelay(stdscr, TRUE);
noecho();
int keyhit;
int ctr = 1; // Exit flag
int degrees = 10; // Interval to be added to phase
int tempcounter = 1;
char buf[32]; // Buffer being sent to formattime
size_t len = sizeof(buf); // Size of buffer being passed
gettimeofday(&tv, NULL); // calling function for epoch time
formatTime(&tv, buf, len); // Calling formaTime for timestamp
getmaxyx(stdscr,y,x);
WINDOW* Window = newwin(y-4, x-2, 2, 5);
scrollok(Window, TRUE);
char cTitle[] = {"Real time Sine/ Cosine Plot"}; // Title string for plot
while (ctr == 1) // This will run the program till
{ // exit is detected (CTRL-X)
usleep(delay1/6); // Delays program execution
keyhit = getch();
mark(Window,'C', WHITE, Sim_Cos());
mark(Window,'S', RED, Sim_Sin());
Sim_Advance(degrees); // Advances PA by "degrees" value (10)
if (tempcounter == 1)
{
frame(cTitle, WHITE, RED); // Prints out the frame once
--tempcounter;
}
if (keyhit == 24)
{
ctr = 0; // Exit flag set
}
}
endwin();
return 0;
}
// Function will advance the Phase angle by assigned value of degrees
// The value of degrees must be an int and must be in radians
void Sim_Advance(int degrees)
{
PA = PA + degrees;
if (PA >= 360)
{
PA = PA - 360;
}
}
// Calculates the Cos of the Phase angle and returns value to main
double Sim_Cos()
{
double PARad; // Need to convert degrees into Radian
PARad = (PA*PI);
return cos(PARad);
}
// Calculates the Sin of the Phase angle and returns value to main
double Sim_Sin()
{
double PARad2; // Variable to hold radian value
PARad2 = (PA*PI);
return sin(PARad2);
}
// Will print the grid and Axis of the display as well as a title for the display
// with variable background and/or foreground colors
void frame(const char* title, int gridcolor, int labelcolor)
{
int offset = 27/2; // Middle of string
int col = 0;
int row = 0;
int botline = 0;
start_color();
init_pair(0, labelcolor, BLACK);
init_pair(1, gridcolor, BLACK);
wmove(stdscr, 0, (x / 2)- offset);
wprintw(stdscr,"%s\n", title);
wrefresh(stdscr);
while (row != x) // This prints the top line
{
attrset(COLOR_PAIR(1));
wprintw(stdscr,"-");
wrefresh(stdscr);
++row;
}
while (col != y-4) // This prints the middle line
{
wmove(stdscr, col + 2, x/2);
wprintw(stdscr, "|\n");
wrefresh(stdscr);
++col;
}
while (botline != x) // Prints the bottom line
{
wprintw(stdscr, "-");
wrefresh(stdscr);
++botline;
}
attrset(COLOR_PAIR(0));
wmove(stdscr, y, 0); // These three things commands
wprintw(stdscr, "-1"); // Will print out the proper footer
wrefresh(stdscr);
wmove(stdscr, y, x/2);
wprintw(stdscr, "0");
wrefresh(stdscr);
wmove(stdscr, y, x);
wprintw(stdscr, "1");
wrefresh(stdscr);
}
// Will print out the characters passed to it in the designated
// window to which it points to.
void mark(WINDOW* window, char ch, int color, double value)
{
int cursep = (((x/2) * value) + (x/2)); // Prints character from middle
int currenty = getcury(window); // of screen
wmove(window, currenty+1, cursep-3); // Moves cursor to desired location
wrefresh(window);
usleep(delay1);
waddch(window, ch);
wrefresh(window);
}
#包括
#包括
#包括“fmttime.h”
#包括
#包括
#包括
枚举颜色
{
黑色
红色
绿色
黄色的,
蓝色
洋红
青色,
白色
};
无效模拟前进(整数度);//函数原型声明
双Sim_Cos();
双simu_Sin();
空框(常量字符*标题,int gridcolor,int labelcolor);
无效标记(窗口*窗口、字符、整型颜色、双值);
静态常数双π=(3.1415927/180);//弧度系数
静态双PA=0;//相位角
静态int x;//屏幕的X尺寸
静态int y;//屏幕的Y尺寸
静态整数延迟1=300000;//300毫秒延迟
结构timeval tv;//Timeval对象声明
内部主(空)
{
initscr();//Curses.h初始化
cbreak();
节点延迟(stdscr,真);
noecho();
int键击;
int ctr=1;//退出标志
int degrees=10;//要添加到阶段的间隔
int tempcounter=1;
char buf[32];//正在发送到formattime的缓冲区
size\u t len=sizeof(buf);//正在传递的缓冲区大小
gettimeofday(&tv,NULL);//调用历元时间的函数
formatTime(&tv,buf,len);//调用formatTime获取时间戳
getmaxyx(stdscr,y,x);
WINDOW*WINDOW=newwin(y-4,x-2,2,5);
scrollok(窗口,TRUE);
字符cTitle[]={“实时正弦/余弦绘图”};//绘图的标题字符串
while(ctr==1)//这将运行程序直到
{//检测到退出(CTRL-X)
usleep(delay1/6);//延迟程序执行
keyhit=getch();
标记(窗口,'C',白色,Sim_Cos());
标记(窗口,S,红色,Sim_Sin());
Sim_前进(度);//按“度”值前进PA(10)
if(tempcounter==1)
{
帧(cTitle,白色,红色);//打印帧一次
--温度计数器;
}
if(keyhit==24)
{
ctr=0;//已设置退出标志
}
}
endwin();
返回0;
}
//函数将通过指定的度数值推进相位角
//度的值必须是整数,并且必须以弧度为单位
无效模拟提前(整数度)
{
PA=PA+度;
如果(PA>=360)
{
PA=PA-360;
}
}
//计算相角的Cos,并将值返回给主
双Sim_Cos()
{
double PARad;//需要将度转换为弧度
PARad=(PA*PI);
返回cos(PARad);
}
//计算相角的Sin并将值返回给main
双辛
{
double PARad2;//用于保存弧度值的变量
PARad2=(PA*PI);
回归罪(天堂2);
}
//将打印显示器的网格和轴以及显示器的标题
//具有可变背景和/或前景颜色
空框(常量字符*标题、整型gridcolor、整型labelcolor)
{
int offset=27/2;//字符串中间
int col=0;
int行=0;
int-botline=0;
启动颜色();
初始对(0,labelcolor,黑色);
初始对(1,gridcolor,黑色);
wmove(stdscr,0,(x/2)-偏移量);
wprintw(stdscr,“%s\n”,标题);
wrefresh(stdscr);
while(row!=x)//这将打印顶行
{
属性集(颜色对(1));
wprintw(stdscr,“-”);
wrefresh(stdscr);
++行;
}
while(col!=y-4)//这将打印中间线
{
wmove(stdscr,col+2,x/2);
wprintw(stdscr,“\n”);
wrefresh(stdscr);
++上校;
}
while(botline!=x)//打印底线
{
wprintw(stdscr,“-”);
wrefresh(stdscr);
++博特林;
}
属性集(颜色对(0));
wmove(stdscr,y,0);//这三个命令
wprintw(stdscr,“-1”);//将打印出正确的页脚
wrefresh(stdscr);
wmove(stdscr,y,x/2);
wprintw(stdscr,“0”);
wrefresh(stdscr);
wmove(stdscr,y,x);
wprintw(stdscr,“1”);
wrefresh(stdscr);
}
//将以指定的格式打印传递给它的字符
//它所指向的窗口。
无效标记(窗口*窗口、字符、整型颜色、双值)
{
int cursep=((x/2)*值)+(x/2));//从中间打印字符
int currenty=getcury(窗口);//屏幕的
wmove(窗口,当前Y+1,光标P-3);//将光标移动到所需的位置
int cursep = (((x / 2) * value) + (x / 2));
void mark() {
...
// value = -1.0 --> cursep = 0
// value = +1.0 --> cursep = x - 1
// ** Adjust these 4 vars as needed to re-map your translation **
double val_min = -1.0;
double val_max = +1.0;
int cur_min = 0;
int cur_max = x - 1;
// y = (y1-y0)/(x1-x0)*(x-x0) + y0
double cur_d = (cur_max - cur_min)/(val_max - val_min)*(value - val_min) + cur_min;
int cursep = floor(cur_d + 0.5);