Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation Fault on OpenGL program
    text
    copied!<p>I'm trying to draw the <a href="http://en.wikipedia.org/wiki/Sierpinski_carpet" rel="nofollow">Sierpinski carpet plane fractal</a> using OpenGL, but my program keeps receiving a SegFault error.</p> <p>My code is as follows: </p> <pre><code>#include &lt;GL/gl.h&gt; #include &lt;GL/glut.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; class GLintPoint { public: GLint x, y; }; int random(int m) { return rand() % m; } void drawDot(GLint x, GLint y) { glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } void init() { glClearColor(1.0, 1.0, 1.0, 0.0); glColor3f(0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0); } int isSierpinskiCarpetPixelFilled(int x, int y, int width, int height) { GLintPoint point; // base case 1 of 2 if ((x &lt;= 0)||(y &lt;= 0)||(x&gt;=width)||(y&gt;=height)) //top row or left column or out of bounds should be full { point.x = x; point.y = y; drawDot(point.x, point.y); } { /* If the grid was split in 9 parts, what part(x2,y2) would x,y fit into? */ int x2 = x * 3 / width; // an integer from 0..2 inclusive int y2 = y * 3 / height; // an integer from 0..2 inclusive // base case 2 of 2 if (x2 == 1 &amp;&amp; y2 == 1) // if in the center square, it should be empty return 0; // general case /* offset x and y so it becomes bounded by 0..width/3 and 0..height/3 and prepares for recursive call some offset is added to make sure the parts have all the correct size when width and height isn't divisible by 3 */ x -= (x2 * width+2) / 3; y -= (y2 * height+2) / 3; width = (width +2-x2)/3; height = (height+2-y2)/3; } return isSierpinskiCarpetPixelFilled(x, y, width, height); } void drawSierpinskiCarpet() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 1.0, 0.0); int x = 50; int y = 50; isSierpinskiCarpetPixelFilled(x,y,50,50); glFlush(); } int main(int argc, char *argv[]) { glutInit(&amp;argc, argv); glutInitWindowSize(640,480); glutInitWindowPosition(10,10); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutCreateWindow("The Sierpinski Carpet"); glutDisplayFunc(drawSierpinskiCarpet); init(); glutMainLoop(); return EXIT_SUCCESS; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload