//alter load_map.dev
//safety verion 2016/1/12
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include<sstream> //使用istringstream必须包含的头文件
#include<string>
#include "string2num.hpp"
#include "map.hpp"
#include <windows.h>
#include <GL/glut.h>
using namespace std; void test_map(){ unsigned int sum;double Sa;
double north=polys[]->north();
double south=polys[]->south();
double east=polys[]->east();
double west=polys[]->west();
for(int i=;i<polys.size();i++){
sum+=polys[i]->points.size();
Sa+=polys[i]->area();
}
for(int i=;i<polys.size();i++){
//比较每一个polygon的边界值,求出整个地图的四个边界值
if(polys[i]->north()>=north)north=polys[i]->north();
if(polys[i]->south()<=south)south=polys[i]->south();
if(polys[i]->east()>=east)east=polys[i]->east();
if(polys[i]->west()<=west)west=polys[i]->west();
} ofstream out("map_para.txt");
if(out.is_open())
{
out <<"map parameter:\n";
out<<"count polygon="<<polys.size()<<endl;
out<<"size="<<sum<<endl;
out<<"area="<<Sa<<endl;
out<<"north="<<north<<endl;
out<<"south="<<south<<endl;
out<<"east="<<east<<endl;
out<<"west="<<west<<endl;
out.close();
}
} void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
//用蓝色色绘制各省边界
glColor3f (0.0, 0.0, 1.0);
glPolygonMode(GL_BACK, GL_LINE);
for(int i=;i<polys.size();i++)
{
vector<MapPoint> points=polys[i]->points;
glBegin(GL_LINE_STRIP);
for(int j=;j<points.size();j++)
{
glVertex3f (points[j].longitude, points[j].latitude, 0.0);
}
glEnd();
}
glFlush();
}
void init (void)
{
//设置背景颜色
glClearColor (, 1.0, , 0.0);
//初始化观察值
glMatrixMode(GL_PROJECTION); //将矩阵模式设为投影
glLoadIdentity(); //对矩阵进行单位化
glOrtho(110.0, 118.0, 30.0, 38.0, -1.0, 1.0); //构造平行投影矩阵
} int main(int argc, char *argv[]){
//数据文件请到http://files.cnblogs.com/opengl/HenanCounty.rar下载放到D盘根目录下并解压
string filename="HenanCounty.txt";//在当前工程目录下
// ReadData2num(filename);
polys=ReadMapData(filename);
test_map(); glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); //单缓存和RGB
glutInitWindowSize (, );
glutInitWindowPosition (, );
glutCreateWindow ("Map_henan");
init ();
glutDisplayFunc(display); //显示回调函数
glutMainLoop(); return ;
}
 
"HenanCounty.txt"是一份文本格式的地图:

单个整数代表点数(包含的点可能是某个省内市区的范围),整数n下面紧接着的数据行共有n行,是以经纬度表示的地理坐标。
程序运行结果:

关于地图的信息(多边形数目、总点数、面积、边界)通过test_map()函数写入map_para.txt

目前算得的面积还有问题,这通过边界值就可以看出来,这还有待解决。

string2num.hpp定义了模板函数用于字符串形式的数字向基本数值类型的转化(其实这个定义比较多余<sstream>里面定义的字符串处理类包含此功能)

#ifndef _STRING2NUM_HPP_
#define _STRING2NUM_HPP_
#include<sstream> //使用istringstream必须包含的头文件
#include<string>
using namespace std;
//模板函数:将string类型变量转换为常用的数值类型 by maowei
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss>>num;
return num;
} #endif

map.hpp包括了基本类的定义,这是在map_origin.cpp的基础上修改得到的,主要是增加了一些获取地图信息相关的函数。其中多边形容器的定义部分值得充分学习消化。

#ifndef _MAP_HPP_
#define _MAP_HPP_
#include <vector>
#include <math.h>
using namespace std;
class MapPoint
{
public:
double longitude;//经度
double latitude;//纬度
MapPoint(){}
MapPoint(double x,double y){longitude=x;latitude=y;}
};
class Map
{
public:
int mapsize;
vector<MapPoint> points; //多边形的顶点序列
Map(){}
Map(int i){mapsize=i;}
}; //unsigned int count,num;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%translate from origin
class Polygon
{
public:
vector<MapPoint> points; //多边形的顶点序列
double area(void){
double A=;unsigned int N=points.size();
for(int i=;i<(N-);i++){
A+=fabs(points[i].longitude*points[i+].latitude-points[i].latitude*points[i+].longitude);
}
A+=fabs(points[N-].longitude*points[].latitude-points[N-].latitude*points[].longitude);
return A/;
}
double north(void){
double N=points[].latitude;
for(int i=;i<points.size();i++){if(points[i].latitude>=N)N=points[i].latitude; }
return N;
}
double south(void){
double S=points[].latitude;
for(int i=;i<points.size();i++){if(points[i].latitude<=S)S=points[i].latitude; }
return S;
}
double east(void){
double E=points[].longitude;
for(int i=;i<points.size();i++){if(points[i].longitude>=E)E=points[i].longitude; }
return E;
}
double west(void){
double W=points[].longitude;
for(int i=;i<points.size();i++){if(points[i].longitude<=W)W=points[i].longitude; }
return W;
}
};
vector<Polygon*> polys; //多边形集合
vector<Polygon*> ReadMapData(const string filename)
{
int PointCount;
vector<Polygon*> polygons;
ifstream fs(filename.c_str());
while(fs.eof()!=true)
{
Polygon* poly=new Polygon;
fs>>PointCount;
// cout<<PointCount<<endl;
for(int i=;i<PointCount;i++)
{
MapPoint p;
fs>>p.longitude>>p.latitude;
poly->points.push_back(p);
}
polygons.push_back(poly); }
return polygons;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Map ReadData2num(const string str) //逐行读取 并转化为常用数据类型 by mememagic
{
cout<<"start read map"<<endl;
ifstream fin(str.c_str());
if (! fin.is_open())
{ cout << "Error opening file"; exit (); }
string s; Map Imap;
int Size=,i=;bool flag;
while( getline(fin,s) )
{
if(i==Size){int s_i=stringToNum<int>(s);Size=s_i+;i=;//count+=s_i;num++;
//cout << "(vertex num): " << s_i << endl;
}
else
{
istringstream is(s);//采用istringstream从string对象str中读取字符
string s1;double x,y;
while(is>>s1){
double s_d=stringToNum<double>(s1);
if(!flag){x=s_d;flag=!flag; }
else{y=s_d;flag=!flag; }
//cout<<s_d<<' ';
}
//cout<<"x="<<x<<' '<<"y="<<y<<endl;
MapPoint p(x,y);
Imap.points.push_back(p);
}
i++;
}
return Imap;
} #endif

与Draw_v1相比,这个程序有了不少变化:数据来源不再靠手工在命令窗口输入,而是一个有确定“格式”的简单文本,数据量也较大,程序里面对数据的处理代码自然不同;多边形容器(vector<Polygon*> polys )是对图形(vector<Shape>)容器的拓展;利用OpenGL实现了数据的可视化(关于OpenGL绘图的原理有待进一步学习理解).

												

Draw_extend使用OpenGL显示数据点的更多相关文章

  1. ZingChart 隐藏数据点

    正常情况下 zingChart 的数据点会显示到图表中,但是如果数据点很多的情况下,可能会让你无法准确的预测趋势,而且也不美观 在 js 配置中添加最多允许显示的数据点,超过这个值将不显示数据点 效果 ...

  2. 理解数据点,自变量和因变量(参数和值)ChartControl

    WinForms Controls > Controls > Chart Control > Fundamentals > Charting Basics > Under ...

  3. Keil UV4 BUG(带字库液晶不能显示“数、正、过”问题的请看)

    Keil UV3一直存在汉字显示(0xFD)的bug,以前在用到带字库的12864液晶的时候,“数”字总是不能正常显示,后来有网友告诉我这是keil的bug,解决掉了.后来keil升级了,我也换了新版 ...

  4. OpenGL显示图片

    最近想用C++在windows下实现一个基本的图像查看器功能,目前只想到了使用GDI或OpenGL两种方式.由于实在不想用GDI的API了,就用OpenGL的方式实现了一下基本的显示功能. 用GDAL ...

  5. 第12课 OpenGL 显示列表

    显示列表: 想知道如何加速你的OpenGL程序么?这一课将告诉你如何使用OpenGL的显示列表,它通过预编译OpenGL命令来加速你的程序,并可以为你省去很多重复的代码. 这次我将教你如何使用显示列表 ...

  6. android linphone中opengl显示的实现

    1,java层 在界面中创建GL2JNIView(基类为GLSurfaceView). 创建对象AndroidVideoWindowImpl,将GL2JNIView作为参数传入构造函数.在该对象中监听 ...

  7. VS+OpenGl 显示三维STL模型 代码

    今天调出了用VS环境结合OpenGL glut工具包进行显示STL模型的模块,进行了渲染.效果: 如下,后期会进行进一步优化,先贴上: #ifndef DATA_H #define DATA_H st ...

  8. [记录]使用openGL显示点云的一个程序

    #include <GL/glut.h> #include <stdio.h> #include <iostream> using namespace std; v ...

  9. OPENGL 显示BMP图片+旋转

    VS2010/Windows 7/ 1. 需包含头文件 stdio.h, glaux.h, glut.h.需要对应的lib,并添加包含路径 2. 窗口显示用glut库的函数 3. bmp图片从本地读取 ...

随机推荐

  1. WCF初探-26:WCF中的会话

    理解WCF中的会话机制 在WCF应用程序中,会话将一组消息相互关联,从而形成对话.会话”是在两个终结点之间发送的所有消息的一种相互关系.当某个服务协定指定它需要会话时,该协定会指定所有调用(即,支持调 ...

  2. node.js 学习

    http://www.cnblogs.com/haogj/category/612022.html

  3. [WebServer] Windows操作系统下 Tomcat 服务器运行 PHP 的环境配置

    前言: 由于本人在开发和学习过程中需要同时部署 JavaWeb 和 PHP 项目,于是整理了网上的一些相关资料,并结合自己的实际操作,记录于此,以供参考. 一.环境(64bit): 1.操作系统.To ...

  4. 使用CSS3线性渐变实现图片闪光划过效果

    <p class="overimg"> <a><img src="http://www.nowamagic.net/librarys/ima ...

  5. 【JavaScript】固定布局轮播图特效

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Java冒泡随笔

    package homework; import java.util.Scanner; public class ArraySort { /** * @param args */ public sta ...

  7. 【初级】linux mv 命令详解及使用方法实战

    mv:移动文件或者将文件改名 前言: mv是move的缩写,顾名思义是移动.它的功能既能移动文件/文件夹,又可以用来改名,经常用来做文件的备份,比如再删除之前,先给文件做备份(保护数据)也是linux ...

  8. linux环境下安装tomcat并配置tomcat日志分割

    1.直接解压apache-tomcat-7.0.69.tar.gz 存放在/home目录下 根据需要自定义tomcat名称 mv apache-tomcat-7.0.69 Tomcat7 2.解压cr ...

  9. AngularJs的UI组件ui-Bootstrap分享(十四)——Carousel

    Carousel指令是用于图片轮播的控件,引入ngTouch模块后可以在移动端使用滑动的方式使用轮播控件. <!DOCTYPE html> <html ng-app="ui ...

  10. 写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”)

    public class Test { public void index() { String strWords = "Hello World My First Unit Test&quo ...