从文件中读取绘图点,然后缩放
Reading plot points from a file and then scaling
我正试图用OpenGL在c++中编写一个程序,从文件中读取数据并缩放然后绘制数据。
文件中的数据设置如下:
<>之前0.017453293 tab 2.01623406 不相上下0.087266463 tab 2.056771249 不相上下0.191986218 tab 2.045176705 不相上下0.27925268 tab 1.971733548 不相上下之前,其中tab
表示x坐标,par
表示y坐标。
我写的代码似乎不工作,虽然。
#include "stdafx.h"
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include "glut.h"
#include <iostream>
#include <string>
#include <fstream>
int _tmain(int argc, char **argv) {
void myInit(void);
void myDisplay(void);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("CMPS389 HW2");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
void myInit(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
std::string words = "";
float locX;
float locY;
std::ifstream fileN("curveData.txt");
while(fileN != NULL) {
fileN>>words;
if (words == "par") {
fileN>>locX;
}
if (words == "tab") {
fileN>>locY;
glBegin(GL_LINES);
glVertex2f(locX*300, locY*300);
glEnd();
}
glFlush();
}
}
你真的需要削减开支了。我只关注文件解析部分。有一种方法可以解决这个问题。注意,下面的代码不检查tab或par后缀。如果你真的需要,你可以自己添加。
#include <fstream>
#include <iostream>
#include <string>
#include <iterator>
struct position
{
double x;
double y;
};
std::istream& operator>>(std::istream& in, position& p)
{
std::string terminator;
in >> p.x;
in >> terminator;
in >> p.y;
in >> terminator;
return in;
}
int main()
{
std::fstream file("c:\temp\testinput.txt");
std::vector<position> data((std::istream_iterator<position>(file)), std::istream_iterator<position>());
for(auto p : data)
std::cout << "X: " << p.x << " Y: " << p.y << "n";
system("PAUSE");
return 0;
}
相关文章:
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 计算缩放多边形的比例,得到给定的多边形面积
- QwtPlot具有相等的轴和自动缩放
- 如何在directx/c++中进行平移/缩放操作
- 在Qt中实现无限可缩放的画布
- 是否可以为 QPixmap 派生类嵌入缩放方法?
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何在OpenGL(GLFW,很高兴)中进行2D缩放?
- 改变或缩放两个正态分布以具有特定的相关系数
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 如何在OpenGL中正确旋转和缩放对象?
- 仅当类型为 std::complex 时,才进行缩放
- 将 VS Code 用于跨平台可缩放C++项目
- 根据帧速率缩放/缩小数字
- 在 c++ 中缩放浮点值
- 如何防止 DirectX C++程序的拉伸/缩放
- 使用提升几何缓冲区缩放多边形时的冗余折点
- 缩放QGrahicsView时,mousewheentturn首先滚动,然后缩放
- 从文件中读取绘图点,然后缩放