#define MULTI_PLOT true //Determine whether or not to plot multiple iterations.

#define X_MAX 1.0   // Define extent of reference plane, used in call to gluOrtho2D(...)
#define Y_MAX 1.0
#define X_MIN -1.0
#define Y_MIN -1.0
#define N_X 640 // Number of cells in x and y dimensions
#define N_Y 480
#define OX (-1 + i * dx)
#define OY (1 - j * dy) #include <stdio.h>
#include <GL/glut.h>
#include <iostream>> double h = 0.0; // Height of reference plane in which contour is to be plotted -- f(x,y)=h
double dx = 2 / (double)(N_X - 1);
double dy = 2 / (double)(N_Y - 1); double data[N_X][N_Y]; // Array to hold function values (a,b,c,d) at corners of each cell
double f(double x, double y); // Function whose contour is to be plotted
int cell(double a, double b, double c, double d); // Helper ftn to determine cell type from f corner values
void lines(int, int, int, double, double, double, double); // Helper ftn to draw correct line in a cell
void display(); void multidisplay(){
glClear(GL_COLOR_BUFFER_BIT); h = 0;
for (; h < 1; h += .01){
display();
}
}
void display() // Display callback function - called when window appears; drawing done here
{
if (!MULTI_PLOT) glClear(GL_COLOR_BUFFER_BIT); // form data array from function
for (int i = 0; i < N_X; i++)
for (int j = 0; j < N_Y; j++)
data[i][j] = f(OX, OY); // double loop to process each cell (loop through each cell i,j; determine cell type, draw line in cell)
int k = 0;
for (int i = 0; i < N_X - 1; i++){
for (int j = 0; j < N_Y - 1; j++){
int c = cell(data[i][j], data[i + 1][j], data[i][j + 1], data[i + 1][j + 1]); // return cell type c (0-15)
lines(c, i, j, data[i][j], data[i + 1][j], data[i][j + 1], data[i + 1][j + 1]); // draw correct line
k++;
}
} glFlush();
} double f(double x, double y) // Function to be plotted, example: Ovals of Cassini
{
double a = 0.48, b = 0.5;
return (x*x + y*y + a*a)*(x*x + y*y + a*a) - 4 * a*a*x*x - b*b*b*b;
} void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(X_MIN, X_MAX, Y_MIN, Y_MAX);
glMatrixMode(GL_MODELVIEW);
} int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);
glutCreateWindow("Oval of Cassini Contour plot");
init();
glutDisplayFunc(MULTI_PLOT ? multidisplay : display); // White drawing color
glClearColor(0.0, 0.0, 0.0, 1.0); // Black background
glColor3f(1.0, 1.0, 1.0);
glutMainLoop();
} int cell(double a, double b, double c, double d) // Determine and return cell type
{
int n = 0;
if (a > h) n += 1;
if (b > h) n += 8;
if (c > h) n += 4;
if (d > h) n += 2;
return n;
} void draw_one(int n, int i, int j, double a, double b, double c, double d){
double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
switch (n){
case 1:
case 14: x1 = OX;
y1 = OY - (dy * ((h - a) / (c - a)));
x2 = OX + (dx * ((h - a) / (b - a)));
y2 = OY;
//printf("1: (%f, %f)\n", (h - a) / (d - a), (h - a) / (b - a));
break;
case 4:
case 11: x1 = OX;
y1 = OY - (dy * ((h - a) / (c - a)));
x2 = OX + (dx * ((h - c) / (d - c)));
y2 = OY - dy;
//printf("2: (%f, %f)\n", (h - c)/(b - c), (h - c)/(a - c));
break;
case 2:
case 13: x1 = OX + dx;
y1 = OY - (dy * ((h - b) / (d - b)));
x2 = OX + (dx * ((h - c) / (d - c)));
y2 = OY - dy;
// printf("3: (%f, %f)\n", (h - d) / (a - d), (h - d) / (c - d));
break;
case 7:
case 8: x1 = OX + dx;
y1 = OY - (dy * ((h - b) / (d - b)));
x2 = OX + (dx * ((h - a) / (b - a)));
y2 = OY;
// printf("4: (%f, %f)\n", (h - b) / (c - b), (h - b) / (d - b));
break;
}
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glEnd();
} void draw_opposite(int n, int i, int j, double a, double b, double c, double d){
double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
switch (n){
case 5:
case 10: y1 = OY;
x1 = OX + (dx * ((h - a) / (b - a)));
y2 = OY - dy;
x2 = OX + (dx * ((h - c) / (d - c)));
break;
case 6:
case 9: x2 = OX;
y1 = OY - (dy * ((h - b) / (d - b)));
x1 = OX + dx;
y2 = OY - (dy * ((h - a) / (c - a)));
break;
}
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glEnd(); } void lines(int n, int i, int j, double a, double b, double c, double d) // Draw correct line
{
// Your code goes here
// n is cell type (0-15)
// i,j specifes which cell
// a,b,c,d are function values at cell corners (from data array)
switch (n){
case 1:
case 2:
case 4:
case 7:
case 8:
case 11:
case 13:
case 14:
draw_one(n, i, j, a, b, c, d);
break;
case 5:
case 6:
case 9:
case 10: draw_opposite(n, i, j, a, b, c, d);
break;
case 0:
case 15: break;
default:
//exit(2);
break;
}
}

ContourLine的更多相关文章

  1. ArcGIS Runtime for Android 使用异步GP服务绘制等值线

    关于基于Android上ArcGIS Server GP服务的调用,已经有前辈给出了很好的例子: http://blog.csdn.net/esrichinacd/article/details/92 ...

  2. (转)ArcGIS Runtime for Android 使用异步GP服务绘制等值线

    关于基于Android上ArcGIS Server GP服务的调用,已经有前辈给出了很好的例子: http://blog.csdn.net/esrichinacd/article/details/92 ...

随机推荐

  1. cocos2d学习之路四(添加遥控杆)

    添加遥控杆 1. 首先需要再HelloWorldLayer.h中包含ZJoystick.h文件 并且让其实现ZJoystickDelegate协议 2.打开HelloWorldLayer.mm文件实现 ...

  2. tomcat创建一个windows服务

    具体步骤如下: 1.把JDK解压到C:\Program Files\Java下,Tomcat解压到D:\tomcat下 2.配置环境变量 JAVA_HOME:C:\Program Files\Java ...

  3. 第一个python

    import MySQLdb import os,sys import string dto=os.getcwd()+"\\dto" dao=os.getcwd()+"\ ...

  4. 不想作死系列---virtualbox最小化安装centos6.5

    背景: 最近已经重装了5个系统,实在不想折腾了.于是打算在虚拟机中安装所需环境. 系统版本: 宿主机:win7 virtualbox4.3.10 centos 6.5(final) 1.下载安装vir ...

  5. 基于支持向量机的车牌识别-- opencv2.4.7+vs2012环境搭建

    环境说明: 环境: OS:win7 sp1 opencv:2.4.7 vs2012 搭建过程: PS:机器上原本已安装vs2012 1.opencv 1.1 下载,直接双击安装即可. 此处我的安装位置 ...

  6. 线程内唯一对象HttpContext

    在asp.net中,HttpContext是主线程内唯一对象.在web应用中开启多线程,在另外一个线程中是无法访问HttpContext. 如果需要在另外一个线程中使用HttpContext.Curr ...

  7. JDBC开发

    1.JDBC简介 )数据库驱动 )Sun公司为简化数据库开发,定义了一套jdbc接口,这套接口由数据库厂商去实现,这样,开发人员只需要学习jdbc接口,并通过jdbc加载具体的驱动,就可以操作数据库. ...

  8. [置顶] 使用严苛模式打破Android4.0以上平台应用中UI主线程的“独断专行”

    传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 已经有好一段时间没有关注Android应用方面的事情了:)最近单位来了一个Androi ...

  9. HDU 1880 字符串hash 入门题

    Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...

  10. ajax获取后台传递的json数据

      最近在使用JQuery的ajax方法时,需要返回的数据为json数据,在success返回中数据处理会根据返回方式不同会采用不同的方式来生成json数据.在$.ajax方法中应该是如何来处理的,简 ...