ContourLine
#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的更多相关文章
- ArcGIS Runtime for Android 使用异步GP服务绘制等值线
关于基于Android上ArcGIS Server GP服务的调用,已经有前辈给出了很好的例子: http://blog.csdn.net/esrichinacd/article/details/92 ...
- (转)ArcGIS Runtime for Android 使用异步GP服务绘制等值线
关于基于Android上ArcGIS Server GP服务的调用,已经有前辈给出了很好的例子: http://blog.csdn.net/esrichinacd/article/details/92 ...
随机推荐
- 如何去除AJAX收到数据中包含的html页面数据
问题: 如下代码所示,我用AJAX收到来自url: 'kzkj_check.jsp',返回的数据msg,总是包含页面的html数据,可是我只想要我返回的数据“false”, $.ajax({ url: ...
- ios缩放图片
http://blog.csdn.net/yanfangjin/article/details/7456681
- Slide-out Sidebar Menu
IOS学习之路十(仿人人滑动菜单Slide-out Sidebar Menu) 2013-09-03 22:13 by lixingle, 270 阅读, 0 评论, 收藏, 编辑 最近滑动菜单比较流 ...
- ngx-push-stream模块源码学习(五)——内存清理
1.定时器 采用nginx自身的定时器管理机制,具体细节待学习过nginx源码后加以补充 2.channel的生成周期 (0).初始(诞生) 发布.订阅均有可能产生ch ...
- Arduino 各种模块篇 震动模块 vibrator
vibrator is a good thing. it has multi-funtionality . :) Now the vibrator we choose is the one whic ...
- 重复弹Toast的解决方案
虽然网上有很多了,还是记录一下吧, 解决思路:不用计算Toast的时间之类的,就是定义一个全局的成员变量Toast, 这个Toast不为null的时候才去make,否则直接setText.为了按返回键 ...
- Linux:用at和crontab调度作业
一.有2种作业调度方式 1.突发性的,就是只运行作业一次而不是定期运行,使用at命令. 例如在进程A运行一段时间后关闭该进程. 2.定期运行,就是每隔一定的周期运行一次,使用crontab命令. 如每 ...
- android sqlite使用之模糊查询数据库数据的三种方式
android应用开发中常常需要记录一下数据,而在查询的时候如何实现模糊查询呢?很少有文章来做这样的介绍,所以这里简单的介绍下三种sqlite的模糊查询方式,直接上代码把: package com.e ...
- 使用CAShapeLayer绘图
之前讲过使用UIBezierPath在UIView的drawRect中绘图, 今天我们讲下另外一种方式: CAShaperLayer 先说说使用CAShapeLayer的优点: GPU执行, GPU执 ...
- 使用 IDEA 创建 Maven Web 项目 (三)- 编写一个简单的 WEB 应用
编写 Servlet 类 首先,需要在 java 目录下,创建一个名为 org.smart4j.chapter1 的包.然后,在该包下创建一个 HelloServlet 的类,代码如下: packa ...