Skip to content

nguyenpanda/ComputerGraphic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


License: MIT

I have always been fascinated by how computer screens work, computer graphics, and how images are displayed and stored on computers. This curiosity inspired me to undertake this project.

Currently, my goal for this project is to render images such as circles, lines, and bijective functions, and then output these images in PNG and BMP formats. Additionally, the program should be able to read images in JPEG and BMP formats for processing within the code.

Table of contents


Requirements


Tool Version
C++ >=20
CMakeFile >=3.28

How to install?


git clone "https://github.com/nguyenpanda/ComputerGraphic.git"
cd /path/to/where/you/place/ComputerGraphic
mkdir build cd build

Please ensure your computer meets all the requirements listed in the Requirements section

Default build system

cmake ..
make
./ComputerGraphic

Windows GNU Make (MinGW Makefiles)

First, check if you have installed GNU Make on your Windows system:

make --version

You should see output similar to the following:

GNU Make 4.4.1
Built for Windows32
Copyright (C) 1988-2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
If the above command outputs the version information for **GNU Make**, you can proceed with the following commands:

If the above command outputs the version information for GNU Make, you can proceed with the following commands:

cmake -G "MinGW Makefiles" ..
make
./ComputerGraphic.exe

All the output files created by int main() function locate in build directory.

Test run

./ComputerGraphic --h
./ComputerGraphic --tc all
./ComputerGraphic --ta -mnist ../mnist/199.csv

How to use?


Creating graphic::Screen object

Initially, we must create graphic::Screen object.

// graphic::Screen(int width, int height)
graphic::Screen scr(w, h);
int width, height;
scr.shape(width, height); // Getting the `scr` shape

Drawing object on graphic::Screen object

Line

// graphic::Line(int x0, int y0, int x1, int y1)
scr << graphic::Line(27, 17, 11, 27);   // Directly draw
// scr::drawline(int x0, int y0, int x1, int y1)
scr.drawline(1, 1, 11, 5);              // Recommence using this way
std::cout << scr;                       // Print the screen onto console/terminal
scr.reset();                            // Reset all Pixel

Circle

// graphic::Circle(int x, int y, int r)
scr << graphic::Circle(25, 25, 15);
// graphic::drawcircle(int x, int y, int r)
scr.drawcircle(25, 17, 5);
scr.drawcircle(17, 25, 5);
scr.drawcircle(25, 33, 5);
scr.drawcircle(33, 25, 5);
scr.drawcircle(3, 3, 1);
scr.drawcircle(8, 8, 0);
std::cout << scr;
Line result Circle result

Plotting

In this example, we will plot f(x) = 0.01*x^3 - 3x in range [-16, 16].

Discrete plot

// Screen::discretePlot(int x_start, int x_end, int (* f)(int))
scr.discretePlot(-16, 16,[](int x) -> int {
    return (int) (std::pow(x, 3) / 100 - 3 * x);
});

std::cout << scr;

Continuous plot

// Screen::plot(int x_start, int x_end, int (* f)(int))
scr.plot(-16, 16,[](int x) -> int {
    return (int) (std::pow(x, 3) / 100 - 3 * x);
});

std::cout << scr;
Line result Circle result

Export text .txt file

graphic::Screen scr(w, 51);

scr.setUp()->setMapChar(graphic::mapchar::std_dot);

scr.plot(0, w - 1,[](int x) -> int {
    return (int) (25 * std::sin(0.2 * (x - 2)));
});

std::cout << scr;

std::ofstream file("write_ofstream0.txt");
file << scr;
file.close();
Line result Result in Terminal
Circle result Result in .txt file

Animation using cursor class

This is some example of animating using curesor class. Get more information on usage at here & here

./ComputerGraphic --tc changeAt
./ComputerGraphic --ta -mnist 199.csv

--tc changeAt

changAt_animation

--ta -mnist 199.csv

mnist_animation

Additionally, it can be used to simulate physical phenomenons.

./ComputerGraphic --ta -bouncing
./ComputerGraphic --ta -orbit

The left side simulates the motion of a rigid object under gravity with fluid resistance, while the right side simulates the motion of an object experiencing only gravitational force.

Bouncing ball

collision

Meteorite's orbit

orbit

Import & Export image

Bitmap (.bmp)

Export bitmap

graphic::Bitmap("filename") << scr;
graphic::Bitmap("filename.bmp") << scr;

Import bitmap

graphic::Bitmap("filename") >> scr;
graphic::Bitmap("filename.bmp") >> scr;

Example

graphic::Screen scr;

graphic::Bitmap("Steveeeeeee") >> scr;

int w, h;
scr.shape(w, h);

for (int j = 0; j < h; ++j) {
    for (int i = 0; i < w; ++i) {
        // Set the green component of all pixels to 100
        scr.pixel(i, j).set_g(100); // Similar to red & blue
    }
}

graphic::Bitmap("Emotional Damage_green=100") << scr;
Bitmap Images from graphic::Bitmap
EmotionalDamage_origin Original Image EmotionalDamage_red=0 Red Channel Removed
EmotionalDamage_green=0 Green Channel Removed EmotionalDamage_blue=0 Blue Channel Removed

Project implementation


Check DOCS.md for more information.

License


MIT License (see LICENSE.txt file).

References


Check REFERENCES.md for more information.