Assignment 7 - Bezier and Koch Curve
Aim: Generate fractal patterns using i) Bezier ii) Koch Curve
Theory:
Bezier Curves
Properties of Bezier Curves
Koch Curve
Fractals are geometric objects. Many real-world objects like ferns are shaped like fractals. Fractals are formed by iterations. Fractals are self-similar.
In computer graphics, we use fractal functions to create complex object
The object representations uses Euclidean-geometry methods; that is, object shapes were described with equations. These methods are adequate for describing manufactured objects: those that have smooth surfaces and regular shapes. But natural objects, such as mountains and clouds, have irregular or fragmented features, and Euclidean methods do not realistically model these objects. Natural objects can be realistically described with fractal-geometry methods, where procedures rather than equations are used to model objects.
In computer graphics, fractal methods are used to generate displays of natural objects and visualizations . The self-similarity properties of an object can take different forms, depending on the choice of fractal representation.
In computer graphics, we use fractal functions to create complex objects
A mountain outlined against the sky continues to have the same jagged shape as we view it from a closer and closer. We can describe the amount of variation in the object detail with a number called the fractal dimension.
Examples: In graphics applications, fractal representations are used to model terrain, clouds, water, trees and other plants, feathers, fur, and various surface textures, and just to make pretty patterns. In other disciplines, fractal patterns have been found in the distribution of stars, river islands, and moon craters; in rain fields; in stock market variations; in music; in traffic flow; in urban property utilization; and in the boundaries of convergence regions for numerical- analysis techniques
Koch Fractals (Snowflakes)
|
• Add Some Randomness:
• The fractals we’ve produced so far seem to be very regular and “artificial”.
• To create some realism and variability, simply change the angles slightly sometimes based on a random number generator.
• For example, you can curve some of the ferns to one side.
• For example, you can also vary the lengths of the branches and the branching factor.
Terrain (Random Mid-point Displacement):
• Given the heights of two end-points, generate a height at the mid-point.
• Suppose that the two end-points are a and b. Suppose the height is in the y direction, such that the height at a is y(a), and the height at b is y(b).
• Then, the height at the mid-point will be:
ymid = (y(a)+y(b))/2 + r, where
r is the random offset
• This is how to generate the random offset r:
r = srg|b-a|, where
s is a user-selected “roughness” factor, and
rg is a Gaussian random variable with mean 0 and variance 1
CODE FOR Bezier Curves
#include <math.h>
#include <time.h>
#include <GL/glut.h>
using namespace std;
int x[4],y[4];
void init(){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,640,0,480);
glClear(GL_COLOR_BUFFER_BIT);
}
void putpixel(double xt,double yt )
{
glColor3f(1,0,0);
glBegin(GL_POINTS);
glVertex2d(xt,yt);
glEnd();
glFlush();
}
void Algorithm(){
glColor3f(0,1,0);
glBegin(GL_LINES);
glVertex2i(x[0],y[0]);
glVertex2i(x[1],y[1]);
glVertex2i(x[1],y[1]);
glVertex2i(x[2],y[2]);
glVertex2i(x[2],y[2]);
glVertex2i(x[3],y[3]);
glEnd();
glFlush();
double t;
for (t = 0.0; t < 1.0; t += 0.0005)
{
double xt = pow(1-t, 3) * x[0] + 3 * t * pow(1-t, 2) * x[1] + 3 * pow(t, 2) * (1-t) * x[2] + pow(t, 3) * x[3];
double yt = pow(1-t, 3) * y[0] + 3 * t * pow(1-t, 2) * y[1] + 3 * pow(t, 2) * (1-t) * y[2] + pow(t, 3) * y[3];
putpixel(xt, yt);
}
}
int main(int argc, char** argv){
cout<<"\n \t Enter The Four Points x space y ";
for(int i=0;i<4;i++){
cout<<"\n \t Enter x and y for "<<i<<" = ";
cin>>x[i]>>y[i];
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(200,200);
glutCreateWindow("Bezier 4 point");
init();
glutDisplayFunc(Algorithm);
glutMainLoop();
return 0;
}
CODE FOR Koch Curve
#include <math.h>
#include <time.h>
#include <GL/glut.h>
using namespace std;
double x,y,len,angle;
int it;
void init(){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,640,0,480);
glClear(GL_COLOR_BUFFER_BIT);
}
void line1(int x1, int y11, int x2,int y2){
glColor3f(0,1,0);
glBegin(GL_LINES);
glVertex2i(x1,y11);
glVertex2i(x2,y2);
glEnd();
glFlush();
}
void k_curve(double x, double y, double len, double angle, int it){
if(it>0){
len /=3;
k_curve(x,y,len,angle,(it-1));
x += (len * cosl(angle * (M_PI)/180));
y += (len * sinl(angle * (M_PI)/180));
k_curve(x,y, len, angle+60,(it-1));
x += (len * cosl((angle + 60) * (M_PI)/180));
y += (len * sinl((angle + 60) * (M_PI)/180));
k_curve(x,y, len, angle-60,(it-1));
x += (len * cosl((angle - 60) * (M_PI)/180));
y += (len * sinl((angle - 60) * (M_PI)/180));
k_curve(x,y,len,angle,(it-1));
}
else
{
line1(x,y,(int)(x + len * cosl(angle * (M_PI)/180) + 0.5),(int)(y + len * sinl(angle * (M_PI)/180) + 0.5));
}
}
void Algorithm(){
k_curve(x,y,len,angle,it);
}
int main(int argc, char** argv){
cout<<"\n Enter Starting Point x space y ";
cin>>x>>y;
cout <<"\n Lenght of line and space angle of line";
cin>>len>>angle;
cout<<"\n No. of ittration ";
cin>>it;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(200,200);
glutCreateWindow("Koch");
init();
glutDisplayFunc(Algorithm);
glutMainLoop();
return 0;
}
Comments
Post a Comment