3.6.2 编程实例-河南地图绘制

程序有关介绍请参考图书3.6.1节内容

程序运行结果如下图:

image

 

 1 #include <iostream>
 2 #include <fstream>
 3 #include<vector>
 4 #include <GL/glut.h>
 5 using namespace std;
 6 
 7 class MapPoint
 8 {
 9 public:
10     double longitude; 
11     double latitude;
12 };
13 class Polygon
14 {
15 public:
16     vector<MapPoint> points; //多边形的顶点序列
17 };
18 vector<Polygon*> polys; //多边形集合
19 vector<Polygon*> ReadMapData(char* filename)
20 {
21     int PointCount;
22     vector<Polygon*> polygons;
23     ifstream fs(filename);
24     while(fs.eof()!=true)
25     {
26         Polygon* poly=new Polygon;
27         fs>>PointCount;
28         cout<<PointCount<<endl;
29         for(int i=0;i<PointCount;i++)
30         {
31             MapPoint p;
32             fs>>p.longitude>>p.latitude;
33             poly->points.push_back(p);
34         }
35         polygons.push_back(poly);
36 
37     }
38     return polygons;
39 }
40 void display(void)
41 {    
42     glClear (GL_COLOR_BUFFER_BIT);    
43     //用蓝色色绘制各省边界
44     glColor3f (0.0, 0.0, 1.0);
45     glPolygonMode(GL_BACK, GL_LINE);     
46     for(int i=0;i<polys.size();i++)
47     {        
48         vector<MapPoint> points=polys[i]->points;
49         glBegin(GL_LINE_STRIP);
50         for(int j=0;j<points.size();j++)
51         {            
52             glVertex3f (points[j].longitude, points[j].latitude, 0.0);        
53         }
54         glEnd();
55     }    
56     glFlush();
57 }
58 void init (void)
59 {
60     //设置背景颜色
61     glClearColor (1.0, 1.0, 1.0, 0.0);
62     //初始化观察值
63     glMatrixMode(GL_PROJECTION);    //将矩阵模式设为投影
64     glLoadIdentity();                 //对矩阵进行单位化
65     glOrtho(110.0, 118.0, 30.0, 38.0, -1.0, 1.0);   //构造平行投影矩阵
66 }
67 int main(int argc, char** argv)
68 {
69     //数据文件请到http://files.cnblogs.com/opengl/HenanCounty.rar下载放到D盘根目录下并解压
70     char* filename="D:/HenanCounty.txt";
71 
72     polys=ReadMapData(filename);    
73     glutInit(&argc, argv);
74     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  //单缓存和RGB
75     glutInitWindowSize (500, 500);
76     glutInitWindowPosition (100, 100);
77     glutCreateWindow ("地图绘制");
78     init ();
79     glutDisplayFunc(display);     //显示回调函数
80     glutMainLoop();
81     return 0; 
82 }

附上本实验的VC++工程代码(VC++2008),已含地图数据文件,放在工程文件夹中,与上述程序路径不一样。

posted on 2014-06-16 09:34  慢步前行  阅读(2427)  评论(2编辑  收藏  举报

导航