Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

Learn how to create a colorful Symmetrical Phyllotaxis Flower Burst animation using OpenGL and GLUT in C++. Includes Ubuntu installation, compile command, source code, explanation, and output.

Symmetrical Phyllotaxis Flower Burst using OpenGL in C++ (Ubuntu)

๐ŸŒธ Symmetrical Phyllotaxis Flower Burst using OpenGL in C++ (Ubuntu)

๐Ÿ“˜ Introduction:

This OpenGL project creates a beautiful Symmetrical Phyllotaxis Flower Burst animation using C++ and GLUT. The program arranges hundreds of colorful circles in a spiral pattern using the famous Golden Angle (137.5°). Each circle is assigned a different color using the HSV color model, producing a visually appealing flower-like animation similar to sunflower seed arrangements found in nature. This project is an excellent example for learning OpenGL graphics, animation, mathematical visualization, and procedural art.

๐Ÿ›  Ubuntu Installation:

Before compiling this program, install the required OpenGL and GLUT development libraries.


sudo apt update

sudo apt install freeglut3-dev

The above command installs: • OpenGL Library • GLU Library • GLUT Library These libraries are required for compiling and running OpenGL applications.

⚙ Compile and Run in Ubuntu:

Step 1: Save the program as flower.cpp

Step 2: Compile the program


g++ flower.cpp -o flower -lGL -lGLU -lglut

Step 3: Run the executable


./flower

Compile Command Explanation

  • g++ → GNU C++ Compiler
  • flower.cpp → Source File
  • -o flower → Creates executable file named flower
  • -lGL → Links OpenGL Library
  • -lGLU → Links OpenGL Utility Library
  • -lglut → Links GLUT Library

๐Ÿ’ป Complete C++ Program:

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

#define PI 3.14159265358979323846

int point = 0;

// HSV to RGB conversion
void HSVtoRGB(float h, float s, float v, float &r, float &g, float &b)
{
    int i = int(h * 6);
    float f = h * 6 - i;
    float p = v * (1 - s);
    float q = v * (1 - f * s);
    float t = v * (1 - (1 - f) * s);

    switch (i % 6)
    {
        case 0: r = v; g = t; b = p; break;
        case 1: r = q; g = v; b = p; break;
        case 2: r = p; g = v; b = t; break;
        case 3: r = p; g = q; b = v; break;
        case 4: r = t; g = p; b = v; break;
        case 5: r = v; g = p; b = q; break;
    }
}

// Draw circle
void drawCircle(float cx, float cy, float radius)
{
    glBegin(GL_LINE_LOOP);

    for (int i = 0; i < 100; i++)
    {
        float angle = 2 * PI * i / 100.0f;

        glVertex2f(
            cx + radius * cos(angle),
            cy + radius * sin(angle));
    }

    glEnd();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    for (int i = 0; i < point; i++)
    {
        float angle = i * 137.5f * PI / 180.0f;

        float distance = i * 0.8f;

        float x = cos(angle) * distance;
        float y = sin(angle) * distance;

        float r, g, b;

        HSVtoRGB(fmod(i * 0.003f, 1.0f),
                 1.0f,
                 1.0f,
                 r,
                 g,
                 b);

        glColor3f(r, g, b);

        drawCircle(x, y, i * 0.1f);
    }

    glutSwapBuffers();
}

void timer(int)
{
    if (point < 500)
    {
        point++;
    }

    glutPostRedisplay();

    usleep(10000);

    glutTimerFunc(1, timer, 0);
}

void init()
{
    glClearColor(0,0,0,1);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(-450,450,-450,450);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowSize(900,900);

    glutCreateWindow("Symmetrical Phyllotaxis Flower Burst");

    init();

    glutDisplayFunc(display);

    glutTimerFunc(0,timer,0);

    glutMainLoop();

    return 0;
}

๐Ÿ“˜ Explanation:

This program creates a colorful Symmetrical Phyllotaxis Flower Burst animation using OpenGL and GLUT. It places circles in a spiral pattern using the Golden Angle (137.5°). Every circle is assigned a different color using the HSV color model, creating a beautiful flower-like design.

1. Header Files

  • #include <GL/glut.h> → Provides OpenGL and GLUT functions for graphics programming.
  • #include <cmath> → Used for mathematical functions like sin(), cos(), and fmod().
  • #include <unistd.h> → Used for the usleep() function to slow down the animation.

2. Global Variable

int point = 0;

The variable point stores the number of circles to draw. Initially, its value is zero. The timer function increases this value continuously until it reaches 500.

3. HSVtoRGB()

This function converts colors from the HSV (Hue, Saturation, Value) model into the RGB (Red, Green, Blue) model because OpenGL uses RGB colors for drawing.

4. drawCircle()

The drawCircle() function draws one circle using 100 small line segments. The coordinates are calculated using the sine and cosine functions.

5. display()

The display() function is responsible for drawing the animation.

  • Clears the screen.
  • Calculates the spiral angle using the Golden Angle (137.5°).
  • Calculates the position of every circle.
  • Generates a different color for every circle.
  • Draws circles with increasing radius.
  • Displays the completed frame.

6. timer()

The timer function increases the value of point by one every few milliseconds. After increasing the value, it redraws the screen to create a smooth animation.

7. init()

This function initializes the OpenGL environment by setting the background color to black and defining a 2D coordinate system.

8. main()

The main() function initializes GLUT, creates the OpenGL window, registers the display and timer functions, and starts the GLUT event loop.

Working Principle

  1. Create the OpenGL window.
  2. Initialize the graphics environment.
  3. Start the timer.
  4. Increase the number of circles.
  5. Calculate spiral positions using the Golden Angle.
  6. Generate different colors using HSV.
  7. Draw colorful circles.
  8. Repeat until 500 circles are displayed.

๐Ÿ–ฅ Sample Output:



Symmetrical Phyllotaxis Flower Burst

• Opens a 900 × 900 OpenGL window.

• Displays a black background.

• Hundreds of colorful circles appear gradually.

• Circles form a beautiful spiral flower pattern.

• Animation stops after drawing 500 circles.

๐Ÿ”‘ Keywords:

OpenGL C++ Program, GLUT Graphics, Computer Graphics using C++, OpenGL Ubuntu Tutorial, Phyllotaxis Flower Burst, Golden Angle Animation, OpenGL Animation, Graphics Programming, C++ OpenGL Example, Ubuntu OpenGL Project

๐Ÿ” Search Description:

Learn how to create a Symmetrical Phyllotaxis Flower Burst animation using OpenGL and GLUT in C++. This tutorial includes Ubuntu installation, compile commands, complete source code, explanation, and sample output.

๐Ÿ“Œ Hashtags:

#OpenGL #GLUT #ComputerGraphics #CPlusPlus #Ubuntu #Animation #GraphicsProgramming #Phyllotaxis #FlowerBurst #1printf

Comments

Popular Posts

๐ŸŒ™