Lab 16 L
Lab 16 L
Lab 16 L
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);
}
};
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);
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
/************************
/ 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;
}
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
#include <complex>
using std::complex;
++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
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;
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.
*/
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();
}
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.
8) Viết chương trình vẽ các đường cong tô vùng: Phoenix, Hilbert, Sierpinxki.
----------------------------------------------------------