从文件中读取绘图点,然后缩放

Reading plot points from a file and then scaling

本文关键字:然后 缩放 绘图 文件 读取      更新时间:2023-10-16

我正试图用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;
}