使用easyX库
easyX库的安装尤其简单,在官网下载程序后,点击使用后安装到相关IDE上即可。
easyX库的使用和Graphic类相似,甚至比它还要简单,首先初始化一个界面:
initgraph(width, height);
然后指定背景颜色,填充背景等:
setbkcolor(WHITE);
cleardevice();
setlinecolor(BLACK);
setfillcolor(RED);
然后就可以划线了,这里花了一个二维坐标轴:
line(30, height - 30, width - 30, height - 30);
line(30, 30, 30, height - 30);
line(width - 45, height - 45, width - 30, height - 30);
line(width - 45, height - 15, width - 30, height - 30);
line(15, 45, 30, 30);
line(45, 45, 30, 30);
最后用画填充圆的方式画点:
for (int i = 0; i < lineNum; i++) {
solidcircle((points.front().x - xsmall)*0.1 + 50, height - (0.1*(points.front().y - ysmall) + 50), 5);
points.pop_front();
}
这里要注意循环的截止不能是points.size()
,因为points每次pop_front()
后size
都会减少。
同时这里的0.1
和50
等数据都是为了让点能在界面中间显示,为了美观。
最后还需要注意窗口坐标系(暂且给它这个名字)的原点是定义在左上角的,向右为x轴,向下为y轴。
最后显示结果
最后实现代码
int width = 700;
int height = 500;
initgraph(width, height);
setbkcolor(WHITE);
cleardevice();
setlinecolor(BLACK);
setfillcolor(RED);
line(30, height - 30, width - 30, height - 30);
line(30, 30, 30, height - 30);
line(width - 45, height - 45, width - 30, height - 30);
line(width - 45, height - 15, width - 30, height - 30);
line(15, 45, 30, 30);
line(45, 45, 30, 30);
list<double>::iterator biggest = max_element(begin(pointsx), end(pointsx));
list<double>::iterator smallest = min_element(begin(pointsx), end(pointsx));
double xsmall = *smallest;
double len_x = *biggest - *smallest;
list<double>::iterator biggest2 = max_element(begin(pointsy), end(pointsy));
list<double>::iterator smallest2 = min_element(begin(pointsy), end(pointsy));
double ysmall = *smallest2;
double len_y = *biggest2 - *smallest2;
for (int i = 0; i < lineNum; i++) {
solidcircle((points.front().x - xsmall)*0.1 + 50, height - (0.1*(points.front().y - ysmall) + 50), 5);
points.pop_front();
}
getchar();