Lab 16 L

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

HÌNH HỌC FRACTAL

1. Đường cong Kock.......................................................................................................1


2. Chương trình Mandelbrotset01.cpp.........................................................................3
3. Chương trình Mandelbrotset02.cpp.........................................................................4
4. Bài tập.........................................................................................................................7

1. Đường cong Kock


#include <cmath>
#include <vector>
#include <windows.h>
#include <gl/glut.h>

using namespace std;

#define eps 0.00001


#define WINDOW_WIDTH 500.0
#define WINDOW_HEIGHT 500.0
#define pi (2*acos(0.0))

#define MAX_VAL 9999999


#define MIN_VAL -9999999

class Point
{
public:
double x,y,z;

Point(){
}
Point(double a, double b, double c){
x = a;
y = b;
z = c;
}
~Point(){}
double dot(Point v){
return x*v.x + y*v.y + z*v.z;
}
Point operator+(Point pt) {
return Point(x + pt.x, y + pt.y, z + pt.z);
}
Point operator-(Point pt) {
return Point(x - pt.x, y - pt.y, z - pt.z);
}
Point operator*(double v) {
return Point(x*v, y*v, z*v);
}
Point operator*(Point v){
return Point(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
}
Point operator/(double pt) {
return Point(x/pt, y/pt, z/pt);
}
Point normalize() {
return *this / sqrt(x*x + y*y + z*z);
}

};

Point pos(0, 0, -150);


Point u(0,1,0);
Point r(1, 0, 0);

KhoaCNTT – Trường ĐHBK 1


Point l(0, 0, 1);

void drawKoch(double x1, double y1, double x2, double y2, int it){
float angle = 60 * (pi/180);
double x3 = (2*x1+x2)/3;
double y3 = (2*y1+y2)/3;

double x4 = (x1+2*x2)/3;
double y4 = (y1+2*y2)/3;

double x = x3 + (x4 - x3)* cos(angle) + (y4 -y3)* sin(angle);


double y = y3 - (x4 - x3)* sin(angle) + (y4 -y3)* cos(angle);

if(it>0){
drawKoch(x1,y1,x3,y3,it-1);
drawKoch(x3,y3,x,y,it-1);
drawKoch(x,y,x4,y4,it-1);
drawKoch(x4,y4,x2,y2,it-1);
}
else{
glColor3f(1.0, 0, 0);
glBegin(GL_LINES);{
glVertex3f( x1,y1,0);
glVertex3f(x3,y3,0);

glVertex3f(x3,y3,0);
glVertex3f(x,y,0);

glVertex3f(x,y,0);
glVertex3f(x4,y4,0);

glVertex3f(x4,y4,0);
glVertex3f(x2,y2,0);
}glEnd();
}
}

void display(){

//clear the display


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0,0); //color black
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/********************
/ set-up camera here
********************/
//load the correct matrix -- MODEL-VIEW matrix
glMatrixMode(GL_MODELVIEW);

//initialize the matrix


glLoadIdentity();

//now give three info


//1. where is the camera (viewer)?
//2. where is the camera looking?
//3. Which direction is the camera's UP direction?

//gluLookAt(100,100,100, 0,0,0, 0,0,1);


//gluLookAt(200*cos(cameraAngle), 200*sin(cameraAngle), cameraHeight,
0,0,0, 0,0,1);
gluLookAt(pos.x, pos.y, pos.z, pos.x + l.x, pos.y + l.y, pos.z + l.z, u.x, u.y, u.z);

//again select MODEL-VIEW


glMatrixMode(GL_MODELVIEW);

drawKoch(50,0,0,100,2);
drawKoch(0,100,-50,0,2);
drawKoch(-50,0,50,0,2);

//ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
glutSwapBuffers();
}

void init(){
//codes for initialization

KhoaCNTT – Trường ĐHBK 2


//clear the screen
glClearColor(0,0,0,0);

/************************
/ set-up projection here
************************/
//load the PROJECTION matrix
glMatrixMode(GL_PROJECTION);

//initialize the matrix


glLoadIdentity();

//give PERSPECTIVE parameters


//gluPerspective(fovY, aspect_ratio, Near, Far);
gluPerspective(80, 1, 1, 1000.0);
//field of view in the Y (vertically)
//aspect ratio that determines the field of view in the X direction (horizontally)
//near distance
//far distance
}

int main(int argc, char **argv){

glutInit(&argc,argv);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); //Depth, Double buffer,
RGB color

glutCreateWindow("Ton That Bao Van 22T_DT3");


init();

glEnable(GL_DEPTH_TEST); //enable Depth Testing


glutDisplayFunc(display); //display callback function
glutMainLoop(); //The main loop of OpenGL

return 0;
}

 Bài tập
- Thay đổi các cấp hiển thị đường cong
- Thay đổi hình dạng ban đầu của đường cong
- Tô màu cho đường cong

2. Chương trình Mandelbrotset01.cpp


#include <GL/glut.h>

#include <complex>
using std::complex;

// Render the Mandelbrot set into the image array.


// The parameters specify the region on the complex plane to plot.
void compute_mandelbrot( double left, double right, double top, double bottom )
{
// The number of times to iterate before we assume that a point isn't in the
// Mandelbrot set.
// (You may need to turn this up if you zoom further into the set.)
const int MAX_ITERATIONS = 500;

const int width = glutGet( GLUT_WINDOW_WIDTH );


const int height = glutGet( GLUT_WINDOW_HEIGHT );

glBegin( GL_POINTS ); // start drawing in single pixel mode


for( int y = 0; y < height; ++y )
{
for( int x = 0; x < width; ++x )

KhoaCNTT – Trường ĐHBK 3


{
// Work out the point in the complex plane that
// corresponds to this pixel in the output image.
complex<double> c( left + ( x * ( right - left ) / width ),
top + ( y * ( bottom - top ) / height ) );

// Start off z at (0, 0).


complex<double> z( 0.0, 0.0 );

// Iterate z = z^2 + c until z moves more than 2 units


// away from (0, 0), or we've iterated too many times.
int iterations = 0;
while( abs( z ) < 2.0 && iterations < MAX_ITERATIONS )
{
z = ( z * z ) + c;

++iterations;
}

if( iterations == MAX_ITERATIONS )


{
glColor3f( 1.0, 0.0, 0.0 ); // Set color to draw mandelbrot
// z didn't escape from the circle.
// This point is in the Mandelbrot set.
glVertex2i( x, y );
}
else
{
glColor3f( 0.0, 0.0, 0.0 ); //Set pixel to black
// z escaped within less than MAX_ITERATIONS
// iterations. This point isn't in the set.
glVertex2i( x, y );
}
}
}
glEnd();
}

void display()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
const int width = glutGet( GLUT_WINDOW_WIDTH );
const int height = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, width, 0, height, -1, 1 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

compute_mandelbrot( -2.0, 1.0, 1.125, -1.125 );


glutSwapBuffers();
}

int main( int argc, char** argv )


{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( 300, 300 );
glutCreateWindow( "Mandelbrot" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}

3. Chương trình Mandelbrotset02.cpp


Mandelbrot set implementation using c and opengl

KhoaCNTT – Trường ĐHBK 4


The Mandelbrot set is a set of points which are bounded to a property Z(n+1) = Z(n)^2
+ c. Where c is part of Mandelbrot set if, when set Z(0) = 0 and applying iteration
repeatedly the absolute value Z(n) will be in the bounded area however large n gets.
Example:-
c=2
Z(0) = 0
then according to the squence Z(n+1) = Z(n)^2 + c will produce 0, 2, 6, 38 … which lead
to infinity => 2 is not in Mandelbrot set. but check with c = i then u will see for how many
iteration you try the no Z(n+1) is always in the boundary that is in Mandelbrot set.
2nd criteria is |c| < 2 that is the Mandelbrot set is contained in the closed disk of radius 2
around the origin.
Here Z(0), Z(n) and c are complex numbers.
let Z = x + yi then
Z^2 = (x + yi)^2
= x^2 +(yi)^2 + 2xyi
= x^2 – y^2 + 2xyi

real part = x^2 – Y^2


imaginary part = 2xy

#include <GL/glut.h>
#include <stdio.h>
/*
*defining a RGB struct to color the pixel
*/
struct Type_rgb{
float r;
float g;
float b;
};
/*
* pixel variable contain the value of the color pixel in
* the picture.
* pattern is a predefine set of color for a particular
* value.
*/
struct Type_rgb pixels[841*1440], pattern[999];

/*
* function mandelbrotset find where the number is in
* mandelbrotset or not and also assign a color to that
* coordinate with that iteration pattern.
*/

void mandelbrotset()
{
/*
* x0 :- is the real part of c value
* will range from -2.5 to 1.1.
* y0 :- is the imaginary part of c value

KhoaCNTT – Trường ĐHBK 5


* will range from -1 to 1.1.
* x and y :- is the real and imaginary part of Zn.
* iteration :- is to keep control variable of the number
* of iteration
* max_iteration :- maximum number of iteration
* (which is one of bailout condition)
* loc :- represent the location pixel of the
* current x,y coordinate.
*/

float x0, y0, x, y, xtemp;


int iteration, max_iteration, loc=0;
printf("\nstart");
for(y0 = -1; y0 < 1.1; y0 = y0 + 0.0025)
for(x0 = -2.5; x0 < 1.1; x0 = x0 + 0.0025){
x = 0;
y = 0;
iteration = 0;
max_iteration = 1000;
/*
* (x*x) + (y*y) < (2*2) is the 2nd bailout condition ie
* the mandelbrot set is always within a radius of 2.
*/
while(((x*x) + (y*y) < (2*2)) && iteration < max_iteration){
xtemp = (x*x) - (y*y) + x0;
y = (2*x*y) + y0;

x = xtemp;
iteration = iteration + 1;
}
if(iteration >= 999){
/*
* setting color pixel to Mandelbrot set coordinate
*to black.
*/
pixels[loc].r = 0;
pixels[loc].g = 0;
pixels[loc].b = 0;
}else{
/*
* setting color pixel to the reset of the coordinate by the
* pattern of no of iteration before bailout.
*/
pixels[loc].r = pattern[iteration].r;
pixels[loc].g = pattern[iteration].g;
pixels[loc].b = pattern[iteration].b;
}
loc = loc + 1;
}
}

void Init( )
{
/*
* Basic Opengl initialization.
* 1440 = (-2.5 - 1.1)/0.0025
* here total x coordinate distance / no of division.
* 840 = (-1 - 1.1)/0.0025 +1
* here total y coordinate distance / no of division.
*/
glViewport(0, 0, 1440, 841);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
gluOrtho2D(0, 1440, 0, 841);

int i;
float r, g, b;
/*
* Initializing all the pixels to white.
*/
for(i = 0; i < 841*1440; i++){
pixels[i].r = 1;
pixels[i].g = 1;
pixels[i].b = 1;

KhoaCNTT – Trường ĐHBK 6


}

i = 0;
/*
* Initializing all the pattern color till 9*9*9
*/
for(r = 0.1; r <= 0.9; r= r+0.1)
for(g = 0.1; g <= 0.9; g = g+0.1)
for(b = 0.1; b <= 0.9; b = b+0.1){
pattern[i].r = b;
pattern[i].g = r;
pattern[i].b = g;
i++;
}
/*
* Initializing the rest of the pattern as 9*9*9 is 729.
* and we need up to 999 pattern as the loop bailout
* condition is 1000.
*/

for( ; i <= 999; i++){


pattern[i].r = 1;
pattern[i].g = 1;
pattern[i].b = 1;
}
mandelbrotset();

void onDisplay()
{
/*
* Clearing the initial buffer
*/
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
/*
* Draw the complete Mandelbrot set picture.
*/
glDrawPixels(1440, 841, GL_RGB, GL_FLOAT, pixels);
glutSwapBuffers();
}

int main(int argc, char** argv)


{
/*
* Here basic Opengl initialization.
*/
glutInit(&argc, argv);
glutInitWindowSize (1440, 841);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Mandelbrotset by SKR");

Init ();
/*
* connecting the Display function
*/
glutDisplayFunc(onDisplay);
/*
* starting the activities
*/
glutMainLoop();
return 0;
}

4. Bài tập
1) Viết chương trình nhập n để vẽ đường cong Kock bậc n.

KhoaCNTT – Trường ĐHBK 7


2) Viết chương trình nhập n và vẽ đường cong C cấp n.

KhoaCNTT – Trường ĐHBK 8


3) Viết chương trình nhập n và vẽ đường cong Rồng cấp n.

4) Viết chương trình sinh tập Mandelbrot.

KhoaCNTT – Trường ĐHBK 9


5) Viết chương trình sinh đường Pythagoras.

6) Viết chương trình sinh tập Julia.

KhoaCNTT – Trường ĐHBK 10


7)

8) Viết chương trình vẽ các đường cong tô vùng: Phoenix, Hilbert, Sierpinxki.

KhoaCNTT – Trường ĐHBK 11


9) Khai thác các chức năng của phần mềm Ultra Fractal
(https://www.ultrafractal.com/)

----------------------------------------------------------

KhoaCNTT – Trường ĐHBK 12

You might also like