Nothing Special   »   [go: up one dir, main page]

22Nh15 - DHMT - Lab16 - 102220026 - 10220041

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

HÌNH HỌC FRACTAL

Nhóm học phần: 22NH15


Sinh viên 1: Lê Hải Khoa MSSV: 102220026
Sinh viên 2: Lê Nguyễn Ngọc Thanh MSSV: 102220041

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);
}

KhoaCNTT – Trường ĐHBK 1


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);
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);

KhoaCNTT – Trường ĐHBK 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
//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("My OpenGL Program");


init();

glEnable(GL_DEPTH_TEST); //enable Depth Testing


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

return 0;
}

KhoaCNTT – Trường ĐHBK 3


 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

KhoaCNTT – Trường ĐHBK 4


-

- Tô màu cho đường cong

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


#include <GL/glut.h>

KhoaCNTT – Trường ĐHBK 5


#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 )
{
// 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 )

KhoaCNTT – Trường ĐHBK 6


{
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
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

KhoaCNTT – Trường ĐHBK 7


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
* 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;

KhoaCNTT – Trường ĐHBK 8


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;
}

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();

KhoaCNTT – Trường ĐHBK 9


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.
#include <cmath>

KhoaCNTT – Trường ĐHBK 10


#include <iostream>
#include <GL/glut.h>

using namespace std;

#define WINDOW_WIDTH 500


#define WINDOW_HEIGHT 500
#define pi (2*acos(0.0))

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{
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x3, y3);

glVertex2f(x3, y3);
glVertex2f(x, y);
KhoaCNTT – Trường ĐHBK 11
glVertex2f(x, y);
glVertex2f(x4, y4);

glVertex2f(x4, y4);
glVertex2f(x2, y2);
glEnd();
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(2.0);

double x1 = -200;
double y1 = 0;
double x2 = 200;
double y2 = 0;

int n;
cout << "Nhap vao bac cua duong cong Koch (n): ";
cin >> n;

drawKoch(x1, y1, x2, y2, n);

glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-250, 250, -250, 250);
}
KhoaCNTT – Trường ĐHBK 12
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("Koch Curve");

init();
glutDisplayFunc(display);

glutMainLoop();

return 0;
}

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


#include <cmath>
#include <iostream>
#include <GL/glut.h>

using namespace std;

#define WINDOW_WIDTH 500


#define WINDOW_HEIGHT 500

class Point {
public:
double x, y;

Point() {}
Point(double x, double y) : x(x), y(y) {}
};
KhoaCNTT – Trường ĐHBK 13
// Tính toán điểm trung gian của hai điểm
Point midPoint(Point p1, Point p2) {
return Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}

// Vẽ đường cong Cấp n


void drawC(Point p1, Point p2, int n) {
if (n == 0) {
// Điểm dừng khi đạt cấp n
glBegin(GL_POINTS);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
} else {
// Tính điểm trung gian của hai điểm
Point mid = midPoint(p1, p2);

// Tính các điểm cần thiết cho đường cong Cấp n


double deltaX = p2.x - p1.x;
double deltaY = p2.y - p1.y;

double newDeltaX = deltaY;


double newDeltaY = -deltaX;

// Tính toán các điểm cần thiết cho đường cong Cấp n
Point a(mid.x + newDeltaX, mid.y + newDeltaY);
Point b(mid.x - newDeltaX, mid.y - newDeltaY);

// Đệ quy vẽ đường cong Cấp n-1


drawC(p1, a, n - 1);
drawC(a, mid, n - 1);
drawC(mid, b, n - 1);
KhoaCNTT – Trường ĐHBK 14
drawC(b, p2, n - 1);
}
}

// Hàm display
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(2.0);

Point p1(-200, 0);


Point p2(200, 0);

int n;
cout << "Nhap vao bac cua duong cong C (n): ";
cin >> n;

// Vẽ đường cong Cấp n


drawC(p1, p2, n);

glFlush();
}

// Hàm init
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-250, 250, -250, 250);
}

// Hàm main
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
KhoaCNTT – Trường ĐHBK 15
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("C Curve");

init();
glutDisplayFunc(display);

glutMainLoop();

return 0;
}

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


#include <cmath>
#include <iostream>
#include <GL/glut.h>

using namespace std;

#define WINDOW_WIDTH 500


#define WINDOW_HEIGHT 500

class Point {
public:
double x, y;

Point() {}
Point(double x, double y) : x(x), y(y) {}
};

// Đổi hướng quay của đường cong Rồng


void rotate(Point& p, Point& q, double angle) {
double dx = q.x - p.x;
KhoaCNTT – Trường ĐHBK 16
double dy = q.y - p.y;
q.x = p.x + dx * cos(angle) - dy * sin(angle);
q.y = p.y + dx * sin(angle) + dy * cos(angle);
}

// Vẽ đường cong Rồng cấp n


void drawDragonCurve(Point p, Point q, int n) {
if (n == 0) {
glBegin(GL_LINES);
glVertex2f(p.x, p.y);
glVertex2f(q.x, q.y);
glEnd();
} else {
Point mid = {(p.x + q.x) / 2, (p.y + q.y) / 2};
double angle = M_PI / 2.0;
rotate(mid, p, angle);
rotate(mid, q, angle);

drawDragonCurve(p, mid, n - 1);


drawDragonCurve(q, mid, n - 1);
}
}

// Hàm display
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(2.0);

Point p(-200, 0);


Point q(200, 0);

int n;
KhoaCNTT – Trường ĐHBK 17
cout << "Nhap vao bac cua duong cong Rong (n): ";
cin >> n;

// Vẽ đường cong Rồng cấp n


drawDragonCurve(p, q, n);

glFlush();
}

// Hàm init
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-250, 250, -250, 250);
}

// Hàm main
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("Dragon Curve");

init();
glutDisplayFunc(display);

glutMainLoop();

return 0;
}

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


#include <iostream>
KhoaCNTT – Trường ĐHBK 18
#include <GL/glut.h>

using namespace std;

const int WIDTH = 800;


const int HEIGHT = 800;
const int MAX_ITERATIONS = 1000;

// Chuyển đổi pixel (x, y) thành số phức tương ứng trong không gian Mandelbrot
complex<double> mapToComplex(int x, int y, int width, int height, double xMin,
double xMax, double yMin, double yMax) {
double xRatio = (double)x / (double)width;
double yRatio = (double)y / (double)height;

double real = xMin + xRatio * (xMax - xMin);


double imag = yMin + yRatio * (yMax - yMin);

return complex<double>(real, imag);


}

// Tính toán số lần lặp cần thiết để xác định xem một điểm thuộc tập Mandelbrot hay
không
int mandelbrotIterations(complex<double> c) {
complex<double> z(0, 0);
int iterations = 0;

while (iterations < MAX_ITERATIONS && abs(z) < 2) {


z = z * z + c;
iterations++;
}

return iterations;
}

KhoaCNTT – Trường ĐHBK 19


// Vẽ tập Mandelbrot
void drawMandelbrot() {
double xMin = -2.0;
double xMax = 2.0;
double yMin = -2.0;
double yMax = 2.0;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WIDTH, 0, HEIGHT);
glMatrixMode(GL_MODELVIEW);

glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POINTS);
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
complex<double> c = mapToComplex(x, y, WIDTH, HEIGHT, xMin, xMax,
yMin, yMax);
int iterations = mandelbrotIterations(c);
double brightness = (double)iterations / (double)MAX_ITERATIONS;
glColor3f(brightness, brightness, brightness);
glVertex2i(x, y);
}
}
glEnd();

glFlush();
}

// Hàm display
KhoaCNTT – Trường ĐHBK 20
void display() {
drawMandelbrot();
}

// Hàm init
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
}

// Hàm main
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Mandelbrot Set");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

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


#include <iostream>
#include <GL/glut.h>
#include <cmath>

using namespace std;

const int WIDTH = 800;


const int HEIGHT = 800;

// Hàm vẽ đường Pythagoras


void drawPythagoras(int x1, int y1, int x2, int y2, int level) {
KhoaCNTT – Trường ĐHBK 21
if (level == 0) return;

int deltaX = x2 - x1;


int deltaY = y2 - y1;

int x3 = x2 - deltaY;
int y3 = y2 + deltaX;

int x4 = x1 - deltaY;
int y4 = y1 + deltaX;

int x5 = x4 + deltaX;
int y5 = y4 + deltaY;

int x6 = x2 + deltaX;
int y6 = y2 + deltaY;

glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glVertex2i(x6, y6);
glVertex2i(x5, y5);
glEnd();

drawPythagoras(x1, y1, x4, y4, level - 1);


drawPythagoras(x4, y4, x5, y5, level - 1);
drawPythagoras(x5, y5, x6, y6, level - 1);
}

// Hàm display
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
KhoaCNTT – Trường ĐHBK 22
glLoadIdentity();

glColor3f(1.0, 1.0, 1.0);


glTranslatef(WIDTH / 2, HEIGHT / 2, 0);
drawPythagoras(-150, -150, 150, -150, 5);

glFlush();
}

// Hàm init
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WIDTH, 0, HEIGHT);
}

// Hàm main
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Pythagoras Tree");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

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


#include <iostream>
#include <GL/glut.h>
#include <complex>
KhoaCNTT – Trường ĐHBK 23
using namespace std;

const int WIDTH = 800;


const int HEIGHT = 800;

const int MAX_ITERATIONS = 100;


const double MIN_X = -2.0;
const double MAX_X = 2.0;
const double MIN_Y = -2.0;
const double MAX_Y = 2.0;

const double C_REAL = -0.7;


const double C_IMAGINARY = 0.27015;

// Hàm kiểm tra một điểm có thuộc tập Julia hay không
int julia(double x, double y) {
complex<double> z(x, y);
complex<double> c(C_REAL, C_IMAGINARY);

int iterations = 0;
while (abs(z) < 2 && iterations < MAX_ITERATIONS) {
z = z * z + c;
iterations++;
}

if (iterations == MAX_ITERATIONS) return 0;


return iterations;
}

// Hàm display
void display() {
glClear(GL_COLOR_BUFFER_BIT);
KhoaCNTT – Trường ĐHBK 24
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPointSize(1.0);
glBegin(GL_POINTS);

for (int x = 0; x < WIDTH; x++) {


for (int y = 0; y < HEIGHT; y++) {
double real = MIN_X + (double)x / WIDTH * (MAX_X - MIN_X);
double imag = MIN_Y + (double)y / HEIGHT * (MAX_Y - MIN_Y);

int color = julia(real, imag);


double normalizedColor = (double)color / MAX_ITERATIONS;

glColor3f(normalizedColor, normalizedColor, normalizedColor);


glVertex2d(x, y);
}
}

glEnd();
glFlush();
}

// Hàm init
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WIDTH, 0, HEIGHT);
}

// Hàm main
int main(int argc, char** argv) {
KhoaCNTT – Trường ĐHBK 25
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Julia Set");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

7) Viết chương trình vẽ các đường cong tô vùng: Phoenix, Hilbert, Sierpinxki.
#include <iostream>
#include <GL/glut.h>
#include <vector>
#include <cmath>

using namespace std;

// Kích thước cửa sổ


const int WIDTH = 800;
const int HEIGHT = 800;

// Cấu trúc điểm


struct Point {
double x, y;
Point(double _x, double _y) : x(_x), y(_y) {}
};

// Hàm vẽ đường cong Phoenix


void drawPhoenix(int n, Point p1, Point p2) {
if (n == 0) {
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
KhoaCNTT – Trường ĐHBK 26
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
} else {
Point p3(p1.x + (p2.x - p1.x) / 3, p1.y + (p2.y - p1.y) / 3);
Point p4(p2.x - (p2.x - p1.x) / 3, p2.y - (p2.y - p1.y) / 3);
Point p5((p1.x + p2.x) / 2 + (p2.y - p1.y) * sqrt(3) / 6, (p1.y + p2.y) / 2 - (p2.x - p1.x) *
sqrt(3) / 6);

drawPhoenix(n - 1, p1, p3);


drawPhoenix(n - 1, p3, p5);
drawPhoenix(n - 1, p5, p4);
drawPhoenix(n - 1, p4, p2);
}
}

// Hàm vẽ đường cong Hilbert


void drawHilbert(int n, Point p1, Point p2, Point p3, Point p4) {
if (n == 0) {
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glVertex2f(p2.x, p2.y);
glVertex2f(p3.x, p3.y);
glVertex2f(p3.x, p3.y);
glVertex2f(p4.x, p4.y);
glVertex2f(p4.x, p4.y);
glVertex2f(p1.x, p1.y);
glEnd();
} else {
Point p12((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
Point p23((p2.x + p3.x) / 2, (p2.y + p3.y) / 2);
KhoaCNTT – Trường ĐHBK 27
Point p34((p3.x + p4.x) / 2, (p3.y + p4.y) / 2);
Point p41((p4.x + p1.x) / 2, (p4.y + p1.y) / 2);
Point c((p1.x + p3.x) / 2, (p1.y + p3.y) / 2);

drawHilbert(n - 1, p1, p12, c, p41);


drawHilbert(n - 1, p12, p2, p23, c);
drawHilbert(n - 1, c, p23, p3, p34);
drawHilbert(n - 1, p41, c, p34, p4);
}
}

// Hàm vẽ đường cong Sierpinski


void drawSierpinski(int n, Point p1, Point p2, Point p3) {
if (n == 0) {
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glVertex2f(p3.x, p3.y);
glEnd();
} else {
Point mid1((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
Point mid2((p2.x + p3.x) / 2, (p2.y + p3.y) / 2);
Point mid3((p3.x + p1.x) / 2, (p3.y + p1.y) / 2);

drawSierpinski(n - 1, p1, mid1, mid3);


drawSierpinski(n - 1, mid1, p2, mid2);
drawSierpinski(n - 1, mid3, mid2, p3);
}
}

// Hàm display
void display() {
KhoaCNTT – Trường ĐHBK 28
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Vẽ đường cong Phoenix


glColor3f(1.0, 1.0, 1.0);
drawPhoenix(4, Point(100, 200), Point(700, 200));

// Vẽ đường cong Hilbert


glColor3f(1.0, 1.0, 1.0);
drawHilbert(6, Point(100, 500), Point(100, 100), Point(700, 100), Point(700, 500));

// Vẽ đường cong Sierpinski


glColor3f(1.0, 1.0, 1.0);
drawSierpinski(6, Point(400, 600), Point(100, 100), Point(700, 100));

glFlush();
}

// Hàm init
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WIDTH, 0, HEIGHT);
}

// Hàm main
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Fractals");
KhoaCNTT – Trường ĐHBK 29
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

8) Khai thác các chức năng của phần mềm Ultra Fractal
(https://www.ultrafractal.com/)
Trình biên tập Fractal: Ultra Fractal cung cấp một trình biên tập fractal đầy đủ cho phép bạn tạo, chỉnh sửa và tùy
chỉnh các hình ảnh fractal. Bạn có thể thêm, xóa và điều chỉnh các phần tử như biến, hàm và màu sắc để tạo ra hình
ảnh theo ý muốn.

Sử dụng Công thức Fractal: Bạn có thể sử dụng các công thức fractal có sẵn trong Ultra Fractal hoặc tự tạo ra các
công thức mới. Các công thức này có thể được tinh chỉnh để tạo ra các hình ảnh phức tạp và đa dạng.

Thư viện Fractal: Ultra Fractal đi kèm với một thư viện đầy đủ các fractals có sẵn, cho phép bạn duyệt và chọn từ
hàng ngàn hình ảnh fractal để sử dụng hoặc tùy chỉnh.

Màu sắc và Phối màu: Bạn có thể tùy chỉnh màu sắc và phối màu của fractals bằng cách sử dụng bảng màu tích hợp
hoặc tạo ra các bảng màu tùy chỉnh.

Hiệu ứng và Lọc hình ảnh: Ultra Fractal cung cấp một loạt các hiệu ứng và bộ lọc hình ảnh để tạo ra các hình ảnh
fractal đa dạng. Bạn có thể áp dụng làm mờ, làm sắc nét, thay đổi độ tương phản và nhiều hiệu ứng khác.

Xem trước thời gian thực: Bạn có thể xem trước các thay đổi ngay lập tức trong khi tinh chỉnh các thiết lập của
fractals, giúp bạn thấy được kết quả của công việc của mình ngay từ khi bạn thực hiện thay đổi.

Xuất ảnh và chia sẻ: Ultra Fractal cho phép bạn xuất các hình ảnh fractal ở định dạng phổ biến như JPEG, PNG hoặc
BMP để chia sẻ với người khác hoặc in ấn.

Tích hợp với các công cụ 3D: Bạn có thể sử dụng các công cụ 3D để nhập các hình ảnh fractal từ Ultra Fractal và tạo
ra các tác phẩm nghệ thuật 3D phức tạp.

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

KhoaCNTT – Trường ĐHBK 30

You might also like