thinkpad20

Sierpinski Curve drawing program in Qt/OpenGL

Jul 3rd, 2012
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #include "curvedisplay.h"
  2. #include <GL/glu.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. CurveDisplay::CurveDisplay(QWidget *parent) :
  7.     QGLWidget(parent)
  8. {
  9.     sierpinski_level = 0;
  10.     dist0 = 2.5;
  11.     x = 0.0;
  12.     y = 0.0;
  13.     display_grid = true;
  14. }
  15.  
  16. void CurveDisplay::initializeGL(){
  17.     glShadeModel(GL_SMOOTH);
  18.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  19.     glClearDepth(1.0f);
  20.     glEnable(GL_DEPTH_TEST);
  21.     glDepthFunc(GL_LEQUAL);
  22.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  23.  
  24. }
  25.  
  26. void CurveDisplay::resizeGL(int width, int height){
  27.     double xMin = 0, xMax = 10, yMin = 0, yMax = 10;
  28.     glViewport(0, 0, (GLint)width, (GLint)height);
  29.  
  30.     glMatrixMode(GL_PROJECTION);
  31.     glLoadIdentity();
  32.     gluOrtho2D(-1,1,-1,1);
  33.     if (width > height){
  34.         height = height?height:1;
  35.         double newWidth = (xMax - xMin) * width / height;
  36.         double difWidth = newWidth - (xMax - xMin);
  37.         xMin = 0.0 + difWidth / 2.0;
  38.         xMax = 10 + difWidth / 2.0;
  39.     } else {
  40.         width = width?width:1;
  41.         double newHeight = (yMax - yMin) * width / height;
  42.         double difHeight = newHeight - (yMax - yMin);
  43.         yMin = 0.0 + difHeight / 2.0;
  44.         yMax = 10 + difHeight / 2.0;
  45.     }
  46.     gluOrtho2D(xMin, xMax, yMin, yMax);
  47.     glMatrixMode(GL_MODELVIEW);
  48.     glLoadIdentity();
  49. }
  50.  
  51.  
  52. void CurveDisplay::paintGL(){
  53.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  54.     glLoadIdentity();
  55.  
  56.     //draw grid
  57.     if (display_grid) drawSomething();
  58.  
  59.     //draw sierpinski
  60.     paint();
  61.  
  62. }
  63.  
  64. void CurveDisplay::drawSomething()
  65. {
  66.     glLineWidth(1);
  67.     glColor3f(0.9,0.9,0.9);
  68.     for (int i = 1; i<21; i++){
  69.         //draw vertical lines
  70.         glBegin(GL_LINE_LOOP);
  71.         glVertex2f((GLfloat)i/2, (GLfloat)0);
  72.         glVertex2f((GLfloat)i/2, (GLfloat)10);
  73.         glEnd();
  74.  
  75.         //draw horizontal lines
  76.         glBegin(GL_LINE_LOOP);
  77.         glVertex2f((GLfloat)0,(GLfloat)i/2);
  78.         glVertex2f((GLfloat)10,(GLfloat)i/2);
  79.         glEnd();
  80.     }
  81. }
  82.  
  83. void CurveDisplay::goToXY(double x_new, double y_new)
  84. {
  85.     glTranslated(x_new, y_new, 0.0);
  86. }
  87.  
  88. void CurveDisplay::drawLine(double x1, double y1, double x2, double y2)
  89. {
  90.     glBegin(GL_LINES);
  91.     glVertex2f((GLfloat)x1, (GLfloat)y1); // origin of the line
  92.     glVertex2f((GLfloat)x2, (GLfloat)y2); // ending point of the line
  93.     glEnd();
  94. }
  95.  
  96. void CurveDisplay::lineRel(double deltaX, double deltaY) {
  97.     drawLine(x, y, x+deltaX, y+deltaY);
  98.     x += deltaX;
  99.     y += deltaY;
  100. }
  101.  
  102. void CurveDisplay::paint() {
  103.     glColor3f(0,0,0);
  104.     dist = dist0;
  105.     for (int i=sierpinski_level; i > 0; i--) dist /= 2;
  106.     goToXY(2*dist, dist);
  107.     sierpA(sierpinski_level);        // start recursion
  108.     lineRel(+dist, +dist);
  109.     sierpB(sierpinski_level);        // start recursion
  110.     lineRel(-dist, +dist);
  111.     sierpC(sierpinski_level);        // start recursion
  112.     lineRel(-dist, -dist);
  113.     sierpD(sierpinski_level);        // start recursion
  114.     lineRel(+dist, -dist);
  115. }
  116.  
  117. void CurveDisplay::sierpA (int level) {
  118.     if (level > 0) {
  119.         sierpA(level-1);
  120.         lineRel(+dist, +dist);
  121.         sierpB(level-1);
  122.         lineRel(+2*dist, 0);
  123.         sierpD(level-1);
  124.         lineRel(+dist, -dist);
  125.         sierpA(level-1);
  126.     }
  127. }
  128.  
  129. void CurveDisplay::sierpB (int level) {
  130.     if (level > 0) {
  131.         sierpB(level-1);
  132.         lineRel(-dist, +dist);
  133.         sierpC(level-1);
  134.         lineRel(0, +2*dist);
  135.         sierpA(level-1);
  136.         lineRel(+dist, +dist);
  137.         sierpB(level-1);
  138.     }
  139. }
  140.  
  141. void CurveDisplay::sierpC (int level) {
  142.     if (level > 0) {
  143.         sierpC(level-1);
  144.         lineRel(-dist, -dist);
  145.         sierpD(level-1);
  146.         lineRel(-2*dist, 0);
  147.         sierpB(level-1);
  148.         lineRel(-dist, +dist);
  149.         sierpC(level-1);
  150.     }
  151. }
  152.  
  153. void CurveDisplay::sierpD (int level) {
  154.     if (level > 0) {
  155.         sierpD(level-1);
  156.         lineRel(+dist, -dist);
  157.         sierpA(level-1);
  158.         lineRel(0, -2*dist);
  159.         sierpC(level-1);
  160.         lineRel(-dist, -dist);
  161.         sierpD(level-1);
  162.     }
  163. }
  164.  
  165. void CurveDisplay::displayGrid(int choice)
  166. {
  167.     if (choice == 0) display_grid = false;
  168.     else display_grid = true;
  169.     updateGL();
  170. }
  171.  
  172. void CurveDisplay::changeLevel(int new_level){
  173.     sierpinski_level = new_level;
  174.     updateGL();
  175. }
Advertisement
Add Comment
Please, Sign In to add comment