Small warm-up and fun projects using Python and C++.
- Introduction
- Setup and Requirements
- Repository Structure
- Circle Contours
- Surface of Cylinders
- References
This repository contains mini fun and warm up projects. The current projects are:
Python projects were written in Python3.x so at least python3.8 is needed. Used libraries;
- NumPy (
pip install numpy) - Matplotlib (
pip install matplotlib)
No projects have been written in C++ yet. They are upcoming.
- ./Circle-contours/
circle_contours.py: Plots colored contour lines for a circular function.- ./Surface-of-Cylinder/
surface_of_cylinders.py: Demonstrates OOP in Python by modeling circles and cylinders.
File: circle_contours.py
This script creates a contour plot for the function:
Why Use Contour Plots ?
- They provide a 2D way to visualize 3D surfaces.
- Useful for understanding radial distances or levels in a data set.
- Install dependencies:
pip install numpy matplotlib
- Run the script:
python3 circle_contours.py- A
matplotlibwindow displays the filled contour plot with a colorbar. You’ll see circular bands of color radiating outward from the origin.
File: surface_of_cylinders.py
Theory 1
A cylinder can be thought of as two parallel circles (top and bottom) plus a “curved surface” that, if cut and flattened, forms a rectangle.
-
Top and Bottom Circles:
- Radius: r
-
Area:
$\pi r$ 2
-
Curved (Lateral) Surface:
-
Unrolled into a rectangle with:
- Height: h
-
Width: the perimeter (circumference) of the circle,
$2 \pi r$
-
Area: Perimeter * Height =
$2 \pi rh$
-
Unrolled into a rectangle with:
Putting these together, the total surface area S of a closed cylinder is
where
The volume of a cylinder is based on the area of the circle’s base multiplied by the height:
In this mini-project, there are two classes:
-
Circle
-
Attributes:
radius
-
Methods:
-
get_area(): returns$\pi r$ 2 -
get_perimeter(): returns$2\pi r$
-
-
Attributes:
-
Cylinder (inherits from
Circle class)-
Attributes:
height- (inherits
radiusfromCircle)
-
Methods:
-
get_volume(): uses circle’s area * height implies$\pi r$ 2h -
get_surface(): uses the cylinder surface area formula:$2\pi r$ 2 +$2\pi rh$
-
-
Attributes:
By extending Circle with Cylinder, we can reuse Circle’s methods (get_area, get_perimeter) for computing the volume and surface of a cylinder.
-
Run the script:
python3 surface_of_cylinders.py
-
Follow the prompts on the terminal to enter radius and height for cylinder. An example input and output prompts are:
Enter radius: 5 Circle with radius 5 Area of the Circle is 78.53981633974483 Perimeter of the circle is 31.41592653589793 Enter height of cylinder: 10 Cylinder with height 10 and radius 5 Cylinder's volume is 785.3981633974483 Cylinder's surface area is 502.6548245743669
