22Nh15 - DHMT - Lab16 - 102220026 - 10220041
22Nh15 - DHMT - Lab16 - 102220026 - 10220041
22Nh15 - DHMT - Lab16 - 102220026 - 10220041
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);
}
};
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;
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(){
/********************
/ set-up camera here
********************/
//load the correct matrix -- MODEL-VIEW matrix
glMatrixMode(GL_MODELVIEW);
drawKoch(50,0,0,100,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);
glutInit(&argc,argv);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); //Depth, Double buffer,
RGB color
return 0;
}
++iterations;
}
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();
#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.
*/
x = xtemp;
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.
*/
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>
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;
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;
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;
}
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);
}
// 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);
// Hàm display
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(2.0);
int n;
cout << "Nhap vao bac cua duong cong C (n): ";
cin >> 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;
}
class Point {
public:
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
};
// Hàm display
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(2.0);
int n;
KhoaCNTT – Trường ĐHBK 17
cout << "Nhap vao bac cua duong cong Rong (n): ";
cin >> 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;
}
// 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;
// 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;
return iterations;
}
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;
}
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();
// Hàm display
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
KhoaCNTT – Trường ĐHBK 22
glLoadIdentity();
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;
}
// 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++;
}
// 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);
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>
// Hàm display
void display() {
KhoaCNTT – Trường ĐHBK 28
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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.
----------------------------------------------------------