Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "curvedisplay.h"
- #include <GL/glu.h>
- #include <iostream>
- using namespace std;
- CurveDisplay::CurveDisplay(QWidget *parent) :
- QGLWidget(parent)
- {
- sierpinski_level = 0;
- dist0 = 2.5;
- x = 0.0;
- y = 0.0;
- display_grid = true;
- }
- void CurveDisplay::initializeGL(){
- glShadeModel(GL_SMOOTH);
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- }
- void CurveDisplay::resizeGL(int width, int height){
- double xMin = 0, xMax = 10, yMin = 0, yMax = 10;
- glViewport(0, 0, (GLint)width, (GLint)height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(-1,1,-1,1);
- if (width > height){
- height = height?height:1;
- double newWidth = (xMax - xMin) * width / height;
- double difWidth = newWidth - (xMax - xMin);
- xMin = 0.0 + difWidth / 2.0;
- xMax = 10 + difWidth / 2.0;
- } else {
- width = width?width:1;
- double newHeight = (yMax - yMin) * width / height;
- double difHeight = newHeight - (yMax - yMin);
- yMin = 0.0 + difHeight / 2.0;
- yMax = 10 + difHeight / 2.0;
- }
- gluOrtho2D(xMin, xMax, yMin, yMax);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void CurveDisplay::paintGL(){
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- //draw grid
- if (display_grid) drawSomething();
- //draw sierpinski
- paint();
- }
- void CurveDisplay::drawSomething()
- {
- glLineWidth(1);
- glColor3f(0.9,0.9,0.9);
- for (int i = 1; i<21; i++){
- //draw vertical lines
- glBegin(GL_LINE_LOOP);
- glVertex2f((GLfloat)i/2, (GLfloat)0);
- glVertex2f((GLfloat)i/2, (GLfloat)10);
- glEnd();
- //draw horizontal lines
- glBegin(GL_LINE_LOOP);
- glVertex2f((GLfloat)0,(GLfloat)i/2);
- glVertex2f((GLfloat)10,(GLfloat)i/2);
- glEnd();
- }
- }
- void CurveDisplay::goToXY(double x_new, double y_new)
- {
- glTranslated(x_new, y_new, 0.0);
- }
- void CurveDisplay::drawLine(double x1, double y1, double x2, double y2)
- {
- glBegin(GL_LINES);
- glVertex2f((GLfloat)x1, (GLfloat)y1); // origin of the line
- glVertex2f((GLfloat)x2, (GLfloat)y2); // ending point of the line
- glEnd();
- }
- void CurveDisplay::lineRel(double deltaX, double deltaY) {
- drawLine(x, y, x+deltaX, y+deltaY);
- x += deltaX;
- y += deltaY;
- }
- void CurveDisplay::paint() {
- glColor3f(0,0,0);
- dist = dist0;
- for (int i=sierpinski_level; i > 0; i--) dist /= 2;
- goToXY(2*dist, dist);
- sierpA(sierpinski_level); // start recursion
- lineRel(+dist, +dist);
- sierpB(sierpinski_level); // start recursion
- lineRel(-dist, +dist);
- sierpC(sierpinski_level); // start recursion
- lineRel(-dist, -dist);
- sierpD(sierpinski_level); // start recursion
- lineRel(+dist, -dist);
- }
- void CurveDisplay::sierpA (int level) {
- if (level > 0) {
- sierpA(level-1);
- lineRel(+dist, +dist);
- sierpB(level-1);
- lineRel(+2*dist, 0);
- sierpD(level-1);
- lineRel(+dist, -dist);
- sierpA(level-1);
- }
- }
- void CurveDisplay::sierpB (int level) {
- if (level > 0) {
- sierpB(level-1);
- lineRel(-dist, +dist);
- sierpC(level-1);
- lineRel(0, +2*dist);
- sierpA(level-1);
- lineRel(+dist, +dist);
- sierpB(level-1);
- }
- }
- void CurveDisplay::sierpC (int level) {
- if (level > 0) {
- sierpC(level-1);
- lineRel(-dist, -dist);
- sierpD(level-1);
- lineRel(-2*dist, 0);
- sierpB(level-1);
- lineRel(-dist, +dist);
- sierpC(level-1);
- }
- }
- void CurveDisplay::sierpD (int level) {
- if (level > 0) {
- sierpD(level-1);
- lineRel(+dist, -dist);
- sierpA(level-1);
- lineRel(0, -2*dist);
- sierpC(level-1);
- lineRel(-dist, -dist);
- sierpD(level-1);
- }
- }
- void CurveDisplay::displayGrid(int choice)
- {
- if (choice == 0) display_grid = false;
- else display_grid = true;
- updateGL();
- }
- void CurveDisplay::changeLevel(int new_level){
- sierpinski_level = new_level;
- updateGL();
- }
Advertisement
Add Comment
Please, Sign In to add comment